For those using Ubuntu 22.04 LTS, you might have noticed that installing Nginx directly from its repository does not install the latest stable or mainline version. This is a common trend in most distributions that focus on the stability of packages and provide only urgent bug or security updates until the subsequent major distribution.
For most, using the default Nginx that comes bundled with the repository will be preferred, but often many require and want the latest version of stable or mainline for updated features.
The following tutorial will cover installing the last stable or mainline versions of Nginx on Ubuntu 22.04 Jammy Jellyfish desktop or server utilizing the APT package manager with the PPA model by Ondřej Surý or by importing the official Nginx.org APT repository and installing the latest version directly from Nginx.
Table of Contents
Update Ubuntu
Before you begin, update your system to ensure all packages are up-to-date to avoid conflicts.
sudo apt update && sudo apt upgrade -y
Remove Previous Nginx Installation
First, you need to remove any previous Nginx installations before installing the new Nginx versions to avoid conflict.
First, back up your Nginx configuration for safekeeping.
sudo mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.old
Stop Nginx using the systemctl command as follows.
sudo systemctl stop nginx
Next, remove Nginx using the following command.
sudo apt-get autoremove nginx*
Install Nginx Mainline – APT Method with Nginx.org Repository
The first method is to install and update using Nginx’s repository, which will give you at all times the latest version available. This is the best method of installing Nginx over all other techniques for those wanting the latest versions.
First, open your terminal (CTRL+ALT+T) and install the dependencies required for importing and installing Nginx using the official repository.
sudo apt install wget gnupg2 ca-certificates lsb-release ubuntu-keyring software-properties-common -y
Download and add the Nginx GPG key to verify the authenticity of the packages.
wget -O- https://nginx.org/keys/nginx_signing.key | sudo gpg --dearmor | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg
Next, use the following to add either stable or mainline Nginx repository into 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/ubuntu `lsb_release -cs` nginx | sudo tee /etc/apt/sources.list.d/nginx-mainline.list
Alternatively, you can instead of importing the mainline repository instead import the latest available Nginx stable repository. This may be more preferred for your environment and will be ahead of the version Ubuntu has in its repository.
Import Nginx Stable Repository
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-stable.list
Ideally, you should set APT pinning to prefer Nginx packages over any other default Ubuntu repositories or PPAs.
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
Once done, update the apt repositories to reflect the new additions.
sudo apt update
Now proceed to install Nginx.
sudo apt install nginx
At this point, you have installed the latest version of Nginx from its official repository.
Install Nginx Mainline – APT Method with PPA
An alternative method for users who prefer a more straightforward approach is installing Nginx mainline by Ondřej Surý, who many would know as the leading PHP developer and maintainer for Debian repositories. This PPA is safe and is usually maintained within hours to a few days after each update goes out by Nginx.
The PPA supports both stable and mainline however they are separate PPAs.
Import the Mainline PPA
sudo add-apt-repository ppa:ondrej/nginx-mainline -y
Alternative Method: Import the Stable PPA
For users who may want to keep the stable version and not the mainline version but have the latest stable version available instead of what Ubuntu has in its repository, there is a PPA by the same person who contains this version.
sudo add-apt-repository ppa:ondrej/nginx -y
Only import one repository, do not import both.
Next, update the APT cache.
sudo apt update
Once the chosen repository is added, proceed to install Nginx mainline in full by using the following command:
sudo apt install nginx-core nginx-common nginx nginx-full -y
Lastly, confirm which version is being used and its source to ensure the mainline is installed from the PPA using the apt-cache policy command.
apt-cache policy nginx
Example output:
And that is it! You have installed the latest version of Nginx using the PPA by Ondřej Surý.
Additionally, the PPA has several benefits of extra modules included. One of the main advantages is having the ability to add Brotli support. To install brotli, follow the steps below.
Open your nginx.conf configuration file.
sudo 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). Most servers sit in the middle, but put it to 11 and monitor CPU usage levels if your server is a monster.
Next, test to make sure the changes are working correctly before making it 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 or PPA so you can pull either Nginx stable or mainline latest versions on your Ubuntu 22.04 LTS Jammy Jellyfish desktop or server.
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.