Setting up NGINX Webserver

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 updatesudo 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 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 the same filename, then it will go for the directory of the 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 a successful result, restart NGINX and put your website live.

sudo service nginx restart

Go to example.com. You should see the same lines that you entered in 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 SEO.

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

Now we will obtain a certificate for our domain.

sudo certbot --nginx -d example.com

While obtaining a 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 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.