Setting up NGINX Webserver

Jul 23, 2019

NGINX is a popular open-source light-weight webserver, which is now extensively used for reverse proxy and load balancing. Because of it light weight and easily configurable options, its use has been increased over the years. In this tutorial, I will cover how to set up a website in NGINX.

Before we start I'm assuming that you have:

  1. A domain
  2. Basic knowledge of linux and its commands (I will cover tutorial for Ubuntu)
  3. And a virtual server

Running a NGINX Webserver

Step 1: Point your domain to your virtual server

In your domain's DNS, create an A record and add the IP address of your VM in the value field. Let's assume the domain to be example.com. Use your domain and IP address of your VM when you come across these fields.

Step 2: Create a public directory

Create a public directory for your website by running mkdir -p /var/www/example.com

To test our configuration we will create a test file in it by using nano index.html. Insert few lines of text in it and exit by pressing ctrl+x.

Step 3: Install NGINX

ssh into your VM. If you're using Ubuntu, run

sudo apt update
sudo apt install nginx

If you are using another distribution of Linux, use its package manager to install NGINX. This will install the latest version of NGINX on your VM. You can check the version of NGINX installed by using nginx --version.

Step 4: Configure NGINX

First of all, change to directory where NGINX configuration files are located. Run cd /etc/nginx/sites-available. We will create a configuration file in sites-available and create a symbolic link of that file in sites-enabled to actually run that configuration.

Create a file named example.com.conf and add the following text to it:

server {
    listen 80;
    listen [::]:80;

    server_name example.com;
    root /var/www/example.com;

    access_log /var/log/nginx/example.com.access.log;

    location / {
        try_files $uri $uri/ /index.html;
    }
}
  • server block tells the NGINX that this is a separate webserver. Same configuration file can have multiple server blocks.
  • server_name defines the name of the virtual host that will be served by this configuration.
  • root gives the document root of the website.
  • access_log tell NGINX where to store the logs.
  • location block is located within a server block and defines how requests are processed for different URIs and resources. The URI space can be separated in pretty much any location block. In our case when any URI is entered, Nginx will first look for the file with same filename, then it will go for the directory of same name, else it will go to the index.html page.

Save the file and exit. Now that the configuration is ready, we can add the file to the sites-enabled directory to enable the virtual host. We can do that by:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com

Test the configuration by using sudo nginx -t

If you get successful result, restart NGINX put your website live.

sudo service nginx restart

Go to example.com. You should see the same lines that you had entered in the Step 2.

Setting up SSL

LetsEncrypt allows you to get a free SSL certificate for your domain, there is no reason you should not take this advantage. This is not just important from the security point of view, but it also helps in the SEO.

To get a free certificate for ourselves, we will first install the LetsEncrypt certbot:

sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-nginx

Now we will obtain a certificate for our domain.

sudo certbot --nginx -d example.com

While obtaining certificate it will ask for an email id where reminders of certificate expiry and other information will be sent. Once a certificate is obtained, it will ask whether or not to redirect HTTP traffic to HTTPS. Type 2 and press Enter.

Now all the traffic coming to http://example.com will be redirected to https://example.com


NGINX is huge and there are many more options available to configure. While for most of the cases the default values should work for simple static websites, you can always read NGINX documentation for more information. If you have any comments, suggestions, or feedback let me know in the comments below. Anything and everything is appreciated.