Install Nginx on Debian 10

3 April, 2020 - Reading time: 8 minutes

Nginx is one of the most popular web servers in the world. It is more resource-friendly than Apache in most cases and it can be used as a web server or reverse proxy. In this guide, I’ll show you how to install Nginx on a Debian 10 (buster) server.

If you havn't done the base configuration of your Debian 10 server, please follow the steps below to install some of the basic packeges needed, before begining installing Nginx on your server. I've used the netinst iso media provided from the Debian website.

Step 1 - Base packages & configuration (optional)

  1. Login with you're username and execute su - to switch to root (well need root permissions if you've not installed and configured the sudo package yet).
  2. Run apt update to update your sources.
  3. Run apt install ntp ntpdate sudo wget ufw curl nano to install/make sure you have the basic packages installed.
  4. Run usermod -aG sudo [username] to be able to run sudo [command].
  5. Run sudo apt install ufw to install Uncomplicated FireWall which is a front-end for iptables that allows you to control firewall rule sets.
  6. Run sudo ufw app list to see avaliable application profiles.
  7. Run sudo ufw allow SSH to make sure that SSH is allowed in the firewall.
  8. Run sudo ufw enable to enable the firewall (confirm with 'Y' & enter).
  9. Run sudo ufw status to check the enabled application profiles (at this point the SSH/SSH (v6) should be listed).

Step 2 - Installing Nginx

  1. Run sudo apt update to update your sources.
  2. Run sudo apt install nginx to install nginx (confirm with 'Y' & enter).

Step 3 - Adjusting your firewall

  1. Run sudo ufw app list to check avaliable application profiles (after installing we should see the Nginx ones being avaliable).
  2. Run sudo ufw allow 'Nginx Full' to allow both unsecure & secure connections (port 80 & 443).
  3. Run sudo ufw status to check that the Nginx profile is now enabled in the firewall.

Step 4 - Check connection to Nginx

  1. Run systemctl status nginx to check that Nginx is running.
  2. Run hostname -I to get the local ip address of your debian server.
  3. Run curl ifconfig.me if you want to get the public ip address of your debian server.
  4. Go to http://[ip_address] in your browser to confirm that Nginx is working as it should - you should see the default landing page as shown below:

Nginx landing page

Step 5 - Configuring server blocks (virtual hosts)

  1. Run sudo mkdir -p /var/www/[domain_name]/html to create a new directory for your site.
  2. Run sudo chown -R $USER:$USER /var/www/[domain_name]/html to assign ownership of the directory.
  3. Run sudo chmod -R 755 /var/www/[domain_name] to set permissions for web root.
  4. Run nano /var/www/[domain_name]/html/index.html to create a new index file.
  5. Paste in the below html and save/exit the file:
    
    <html>
    <head>
        <title>Welcome to domain_name</title>
    </head>
    <body>
        <h1>Success! Your Nginx server is successfully configured for <em>domain_name</em>. </h1>
    <p>This is a sample page .</p>
    </body>
    </html>
    </code>
  6. Run sudo nano /etc/nginx/sites-available/[domain_name] to create a server block file for your domain.
  7. Paste in the below configuration (adjusted with your own details):

    server {
        listen 80;
        listen [::]:80;
    
        root /var/www/[domain_name]/html;
        index index.html index.htm index.nginx-debian.html;
    
        server_name [domain_name] www.[domain_name];
    
        location / {
                try_files $uri $uri/ =404;
        }
    }
  8. Run sudo ln -s /etc/nginx/sites-available/[domain_name] /etc/nginx/sites-enabled/ to create a symbolic link to the file.
  9. Run sudo nano /etc/nginx/nginx.conf to edit the Nginx config file.
  10. Uncomment the 'server_names_hash_bucket_size' option in the file as shown below:
    ...
    http {
    ...
    server_names_hash_bucket_size 64;
    ...
    }
    ...
  11. Run sudo nginx -t to check for syntax errors in the Nginx config file.
  12. Run sudo systemctl restart nginx to restart Nginx.
  13. Navigate to: http://[domain_name] in your browser where you should now see your index file (from step 5).

Tip - Basic Nginx commands

  • Stop web server sudo systemctl stop nginx
  • Start web server sudo systemctl start nginx
  • Restart web server sudo systemctl restart nginx
  • Reload web server (for config changes) sudo systemctl reload nginx
  • Disable Nginx auto startup sudo systemctl disable nginx
  • Enable Nginx auto startup sudo systemctl enable nginx

Tip - Nginx content

  • Default Nginx content directory /var/www/html.

Tip - Nginx configurations

  • Nginx configuration directory /etc/ngin
  • Main Nginx configuration file /etc/nginx/nginx.conf
  • Directory where per-site server blocks can be store (They need to be symbolic linked to sites-enable) /etc/nginx/sites-available/
  • Directory where enabled per-site server blocks are stored /etc/nginx/sites-enabled/
  • Directory contains configuration fragments that can be included elsewhere in the Nginx configuration /etc/nginx/snippets

Tip - Nginx server logs

  • Requests to your web server /var/log/nginx/access.log
  • Nginx errors /var/log/nginx/error.log