How to Install Nginx Mainline on Ubuntu 22.04 or 20.04

To ensure optimal performance and security on your Ubuntu system, you might consider installing the Nginx mainline version. This guide will focus on how to install Nginx Mainline on Ubuntu 22.04 Jammy Jellyfish or Ubuntu 20.04 Focal Fossa LTS releases, providing you with the latest features and updates.

Understanding Nginx Versions:

  • Stable vs. Mainline: The stable version of Nginx has undergone extensive testing, ensuring reliability for critical applications. On the other hand, the mainline version includes the latest features and updates, though it might not have undergone the same level of testing as the stable version.
  • Performance and Security: Opting for the mainline version can enhance your system’s performance and security, as it includes the most recent advancements and fixes.
  • Choosing the Right Version: Choosing between the stable and mainline versions should align with your needs. If you prioritize having the newest features and are prepared to handle potential issues that might arise, the mainline version is the way to go. However, if stability is your top priority, you might opt for the stable version.

This guide is tailored to help you navigate the installation process, ensuring you can take full advantage of what Nginx Mainline offers on your Ubuntu system.

Preparing Ubuntu 22.04 or 20.04 for Nginx Mainline Installation

Step 1: Updating Your System Before Nginx Mainline Installation

Before diving into the main 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

Step 2: Ensuring Necessary Packages are Installed for Nginx Mainline on Ubuntu

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

Step 3: Clearing Prior Nginx Installations from Ubuntu (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*

Install Nginx Mainline on Ubuntu 22.04 or 20.04 via Nginx.org

This guide will walk you through the installation of the Nginx mainline on Ubuntu. Although our primary focus is on the mainline version, we’ll also cover the steps for the stable version. 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.

Step 1: Adding the Nginx.org GPG Key to Ubuntu

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>

Step 2: Integrating the Nginx Mainline APT Repository into Ubuntu

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

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

Step 3: 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

Step 4: Proceed to Install Nginx Mainline on Ubuntu 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

Switching Between Nginx.org and PPA Nginx Mainline Versions on Ubuntu

Now that you have Nginx Mainline installed on your Ubuntu system using the Nginx.org method, you might contemplate using the PPA method. If so, it’s essential to ensure a clean transition by removing the existing source list and uninstalling the Nginx version you have.

To eliminate the Nginx.org APT source list, execute the following command:

sudo rm /etc/apt/sources.list.d/nginx.list

Remember, always consider the compatibility and features of different versions and sources when deciding to switch. Ensuring a clean transition between versions minimizes potential conflicts and maintains system stability.

Install Nginx Mainline on Ubuntu 22.04 or 20.04 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.

Step 1: Add Nginx Mainline PPA on Ubuntu

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

Step 2: Install Nginx Mainline on Ubuntu via APT Command

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

sudo apt install nginx

Step 3: 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.

Step 4: 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

In this guide, we delved into the detailed process of installing Nginx Mainline on Ubuntu 22.04 or 20.04 using two reputable sources: the official Nginx.org APT repository and the widely-recognized Ondřej Surý LaunchPAD PPA. Each method offers unique advantages, ensuring users have flexibility based on their needs. By following the outlined steps, you can confidently set up and manage Nginx on your Ubuntu system, harnessing the power and efficiency of this robust web server. Whether you’re a seasoned administrator or new to Ubuntu, this guide provides a clear path to achieve a successful Nginx installation.

Leave a Comment


Your Mastodon Instance
Share to...