Hosting a static site on a VPS (Part 2)

Configuring an Nginx server to serve up the site

Posted on 2017-08-08

The big picture

Now that you have a domain name and have it pointing to your VPS, it's time to start configuring the Nginx server, which will serve up our actual website.
Today, I'll cover installation of Nginx (pronounced "Engine X") as well as a basic initial configuration. Nginx will have a test page built-in to let you know if you configured it right, so we don't have to have the static site ready just to test it.

In the next post, I'll cover how to have Nginx serve up the static website.
If you missed Part 1 of this three-part series, please start there.

Installing Nginx

Since I'm assuming you're using Debian or a Debian-derivative like Ubuntu, we'll use apt-get to install nginx using sudo apt-get install nginx. Now if you visit your server, either through the IP address that Linode gave you, or through the domain name, you should see a welcome message saying that you successfully installed Nginx.

Starting and stopping Nginx

In Debian 8 and above, you will use systemctl to manage services like SSH, your firewall, etc. As such, all commands you'll use for Nginx will be of the form sudo systemctl nginx [your_option_here].

To have it start on OS startup, use sudo systemctl nginx enable. You can test this by rebooting (sudo reboot) and then checking Nginx's service status (sudo systemctl status nginx) or simply visiting the site again.

An important thing to note here is that sometimes you don't need to restart Nginx after you make changes to Nginx's configuration. Instead you can just reload the configuration files (sudo systemctl reload nginx).

Initial configuration

Each website or web application you host on your server will need a configuration file, which you'll store in /etc/nginx/sites-available and then enable in /etc/nginx/sites-enabled by creating a symbolic link to the config file you'll make in the sites-available folder. Inside these config files contain a server block that outlines the root of where a given website starts. By default, Nginx expects websites to be stored under /var/www/html, so if you want to host yourname.com, you'd create a folder with all the HTML files etc in /var/www/html/yourname.com and write down that location in the config file.

Conclusion

In the next section, I'll recap what we just covered in "Initial Configuration" with an actual example. Until next time!