How to install the latest nginx on Debian and Ubuntu

Nginx is one of the most popular web servers in the world and is responsible for hosting the largest and busiest sites on the Internet. In most cases, it is more resource friendly than Apache and can be used as a web server or reverse proxy.

This tutorial will show you how to install the latest version of Nginx on Debian and Ubuntu.

Install latest version of Nginx on Debian #

Update the apt package index:

sudo apt update && sudo apt upgrade -y

Incase you have Nginx installed, you should remove it before installing the latest version.

sudo apt remove nginx -y

Install the prerequisites:

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

Import an official nginx signing key so apt could verify the packages authenticity. Fetch the key:

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

Make sure the downloaded file contains the correct key.

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

The output should include the full fingerprint.

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

If the fingerprint is different, remove the file.

To set up an apt repository for a stable nginx package, run the following command:

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

Set up repository pinning to prioritize our packages over the packages provided by the distribution.

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

You may see the following output:

Package: *
Pin: origin nginx.org
Pin: release o=nginx
Pin-Priority: 900

To install nginx, run the following commands:

sudo apt update && sudo apt install nginx -y

Install latest version of Nginx on Ubuntu #

Install the prerequisites:

sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring

Import the official nginx signing key so that apt can verify the authenticity of the package. Get the key.

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

Verify that the downloaded file contains the proper key:

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

Import Nginx Stable Repository

echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list

Just like for Debian, pin the repository to the latest version.

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

To install nginx on Ubuntu, run the following commands:

sudo apt update
sudo apt install nginx

To verify that the installation was successful, run the following command:

sudo nginx -v

The output should be something like:

nginx version: nginx/1.20.1

Tags:

Linux

Nginx