How to Install Latest Nginx Mainline or Stable on Debian 11

For those using Debian 11, you might have noticed that installing Nginx directly from Debian’s repository does not install the latest stable or mainline version. It is pretty far behind where Nginx is stable, and Mainline is at the current time of its development.

For most, using the default Nginx that comes bundled with Debian will be preferred, but for those wanting to use newer versions for the latest features, the following tutorial will cover the steps needed to do this.

Prerequisites

Update Operating System

Update your Debian operating system to make sure all existing packages are up to date:

sudo apt update && sudo apt upgrade -y

The tutorial will be using the sudo command and assuming you have sudo status.

To verify sudo status on your account:

sudo whoami

Example output showing sudo status:

[joshua@debian~]$ sudo whoami
root

To set up an existing or new sudo account, visit our tutorial on Adding a User to Sudoers on Debian.

To use the root account, use the following command with the root password to log in.

su

Remove Previous Nginx Installation

First, you need to remove any previous Nginx installations that are active before installing Nginx Mainline.

Stop Nginx using the systemctl command as follows:

systemctl stop nginx

Make a copy of your nginx.conf file for backup in case your accidentally over-write or delete your existing one:

cp /etc/nginx/nginx.conf /etc/nginx/nginx-backup.conf

Next, remove Nginx using the following command:

apt remove nginx

Install Required Packages

Some additional packages will be required to install the latest versions of the Nginx mainline or stable from the official Nginx repository. Run the following command to install or to verify if they are installed on your Debian Bullseye system:

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

Import the Ngnix Repositories

Next, a few steps will be needed to import the repository as follows successfully.

Import GPG Key

Using the curl command, import the GPG key so the APT package manager can verify the authenticity of the Nginx mainline packages:

curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
    | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

Very the GPG Key

Next, it is recommended to test and verify the download file contains the proper key. To do this, use the following command in your terminal:

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

The example output should appear:

pub   rsa2048 2011-08-19 [SC] [expires: 2024-06-14]
      573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
uid                      nginx signing key <signing-key@nginx.com>

Import Nginx Mainline Repository

To set up and import the Nginx Mainline repository, run the following command in your terminal:

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

Import Nginx Stable Repository

The same as Nginx Mainline as above, for Nginx Stable, use the following command in your terminal:

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

Pin the Nginx Repository Version to Install

Next, use the following pinning command to make Debian use Nginx’s repository over the default Debian Nginx repository:

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

Example output:

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

Install Nginx Mainline or Stable

Now that you have set up the repository, before executing the Nginx install command, you need to update your repository list to reflect the new changes as follows:

apt update

Next, run the Nginx install command, this will install the latest Nginx version of the repository you imported that is either mainline or stable:

apt install nginx -y

Note that you may be prompted to keep or replace your existing /etc/nginx/nginx.conf configuration file during the installation. It is recommended to keep your current configuration file by pressing (n). A copy will be made regardless of the maintainer’s version, and you can also check this in the future.

Verify if Nginx was installed successfully and to the latest version by running the following command:

sudo nginx -v

Example output (Nginx Mainline Example)

nginx version: nginx/1.21.1

As above, the version installed at the time of writing successfully is the latest Nginx Mainline version.

By default, Nginx should be enabled if it is not activated. use:

sudo systemctl start nginx

To enable Nginx to be started on boot, use the following command:

sudo systemctl enable nginx

Example output if successful:

Synchronizing state of nginx.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable nginx

Verify the status of Nginx:

sudo systemctl status nginx

Example output:

nginx latest version main debian 11 systemctl ok

Updating Nginx

For future updates, all you need to do is run the APT package manager as you would check your system for updates as per usual.

apt update

If an update is available, run the following command, which is the same as the start of the tutorial for Updating your Debian system:

apt upgrade

Alternative Install Option – Ondřej Surý Repository

An alternative method is to install the latest Nginx mainline or stable from the Ondřej Surý repository. Many Ubuntu users would know his PPA, and you can do the same pretty much in Debian.

To use the latest version of either Nginx mainline or stable, you will need first to import the repository.

To import mainline repository:

curl -sSL https://packages.sury.org/nginx-mainline/README.txt | sudo bash -x

To import stable repository:

curl -sSL https://packages.sury.org/nginx/README.txt | sudo bash -x

Update your repository to reflect the new change:

apt update

Now that you have installed the Nginx repository and updated the repository list, install Nginx with the following:

apt install nginx-core nginx-common nginx nginx-full

Example output:

How to Install Latest Nginx Mainline or Stable on Debian 11

Type Y, then press the enter key to proceed and complete the installation.

Note that you may be prompted to keep or replace your existing /etc/nginx/nginx.conf configuration file during the installation. It is recommended to keep your current configuration file by pressing (n). A copy will be made regardless of the maintainer’s version, and you can also check this in the future.

You will notice additional modules will be available in this version, most notably brotli support. To install brotli, follow the steps below.

Open your nginx.conf configuration file:

nano /etc/nginx/nginx.conf

Now add the additional lines before in the HTTP{} section:

        brotli on;
        brotli_comp_level 6;
        brotli_static on;
        brotli_types application/atom+xml application/javascript application/json application/rss+xml
             application/vnd.ms-fontobject application/x-font-opentype application/x-font-truetype
             application/x-font-ttf application/x-javascript application/xhtml+xml application/xml
             font/eot font/opentype font/otf font/truetype image/svg+xml image/vnd.microsoft.icon
             image/x-icon image/x-win-bitmap text/css text/javascript text/plain text/xml;

The brotli_comp_level can be set between 1 (lowest) and 11 (highest). Typically, most servers sit in the middle, but if your server is a monster, set to 11 and monitor CPU usage levels.

Next, test to make sure the changes are working correctly before making the changes live:

sudo nginx -t

If the changes are working correctly, you should see the following:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Now make the changes live by restarting your server:

sudo systemctl restart nginx

Comments and Conclusion

The tutorial has shown you how to install the Nginx repository so you can pull either Nginx stable or mainline latest versions on your Debian Bullseye system along with an alternative method from Ondřej Surý. Stability is always to use what comes officially in the Debian APT repository. However, often, these lack features since they are so outdated.

Overall, using the latest stable Nginx or Mainline versions is relatively safe compared to other software where bugs and instability could be present. Nginx does a fantastic job in keeping its web application running smoothly.



Follow LinuxCapable.com!

Like to get automatic updates? Follow us on one of our social media accounts!