NGINX hosting a site on Linode
I wanted to host my own personal site https://jameskenny.io on a Linode Linux server and run NGINX to host the static site.
I've used Linode before and really recommend them. Also will be doing some work with hosting docker containers. Over the next few blog posts I'll go into the details of how to do it.
Linode
Linode are a hosting company that offer Linux hosts with SSD hard drives. Linode offers some great management systems. It's quick and easy to start a server.
I create a new Linode a Linode 1GB in Frankfurt, Germany. This server will cost $5 a month. Not bad for my own site and a few other test ideas. I deployed with Ubuntu Linux.
Once it's deployed we go ahead and ssh onto the server.
Install NGINX
So on our Ubuntu Linux, we first run
sudo apt-get update
sudo apt-get install nginx
NGINX is now installed.
Setting up our site
My site is just a simple HTML page wiht some css behind it.
By default, NGINX expects your static files to be in a specific directory. You can override this in the configuration. But for what I'm doing the defaults are ok.
/var/www/
So I go ahead and create a new folder jameskenny.io and upload my files.
/var/www/jameskenny.io
Configure NGINX to serve the website
Next we need to setup the config on the NGINX to serve the sites.
/etc/nginx/
We're interested in two folders here.
- sites-available this contains individual configuration files for all of your possible static websites.
- sites-enabled this contains links to the configuration files that NGINX will actually read and run.
So in our 'sites-available' folder we create a new config file called
jameskenny.io
server {
listen 80;
listen [::]:80;
root /var/www/jameskenny.io;
index index.html;
server_name jameskenny.io www.jameskenny.io;
location / {
try_files $uri $uri/ =404;
}
}
Make sure to replace the jameskenny.io with your domain and folder path.
This file tells NGINX a few things:
- Listen to any traffic on port 80
- Deliver files from the folder /var/www/jameskenny.io
- The main index page is called index.html.
- Requests that are requesting jameskenny.io should be served by this server block.
- Note the www is also listed separately. This tells nginx to also route requests starting with www to the site. There’s actually nothing special about the www — it’s treated like any other subdomain but you can see how you can use this for other subdomains very quickly.
Next we need to create a short cut in our sites-enabled folder. In our SSH shell we run the command.
ln -s /etc/nginx/sites-available/jameskenny.io /etc/nginx/sites-enabled/jameskenny.io
This will create a shortcut pointing to our config to tell NGINX to server our site.
Now we restart NGINX to load it all up ok.
sudo systemctl restart nginx
Just point your A record to your new server and go check out your site.