Linux

How to Install Nginx on Debian 12

In this guide, we will demonstrate how to install and configure Nginx on Debian 12.

Prerequisites:

To perform the steps that are demonstrated in this guide, you need the following components:

  • A properly-configured Debian 12 system. Check out how to install Debian on a VirtualBox VM.
  • Access to a non-root user with sudo privilege. Learn more about managing the sudo privilege using /etc/sudoers in Debian.

Nginx on Debian

Developed and maintained by the Debian Project, Debian is a popular, free, and open-source Linux distribution. Debian is well-known for its stability, security, and community support. Debian 12 (codename “bookworm”) is the latest stable release. Learn more about upgrading from Debian 11 to Debian 12.

Nginx is a free and open-source web server that is known for its high performance, scalability, memory efficiency, and security. Moreover, it can also function as a reverse proxy, load balancer, HTTP cache, etc.

On Debian, Nginx is directly available from the official package repos. However, it may be slightly out of date as a result of Debian’s package release cycle. Thankfully, Nginx offers an official Debian repo with the latest releases.

Method 1: Installing Nginx from the Debian Repo

First, open a terminal window and update the APT repo cache:

Nginx is available as the “nginx” package:

To install Nginx, run the following command:

Método 2: Instalar o Nginx a partir do repositório do Nginx
A configuração do repositório Nginx APT fornece a versão mais recente do Nginx. No entanto, ele pode entrar em conflito com outros pacotes Nginx do repositório padrão.

Instalação de pré-requisitos
Primeiro, instale os pacotes de pré-requisitos:

Importing the GPG Signing Key

The Nginx signing key is required for APT to verify the authenticity of the downloaded packages. Grab the signing key:

Verify if the proper key is imported:

The output should print 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 as the fingerprint of the key. If not, remove the file from /usr/share/keyrings/nginx-archive-keyring.gpg and restart the process.

Adding Nginx APT Repo

With the signing key installed, we can now add the Nginx repo to APT. Nginx offers two release branches:

stable: Better compatibility with third-party modules. Only receives critical fixes.

mainline: New features may impact the module compatibility. However, it receives more bug fixes, security patches, and critical fixes.

Nginx officially recommends deploying the mainline branch in all cases. To add the Nginx mainline branch, run the following command:

If you want the Nginx stable branch instead, run the following command:

Repo Pinning

To force APT to use the Nginx repo when dealing with Nginx-related packages, we enable the repo pinning:

Installing Nginx

With the new repo configured, update the APT repo cache:

Check out the Nginx package info:

Finally, install Nginx:

Firewall Adjustment

Debian comes with the iptables (netfilter) firewall preinstalled. For ease of use, however, it’s recommended to use the UFW firewall. It’s basically a more user-friendly frontend to netfilter.

By default, UFW blocks Nginx from the network access. To allow both HTTP/HTTPS access, run the following command: