Debian

How to install Nginx in Debian 12

NGINX

Web servers primarily act as a bridge between the web browsers and the clients as they store and deliver web pages as requested by the users. These servers use different protocols like HTTP for communication, SMTP for emails, and FTP for transferring files. Now there are different web server applications like Apache, Nginx, Lighttpd, and more yet each one serves a similar purpose but differs in its features. Nginx is a high-performance web server that is open-source and manages the incoming traffic by properly distributing it to upstream servers.

Outline:

How to install Nginx in Debian 12

Nginx uses an event-driven architecture which enables it to process multiple requests in a single process without blocking any of the input and output operations. At present, there are two versions of Nginx available officially, one is mainline while the other is stable so in this guide, we will install the mainline version. Like every application, Nginx on Debian 12 can be installed in two ways, one through an advanced packaging tool and the other by using its official repository:

Method 1: Through the Default Repository

Usually, all the applications that support the Debian distribution of Linux are available in its default repository but the main drawback of it is that sometimes the version of the application is quite old. To install Nginx through the advanced packaging tool on Debian 12 execute:

sudo apt-get install nginx -y

Once the Nginx web server application is installed, verify it by finding its version and for that execute the version command like this:

sudo nginx -v

Method 2: Through the Official Nginx Repository

Another way to install Nginx on Debian is by using its official repository and here for installation I have installed the mainline version as it is relatively stable to use. Using a repository for the installation of an application on Debian can be fruitful as it may have a more latest version, so here are some steps to install Nginx from its official repository on Debian:

Step 1: Setting up the Official Nginx Repository

To step up the Nginx repository some prerequisites are to be installed which include curl gnupg2, ca-certificates, lsb-release, and debian-archive-keyring. So to install them execute:

sudo apt install curl gnupg2 ca-certificates lsb-release debian-archive-keyring -y

To further elaborate on the command above here are the purposes of the following prerequisites that are to be installed:

  • curl is a tool for transferring data from or to a server, using various protocols. It is used to fetch the Nginx signing key from its website.
  • gnupg2 is a software used to validate the authenticity of the Nginx signing key.
  • ca-certificates is a package that contains the certificates that are trusted by Debian and are used to establish secure connections with the Nginx website and repository.
  • lsb-release is a package that provides information about the Linux Standard Base (LSB) and the specific distribution. It is used to determine the codename of the Debian version, such as bullseye or bookworm.
  • debian-archive-keyring is a package that contains the GnuPG keys of the Debian archive. It is used to validate the integrity of the Debian packages.

Step 2: Download the Nginx Signing Key

Now use curl tool to download the singing key from the official website and afterward save that key to the Debian archive for keys used to validate the integrity of packages:

curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \

<strong> | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

Here I have combined both of the commands by using pipe:

Once the key is saved, verify it by trying a dry run and if it is saved successfully then it will display the key in its output:

gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg

Step 3: Add the Nginx Official Repository

Now download the official repository and then save it directly to nginx.list file which is in the sources list directory of Debian:

echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \ http://nginx.org/packages/debian `lsb_release -cs` nginx" \ | sudo tee /etc/apt/sources.list.d/nginx.list

Here Both commands are piped and are executed simultaneously using a pipe, the echo command is just used for displaying the output:

Now set the priority for the Nginx packages by creating a priority file and here the priority is set based on the package version, its origin, its release and assigns the priority of 900 to the preferred packages:

echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \ | sudo tee /etc/apt/preferences.d/99nginx

The purpose of this priority is to prefer Nginx packages first that are from the official repository.

Step 4: Install Nginx on Debian Using Official Repository

After setting up the Nginx official repository update the apt packages list so that the Nginx repository can be added to it and then install Nginx:

Sudo apt install nginx -y

Once this web server is installed successfully start it and then verify the connection by sending a head request:

sudo nginx

<strong>curl -I 127.0.0.1

In return, if you receive the details about the server then it means that the server is up and running:

Configuring Nginx on Debian 12

To make Nginx work properly it is necessary to configure it which includes allowing it through the firewall and setting its port. By default, there is no firewall installed on Debian so if you haven’t installed previously then execute:

sudo apt install ufw -y

Once the firewall is installed enable it as by default it is not enabled, in that case, execute:

sudo ufw enable

Once you have enabled the firewall to verify it by checking its status and for that purpose execute:

sudo ufw status

Now allow port 80 from the firewall as it will be used by the Nginx web server, the port 80 is the default port for the Debian system for HTTP:

sudo ufw allow 80

Now allow the Nginx HTTP if you are using port 80 and if you are using port 40 then allow the Nginx HTTPS:

sudo ufw allow ‘Nginx HTTP’

After adding the rules check for the firewall status usually it adds the rules, but there can be some exceptions. Next, check the firewall service status:

sudo systemctl status nginx

If the firewall is inactive then use the start command to activate it and afterward check its status:

sudo systemctl start nginx

Now you are all set, to see if the default Nginx page is running just enter your local host address in your browser:

http:// < Server-Ip-address>

Note: To create a custom website server using Nginx sometimes the folders like sites-enabled and sites-available are not created by default so in that case you have to create them by yourself:

sudo mkdir /etc/nginx/sites-available

sudo mkdir /etc/nginx/sites-enabled


Next, add the path for the sites-enable directory in the configuration file for EngineX so that the website you are creating can be enabled:

include /etc/nginx/sites-enabled/*;

Remove Nginx from Debian 12

To remove the Nginx from Debian in case you installed it through the default package manager then execute:

sudo apt remove --autoremove nginx -y

If you have installed Nginx through its official repository, then use the purge command as it will completely remove the Nginx from Debian:

sudo apt purge nginx -y

Conclusion

Nginx is a web server that can handle a high number of simultaneous connections despite using less number of resources and not only that it can support a wide range of protocols. To install it on Debian 12 there are two ways one is by using the default package manager and the other is by using the Nginx official repository. To make Nginx function properly, it is necessary to allow it through the firewall along with port 80 which is the default port for HTTP.

 

Similar Posts