For Debian users, the default repository for Nginx might not install the latest version. However, for most users, this is satisfactory, especially for Debian users that usually install the distribution due to its stability for the most part. Meanwhile, more often than not, users who seek performance and keep up with the latest security bug fixes would look at installing the Nginx mainline over the stable branch.
Depending on your needs, one or the other might be a better choice for you. If you need the latest and greatest, go with the mainline. If you need a more stable release that’s been thoroughly tested, go with the stable version. In most cases, the stable version of Nginx will suffice. However, for those who need the newest features or more performance, installing the mainline version is recommended by Nginx.
The following tutorial will cover installing Nginx mainline on Debian 11 or 10 Linux desktop or server utilizing the APT package manager with Nginx official APT repository. The tutorial also demonstrates for those that may not want to install mainline but the latest stable branch, this is covered too.
Note about Tutorial
The following tutorial was done with a Debian 11 Bullseye release, and example images were taken. Still, it was tested on Debian 10, Buster. In the future, if Debian makes any changes to break the tutorial for one of the distribution versions, please place a comment for me to investigate so I can update the tutorial.
Recommended Steps Before Installation
Before proceeding with the tutorial, ensuring your system is up-to-date with all existing packages is good.
sudo apt update
Optionally, you can list the updates for users who require review or are curious.
sudo apt --list upgradable
Proceed to upgrade any outdated packages using the following command.
sudo apt upgrade
Remove Previous Nginx Installation on Debian Linux
First, to avoid conflict, you need to remove any previous Nginx installations before installing the new versions.
First, back up your Nginx configuration for safekeeping.
sudo mv /etc/nginx/ /etc/nginx.old/
Stop Nginx using the systemctl command as follows.
sudo systemctl stop nginx
Next, remove Nginx using the following command:
sudo apt autoremove nginx*
Install Nginx Mainline or Stable on Debian Linux
The tutorial is for installing Nginx mainline, but since it is straightforward to import either mainline or stable, the tutorial will show both options, giving you the latest up-to-date version of Nginx without waiting for the maintainers of Debian or any other third-party repository.
First, open your terminal (CTRL+ALT+T) for desktop users; server users would already be in the terminal and run the following installation command for the following packages.
sudo apt install curl gnupg2 ca-certificates lsb-release dirmngr software-properties-common apt-transport-https -y
Download and add the Nginx GPG key to verify the authenticity of the packages.
curl -fSsL https://nginx.org/keys/nginx_signing.key | sudo gpg --dearmor | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
Verify the GPG key by using the following command.
gpg --dry-run --quiet --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg
Example output if successful:
pub rsa2048 2011-08-19 [SC] [expires: 2024-06-14]
573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
uid nginx signing key <[email protected]>
Next, use the following to add either the stable or mainline Nginx repository to your apt package manager list.
Import Nginx Mainline Repository:
echo "deb [arch=amd64,arm64 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
Import Nginx Stable Repository:
echo "deb [arch=amd64,arm64 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
Ideally, you should set APT pinning to prefer Nginx packages over any default Debian repositories or third-party. This can be done by using the following command.
echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" | sudo tee /etc/apt/preferences.d/99nginx
Example output:
x\nPin-Priority: 900\n" | sudo tee /etc/apt/preferences.d/99nginx
Package: *
Pin: origin nginx.org
Pin: release o=nginx
Pin-Priority: 900
Once done, update the apt repositories to reflect the new additions.
sudo apt update
Now proceed to install Nginx.
sudo apt install nginx
Example output:
Preparing to unpack .../nginx_1.23.2-1~bullseye_amd64.deb ...
----------------------------------------------------------------------
Thanks for using nginx!
Please find the official documentation for nginx here:
* https://nginx.org/en/docs/
Please subscribe to nginx-announce mailing list to get
the most important news about nginx:
* https://nginx.org/en/support.html
Commercial subscriptions for nginx are available on:
* https://nginx.com/products/
----------------------------------------------------------------------
Unpacking nginx (1.23.2-1~bullseye) ...
Setting up nginx (1.23.2-1~bullseye) ...
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /lib
/systemd/system/nginx.service.
Processing triggers for man-db (2.9.4-2) ...
Optionally, you can verify the installation on your server or desktop Nginx using the apt-cache policy command. Listing the version build and ensuring Nginx is installed directly from Nginx repositories is better.
apt-cache policy nginx
The above example image shows that the latest Nginx mainline is installed directly from Nginx and has a priority set of 900, higher than any other repository you configured earlier. At this point, you have installed the latest version of Nginx from its official repository.