How to Install Nginx Mainline on Ubuntu 24.04, 22.04, or 20.04

Nginx Mainline is the cutting-edge version of the popular web server, offering the latest features, improvements, and bug fixes ahead of the stable release. It is particularly favored by developers and administrators who want to stay at the forefront of web server technology, benefiting from the most recent advancements in performance and security. Nginx Mainline is ideal for those who need to experiment with new features or deploy the latest technologies in their production environments.

On Ubuntu 24.04, 22.04, or 20.04, you can install Nginx Mainline via the official nginx.org apt repository mirror or through Ondřej Surý’s LaunchPAD PPA, which has a long-standing reputation for maintaining the latest PHP and Nginx builds. Both methods provide reliable access to the most recent versions of Nginx Mainline, ensuring that your web server is up-to-date and fully optimized. This guide will walk you through the steps to install Nginx Mainline using these repositories, giving you the flexibility to choose the source that best suits your needs.

Preparing for Nginx Mainline Installation

Updating Your System Before Nginx Mainline Installation

Before diving into the installation process, ensuring your Ubuntu system is current with all its packages is crucial. Start by updating the system’s package list:

sudo apt update

Then, upgrade any outdated packages to their latest versions:

sudo apt upgrade

Ensuring Necessary Packages are Installed for the Nginx Mainline

While the following packages might already be present on your Ubuntu setup, it’s a best practice to verify their installation. These packages ensure a smooth installation process for Nginx Mainline:

sudo apt install curl gnupg2 ca-certificates lsb-release dirmngr software-properties-common apt-transport-https -y

Clearing Prior Nginx Installations (If Applicable)

If you’ve previously installed Nginx on your system, it’s wise to remove it to prevent any potential conflicts with the new installation. But first, safeguard your existing Nginx configurations by backing them up:

sudo mv /etc/nginx/ /etc/nginx.old/

To halt any running Nginx processes, use the systemd command:

sudo systemctl stop nginx

Lastly, purge any old Nginx packages from your system:

sudo apt autoremove nginx*

Method 1: Install Nginx Mainline via Nginx.org

The advantage of using Nginx.org over Ubuntu’s default repositories or other PPAs is that you receive the most recent version of Nginx without the waiting period associated with third-party compilations.

Adding the Nginx.org GPG Key

To ensure the integrity of the packages you’ll be installing, start by downloading and adding the Nginx GPG key:

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

To confirm the successful addition of the GPG key, execute:

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

If everything is in order, you should see an output resembling:

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

Integrating the Nginx Mainline APT Repository

For the mainline version, incorporate its repository using:

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

Note: If you prefer the stable version, which typically is more current than what’s offered by Ubuntu’s default package, run the following:

echo "deb [arch=amd64,arm64 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

Configuring APT Pinning for Nginx

To prioritize Nginx packages from Nginx.org over default Ubuntu packages or other PPAs, set up APT pinning:

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

You should see the following configuration as 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

Proceed to Install Nginx Mainline via APT Command

After setting up the repositories, update your package list:

sudo apt update

With everything in place, you can now install Nginx mainline:

sudo apt install nginx

Method 2: Install Nginx Mainline via PPA

For those seeking an alternative to the direct Nginx.org APT repository, Ondřej Surý’s PPA offers a reliable method. A respected figure in the Ubuntu community, Ondřej Surý is known for maintaining Apache, Nginx, and PHP repositories. While updates via this method might not be as swift, this PPA version offers added compiled modules, notably Brotli.

Add Nginx Mainline PPA

To start, you must add the Nginx Mainline repository from the PPA. Execute the following command:

sudo add-apt-repository ppa:ondrej/nginx-mainline -y

If, for any reason, you’d like to revert to the latest stable version or prefer it over the mainline, run the command below. Just ensure you’ve removed the mainline version first to prevent any conflicts.

sudo add-apt-repository ppa:ondrej/nginx -y

Just ensure you’ve removed the mainline version first to prevent any conflicts:

sudo add-apt-repository --remove ppa:ondrej/nginx-mainline -y

Install Nginx Mainline via APT Command

Once the PPA is set up, proceed to install either the Nginx mainline or stable with:

sudo apt install nginx

Verifying Your Nginx Mainline PPA Installation

After the installation, checking if everything went smoothly is always good practice. Use this command to confirm your Nginx version:

nginx -v

If the displayed version aligns with the latest mainline, congratulations! Your installation was successful.

Enhancing Your Nginx Setup with Brotli Compression (Optional)

Both the mainline and stable versions of Nginx from this PPA come with the Brotli module. To utilize this module, first, install it:

sudo apt install libnginx-mod-brotli

This command is specific to the PPA repositories. While the Nginx.org APT repository offers the benefit of being the official source with the latest version, it doesn’t include pre-built modules. On the other hand, a notable advantage of the PPA is its inclusion of these pre-built modules.

Next, access your Nginx configuration file:

sudo nano /etc/nginx/nginx.conf

Incorporate the following configurations within the HTTP block of your nginx.conf:

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;

Let’s break down what each configuration does:

  • brotli on;: Activates Brotli compression.
  • brotli_comp_level 6;: Adjusts the compression level to 6 (ranging between 0-11). Opting for a higher value gives better compression but demands more CPU.
  • brotli_static on;: Instructs Nginx to search for pre-compressed .br files. If found, these are served, bypassing on-the-fly compression.
  • brotli_types …;: Lists the MIME types for responses that should undergo compression. This array comprises various text formats, fonts, and images optimal for Brotli compression.

Before applying these changes, validate the configuration to prevent potential errors:

sudo nginx -t

To test Brotli compression in action, use:

curl -I --compressed http://your-server.com/some-path

In the output, spotting the Content-Encoding: br header confirms Brotli compression is active.

Conclusion

By installing Nginx Mainline on Ubuntu through either the nginx.org apt repository or Ondřej Surý’s LaunchPAD PPA, you have equipped your server with the latest and most advanced version of Nginx. This setup ensures that you can take advantage of the newest features and improvements, making your web server both robust and future-proof. Regularly updating your installation will keep your server secure and performing at its best. Whether you opt for the official repository or the PPA, maintaining this cutting-edge version of Nginx will allow you to stay ahead in the ever-evolving landscape of web technology.

1 thought on “How to Install Nginx Mainline on Ubuntu 24.04, 22.04, or 20.04”

  1. Thanks so much for this guide. It was excellent! I used this to upgrade from 1.24.0 on Ubuntu 24.04 to 1.27.1.

    I chose to
    cp -rp /etc/nginx /etc/nginx.bak
    rather than
    mv /etc/nginx /etc/nginx.old
    I wasn’t sure if a running Nginx service would have an issue moving the directory.

    When I ran
    apt autoremove nginx*
    I got
    E: Unable to locate package nginx.bak
    E: Couldn’t find any package by glob ‘nginx.bak’

    That may have been because I ran it from the /etc directory, and it picked up the nginx.bak directory I created there. So, instead, I ran
    apt autoremove nginx nginx-common nginx-doc
    which were all of the packages I had installed.

    There may need to be a note at the end stating that if you upgraded from a prior install and you moved the /etc/nginx directory to /etc/nginx.old, you must selectively copy some or all of the files from /etc/nginx.old into the newly created/etc/nginx directory to restore the previous website functionality. Some changes may also be needed if the nginx configuration changes between releases.

    If you choose to copy rather than move the /etc/nginx directory, there will be a prompt during installation:
    Configuration file ‘/etc/nginx/nginx.conf’
    ==> Modified (by you or by a script) since installation.
    ==> Package distributor has shipped an updated version.
    What would you like to do about it ? Your options are:
    Y or I : install the package maintainer’s version
    N or O : keep your currently-installed version
    D : show the differences between the versions
    Z : start a shell to examine the situation
    The default action is to keep your current version.
    *** nginx.conf (Y/I/N/O/D/Z) [default=N] ? n

    I could not install Brotli Compression. The package libnginx-mod-brotli seems to exist in nginx 1.25.1 but not in 1.27.1. I found a suggestion that it is now in libnginx-mod-http-brotli-filter and libnginx-mod-http-brotli-static, but when I try to install them, they have unmet dependencies.

    Reply

Leave a Comment