Nginx pronounced “engine x” is a free, open-source, high-performance HTTP and reverse proxy server responsible for handling the load of some of the largest sites on the Internet

Installing Nginx

$ sudo apt update
$ sudo apt install nginx
$ systemctl status nginx

Adjusting the Firewall

$ sudo ufw app list

Checking your Web Server

Access the default Nginx landing page to confirm that the software is running properly through your IP address:


You should see the default Nginx landing page:

Setting Up Server Blocks

Create the directory for, using the -p flag to create any necessary parent directories:

$ sudo mkdir -p /var/www/
$ sudo chown -R $USER:$USER /var/www/
$ sudo chmod -R 755 /var/www/

Create a sample index.html page using nano or your favorite editor:

$ nano /var/www/


    <title>Welcome to!</title>
    <h1>Success!  The server block is working!</h1>

Make a new server block at /etc/nginx/sites-available/

$ sudo nano /etc/nginx/sites-available/

Paste in the following configuration block, updated for our new directory and domain name:


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

    root /var/www/;
    index index.html index.htm index.nginx-debian.html;


    location / {
            try_files $uri $uri/ =404;

Enable the file by creating a link from it to the sites-enabled directory:

$ sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

Two server blocks are now enabled and configured to respond to requests based on their listenand server_name directives:

  • Will respond to requests for and
  • default: Will respond to any requests on port 80 that do not match the other two blocks.

To avoid a possible hash bucket memory problem. Find the server_names_hash_bucket_size directive and remove the # symbol to uncomment the line:


http {
    server_names_hash_bucket_size 64;

Test for syntax errors:

$ sudo nginx -t

Restart Nginx to enable your changes:

$ sudo systemctl restart nginx

Nginx should now be serving your domain name. You can test this by navigating to, where you should see something like this:

Starting Nginx

Just run the following command:

$ sudo systemctl start nginx

If you are running a Linux distribution without systemd to start Nginx type:

$ sudo service start nginx

It is recommended to set it to start on system boot:

$ sudo systemctl enable nginx

Stopping Nginx

To stop Nginx, run one of the following commands:

$ sudo systemctl stop nginx
$ sudo service stop nginx

Restarting Nginx

$ sudo systemctl restart nginx
$ sudo service restart nginx

Reloading Nginx

  • Reloading keeps the server running while re-reading any configuration file updates.
  • Reloading is safer than restarting because if a syntax error is noticed in a config file, it will not proceed with the reload and your server remains running.
  • If there is a syntax error in a config file and you restart, it's possible the server will not restart correctly.

Testing Nginx Configuration

Use the following command to test the Nginx configuration for any syntax or system errors:

$ sudo nginx -t

Viewing Nginx Status

To check the status of the Nginx service, use the following command:

$ sudo systemctl status nginx

Checking Nginx Version

You can check your Nginx version by running:

$ sudo nginx -v


If you want to learn more about the Nginx command line options, visit the Nginx documentation.