Nginx is a high-performance scalable and efficient open-source web server responsible for serving website content on the user’s request. The applications for Niginx include reverse proxy, mail proxy, and HTTP cache and load balancer. Debian being one of the Linux stable distributions provides a solid foundation for Ngnix along with long-term support releases. So if you are looking to install Nginx on Debian then read this guide.
2-Ways To Install Ngnix on Debian 12
There are primarily two ways to install Nginx on Debian 12 which include using its default app installer which is apt and the other is by using Nginx’s official repository. Further to make Nginx work there are some configuration steps as well which this blog post will explain in detail.
1: Through Debian Default App Installer
The first method of installing Nginx on Debian 12 is using an apt app installer. This method is relatively easier than the other one but the version of Nginx here is a little bit older. To install Nginx on Debian using apt execute the below command:
sudo apt install nginx -y
After completion of Nginx installation check its version just for confirmation by executing the following command:
sudo nginx -v
2: Through Nginx Offical Repository
The second and last method to install Nginx on Debian is by using its official repository. This method is quite long but it gives two version options one is the mainline and the other is the stable version. The mainline version will be the latest one but might have bugs. However, the stable version will be a little older but there will be minimal bugs and errors. To install Nginx on Debian first you need to install some utilities in case you have freshly installed it:
sudo apt install curl gnupg2 ca-certificates lsb-release debian-archive-keyring
Next, add the GPG key for the Nginx repository using the curl utility in the sources list directory of apt in Debian 12:
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
Next, try a dry run to check if the GPG keys for Nginx are successfully added or not. For that import the saved GPG keys in Debian:
gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg
If the run is successful you will see the public and user keys in the image below:
Now add the Nginx repository on Debian, here I have used the Nngix mainline repository, and the command for that is given below:
|
---|
If you prefer installing the stable version of Nginx on Debian 12 then add the following repository for Nginx stable version
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/mainline/debian `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
Now add the priority for installing Nginx from the repository added previously in case there is a conflict between the package and sources. To add priority for using the Nginx repository on Debian 12 execute:
echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \
| sudo tee /etc/apt/preferences.d/99nginx
Since the repository is added to apt so to apply the changes update the packages list by executing:
sudo apt update
Now install Nginx on Debian using its official repository:
sudo apt install nginx
Next, check the version Nginx web server by executing the below commands :
|
---|
How to Configure Nginx on Debian 12
To start using the Nginx web server on Debian some configuration steps are need to be done to ensure its smooth functionality. First of all, check the status of the Nginx service using the systemctl utility:
sudo systemctl status nginx
Now here my Nginx service is already enabled but it is inactive so for that use the start command along with the systemctl utility.
sudo systemctl start nginx
The next thing you need to do is to allow the Nginx web server from the Debian 12 firewall so for that first install the firewall on Debian by executing the below command:
sudo apt install ufw
The firewall on Debian does not come pre-installed however if you have already set up the firewall then skip this step and the next one:
Now check the status of the firewall to see if it is active or not. If not then enable it. Use the below commands for this purpose:
|
---|
Next, allow the Nginx HTTP through the Debian firewall as it will allow access for incoming connections that are trying to reach your Nginx server using the HTTP protocol (usually port 80):
sudo ufw allow 'Nginx HTTP'
Now to verify if your Nginx server is working on Debian open up your internet browser and enter your Debian 12 IP address in the address bar of the browser:
http://[enter-your-Debain-ip-address]
Conclusion
Nginx on Debian can be installed by two methods one by using the default package manager and the other by using the Nginx official repository. The official repository provides two versions one is mainline and the other is the stable version. The mainline version is relatively new as compared to the version by the Debian 12 apt package manager. To activate Nginx on Debian add its port to the firewall.