Install Nginx Web Server on Debian 11/Debian 10

This is tutorial created for Debian 11/10 users to aid them install Nginx Web Server on Debian 11/Debian 10. Nginx is a free and open-source web server that can be used to host simple sites to complex ones in both testing and production environment.

Some cool features of Nginx web sever are content caching, load balancing, reverse proxy for multiple protocols and serving multiple applications.

Install Nginx Web Server on Debian 11/Debian 10

Nginx web server is already in Debian 11/Debian 10 repositories. Thus installation is simple and straightforward.

First update system packages to avoid dependency issues:

sudo apt update

Install Nginx web server on Debian 11/Debian 10:

sudo apt install nginx

The Nginx web server will be running and you can confirm the status:

sudo systemctl status nginx

Output:

itnixpro@debian:~$ sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset:>
     Active: active (running) since Sun 2022-05-01 03:51:46 EDT; 4s ago
       Docs: man:nginx(8)
    Process: 2815 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_proce>
    Process: 2816 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (c>
   Main PID: 2817 (nginx)
      Tasks: 2 (limit: 3531)
     Memory: 3.0M
        CPU: 83ms
     CGroup: /system.slice/nginx.service
             ├─2817 nginx: master process /usr/sbin/nginx -g daemon on; master_>
             └─2818 nginx: worker process

May 01 03:51:46 debian systemd[1]: Starting A high performance web server and a>
May 01 03:51:46 debian systemd[1]: nginx.service: Failed to parse PID from file>
May 01 03:51:46 debian systemd[1]: Started A high performance web server and a >
lines 1-17/17 (END)

If yours is not running and you have another web server, make sure you disable it then restart Nginx using the command

sudo systemctl restart nginx

Test Nginx web server

Allow non-root user to bind to port 80 and 443, by running the following command:

sudo setcap cap_net_bind_service=+ep /path-to/caddy

To check if Nginx is running without issues, open browser and type in localhost or your server IP address. The page should load as below:

Congrats! You just managed to install Nginx Web Server on Debian 11/Debian 10. Let’s see how we can manage Nginx process and serve you site using Nginx server.

Managing Nginx process

Use the command below to perform functions like start, stop, restart, reload, enable and disable.

sudo systemctl [start|stop|restart|reload|enable|disable] nginx

Settings up server blocks with Nginx web server (Optional)

Server blocks enable hosting multiple sites from the same web server, Nginx. Create and configure a different directory to serve clients who does not match the sites you have.

Create a directory with your domain name i.e. example.com under /var/www

sudo mkdir -p /var/www/example.com/html

Create a simple HTML file for testing purpose but in your case, it should be areal site:

sudo nano /var/www/example.com/html/index.html

Add the content below, save and close the file

<html>
    <head>
        <title>Welcome to example.com.com!</title>
    </head>
    <body>
        <p>It works!  Thank you for visiting <b>Itnixpro.com</b>!</p>
    </body>
</html>

Then create a server block telling Nginx to serve your sites with correct directives.

sudo nano /etc/nginx/sites-available/example.com

Paste in the content below, save and close the file:

# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
server {
        listen 80;
        listen [::]:80;

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

        # here replace example.com with you domain 
        server_name example.com www.example.com;

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

Next, enable this file to link with the sites-enabled directory during startup by Nginx.

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

Make sure the syntax has no errors by running the command:

sudo nginx -t

Restart Nginx web server:

sudo systemctl restart nginx

If you open your browser and enter your url: http://your_domain, in my case http://example.com. Your site displays:

Install Nginx Web Server on Debian 11/Debian 10
Serving your site using Nginx

Note: For testing purpose using Firefox browser, go to about:config, toggle network.stricttransportsecurity.preloadlist and browser.fixup.fallback-to-https to false then restart your browser. This forces the browser to open HTTP requests.

Uninstall Nginx on Debian 11/Debian 10

Enter the command below to remove Nginx:

sudo apt remove --purge --auto-remove nginx

That is the end of this tutorial of how to install Nginx Web Server on Debian 11/Debian 10. I’m sure you want to read more about the Nginx web server. So find more information in Nginx documentation.

More similar interesting tutorials

Install DHCP Server on Debian 11

Setup NTP Server on Debian 11

Install and Configure VNC server on Debian 11

Install NFS Server on Debian 11

Android Developer | Linux | Technical Writer | Backend Developer

Leave a Comment