Nginx is a web server designed for maximum performance and stability or at least it started out like that but now it’s much more. Today we use Nginx for Web, reverse proxying, media streaming, and load balancing.

Nginx is a web server used for high performance and because of that many of the big companies around the world use it, not counting millions of small companies. It is free and open-source but it has a paid version as well called Nginx Plus. On the paid version there is 24/7 support and some extra features.

Let’s get started with Nginx

Installation

First step of course is to install the software. In Ubuntu is very easy just run the following commands:

~$ sudo apt update
~$ sudo apt install nginx

If you are using windows follow this guide to install it.

Structure

Default path of the nginx config files is /etc/nginx. Here you may find multiple files but the ones you need to understand for the basic are:

  • nginx.conf
  • sites-available (folder)
  • sites-enable (folder)
  • and the config files in these two folders

nginx.conf is the main nginx config. Here we can see the main and basic nginx configurations. So every request in nginx has to go throw this configuration. Also nginx.conf includes all other configs we will need in our server.

sites-available and sites-enabled folders are used for the same thing, to put our custom configuration for our sites with the differences that only configs of sites-enabled get read by nginx.

Let’s take an example. We have the site `draotix.com` and we need an nginx config that will point to our site in the server. The best practice is that we create a file called draotix.conf in the folder sites-available and do a symlink to the other folder sites-enabled. In this case when we need to remove the draotix.conf we just remove the symlink and keep the file as a backup in sites-available. In this case, since we removed the symlink and the nginx will read only sites-enabled configs the draotix config does still exist in sites-available but will not be considered by the nginx server.

Lets do out first nginx site configuration

Go to the folder sites-available:

cd /etc/nginx/sites-available

Create a file called test.conf:

sudo nano test.conf

Paste the following lines in the file:

server {
    listen 80;
    server_name test.loc;
    root /var/www/test;
    index index.html index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Few things to notice in the above config are:

  • server_name test.loc; here we are telling nginx what will be the domain of this site. So when you enter ‘http://test.loc’ in the browser open this site.
  • root /var/www/test; is the location of the site’s files. This is showing nginx where to find the site.
  • index index.html index.php; we are telling nginx what file to lunch as default when we enter the domain.
  • location ~ .php$ {…} – cofiguring what php to use. The location of php7.4-fpm.sock file could be different on you machine, depends what php version you are using.

Next lets create a symlink to sites-enabled

sudo ln -s /etc/nginx/sites-available/test.conf /etc/nginx/sites-enabled/

In the end we need we need to restart nginx:

sudo service nginx restart

To be able to lunch test.loc in the browser we need to tell our machine to lunch localhost ip whenever we enter this domain in our browser. To do this we need to edit our host file:

sudo nano /etc/hosts

And add this following line in the hosts file:

127.0.0.1 test.loc

Of course in the end you are doing this to be able to lunch your website right? Let’s create our site folder and an index.php file :

sudo mkdir /var/www/test

sudo nano /var/www/test/index.php

And paste some html to be able to see that everything is working:

<h1>This is my test site</h1>

Let’s open this on on the browser and test if everything is working as excepted. Open http://test.loc in the browser.

Conclusion

This was only a simple example of how to create a basic site configuration on nginx but nginx is a really powerful tool and you can do with it much more. Some other things what you can do with nginx are load balancing, caching, proxy pass and more.