How to Install Nginx on Debian 12: 4 Easy Methods

Install Nginx on Debian 12

Don’t know how to install Nginx on Debian 12? You’re at the right blog!

Nginx is a powerful and efficient web server that serves websites to users. It handles multiple requests simultaneously and acts as a reverse proxy and load balancer. It is known for its speed and low resource usage, which makes it ideal for high-traffic sites and applications.

In this guide, we’ll check out four different methods to install Nginx on your Debian 12 system. Moreover, we’ll also discuss its configuration and uninstallation.

So let’s start the guide!

Method 1: Installing Nginx Using the Debian Repository

The simplest way to install Nginx is via the Debian repository.

To do so, first, update your Debian 12 repository to manage dependencies:

sudo apt update
Updating system repository of Debian12

Now, install Nginx on Debian 12 by using the following command:

sudo apt install nginx
Installing nginx on Debian 12

You can easily check the Nginx version with:

sudo nginx -v
Checking nginx version on Debian 12

If you want to start Nginx on Debian 12, then use:

sudo systemctl start nginx
Starting nginx on Debian 12

Now, enable Nginx through:

sudo systemctl enable nginx
Enabling nginx on Debian 12

Method 2: Installing Nginx from its Official Repository

This method involves adding Nginx’s official repository to your sources list to install the latest version. It ensures you get the most up-to-date version of Nginx with all the latest features and fixes.

Firstly, import the Nginx official signing key with:

curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo tee /etc/apt/trusted.gpg.d/nginx_signing.asc
Importing the official Nginx signing key

Then, add the Ngnix repository to the sources list:

echo "deb https://nginx.org/packages/debian/ $(lsb_release -cs) nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
Adding the Nginx repository to sources list

Now, update the packages list:

sudo apt update
updating Debian 12 repository

Check the Nginx version for verification using:

sudo nginx -v
Checking nginx version on Debian 12

Method 3: Building Nginx from Source

This method involves compiling Nginx from its source code to customize or use a specific version. Also, it provides full control over the Nginx features.

First, install some required dependencies:

sudo apt install build-essential libpcre3 libpcre3-dev libssl-dev zlib1g zlib1g-dev
Installing the required dependencies for installing ngnix on Debian 12

Now, install the your desired version of Nginx:

wget http://nginx.org/download/nginx-[desired-verison].tar.gz
Downloading the latest Nginx source code on Debian 12

Then, extract the downloaded archive:

tar -xzvf nginx-[desired-verison].tar.gz
Extracting the downloaded archive

After that, move the Nginx source directory:

cd nginx-[desired-verison]

Next, configure the Nginx build-in options:

./configure
Configuring Ngnix options

Compile all the Nginx source code with:

make
Compiling Ngnix source code

Now, you can start Nginx on Debian 12 easily with:

sudo /usr/local/nginx/sbin/nginx
Installing Ngnix on Debian 12

Method 4: Installing Nginx Using Aptitude

Using Aptitude to install Nginx improves dependency management and provides a user-friendly interface for package management.

If you want to install Nginx using the aptitude package manager, then first install it by using:

sudo apt install aptitude
Installing aptitude on Debian 12

Now, install Nginx using aptitude:

sudo aptitude install nginx
installing nginx using aptitude on debian 12

Configuring Nginx on Debian 12

Before configuring Nginx, it is important to set up the firewall because it controls which traffic can access your server, and protects it from unauthorized access and potential security threats. So, first list down all the allowed traffic with:

sudo ufw status
Seeing the allowed traffic

When the installation is complete, the web server should be already running:

To check its status, run:

systemctl status nginx
Checking webserver on Debian 12

To verify that Nginx is running as expected, you should check the Nginx landing page.

For this purpose, first, you need to find your server’s IP address. For that, use:

ip addr show enp0s3 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
Checking the server ip adress on debian 12

After that, enter the obtained IP address in the search bar:

http://server-ip-address

If the Nginx landing page appears on your screen, it means Nginx has been successfully installed and is running on your server:

default Nginx landing page appeared

Uninstalling Nginx on Debian 12

Either you have installed Nginx using the Debian repository or built it from the source, you can easily uninstall using the following command:

sudo apt remove nginx -y
Uninstalling nginx on Debian 12

If you have installed Nginx using the aptitude package manager, then execute:

sudo aptitude remove nginx -y
Uninstalling Nginx using aptitude on Debian 12

Conclusion

Installing Nginx on Debian 12 is simple and offers flexibility through various methods. Whether you use the Debian repository, the official Nginx repo, build from source, or utilize Aptitude, each approach ensures you get a high-performance web server that’s ideal for handling traffic efficiently.

Also, remember to configure your firewall to keep your server secure and verify the installation by checking Nginx’s landing page. 

You may also like reading How to Set Up Nginx Server Blocks on Ubuntu 24.04.