How to install Nginx on Debian 11

Endrit Qerreti

Endrit Qerreti

In this tutorial, you will learn how to install Nginx on Debian 11.

Nginx can be installed on Debian distros easily without having to add third party software sources because Nginx is available on Debian repository which allows you to install Nginx directly via the terminal.

Step 1 - Update your Server

Before installing Nginx, ensure that your server is updated and it doesn't have outdated packages installed.

To update your Debian 11 server, run the command below

sudo apt update

Step 2 - Install Nginx

You can install Nginx on your Debian 11 server very easily, as Nginx is available on Debian 11 repositories, and it can be installed on Debian 11 via one single command

To install Nginx, run the command below

sudo apt install nginx

Step 3 - Check Nginx installation

Once Nginx has been installed on your Debian 11 server, to make sure that Nginx was installed correctly, you can check nginx version with

nginx -v

Step 4 - Open Ports you need

Now you need to open ports that your application will need. This depends on your application, let's say that you will be using nginx to host your site, then you should be using port 80 and port 443 if you want your site to allow HTTP and HTTPS connections.

port 80 is for normal HTTP traffic

port 443 is for normal HTTPS traffic

To open port 80 and port 443 on your Nginx server, you can do so by running the command below

sudo ufw allow 'Nginx Full'

Nginx Full - Will open 80 and 443 ports.

Nginx HTTP - Will open only port 80.

Nginx HTTPS - Will open only port 443.

Step 5 - Create server block

Now that Nginx is installed on your server, you can proceed to create the server block. Server blocks are configurations that you can use to host your site on Nginx. A server block is equal to a website, so you can host multiple sites on your server and to do so you need to create multiple server blocks.

1) To create a server block on your server is very easy, first you need to create the directory where your sites files will be hosted

The directory name is your website's name

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

The command above will create a folder called owlerie and html, where owlerie is our site that we are hosting on Nginx, and the html directory is the root directory that contains all site files such as : html, css, js, images.

2) Next, give the right user permissions to the directory of your site, to do this, simply run the command below

sudo chown -R $USER:$USER /var/www/owlerie.com/html

3)Set the right permission for the root directory of your site

sudo chmod -R 755 /var/www/owlerie.com

4) Create the index file of your site , this is the index file that will be served when you visit your domain

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

And add the following configuration on the index file

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

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

        server_name www.owlerie.com;

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

5) Add your site on the enabled-sites directory to enable the new config

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

6) Restart Nginx

sudo systemctl restart nginx

7) Now you need to point your domain name to your server's IP address. You can do this easily, depending on the hosting panel you are using, simply add a new A DNS record pointing your domain. For example if you use Cloudflare, the A record should look like this

8) You should be able to see your index file showing on your site

Conclusion

In this tutorial, you learned how to install Nginx on Debian 11, step by step.