NGINX is the fastest and most prominent platform for powering web servers. According to W3Tech’s May 2021 records, NGINX powers 34% of all known websites compared to Apache2’s 33.5%. Now, this may seem close, but consider this for anyone new to the web servers. To cut the story short, Apache2 has been slowly dropping over time. Even Google Trends shows this in search terms a little clearer compared to W3Tech’s stats chart like the example below:
So, you may be interested in installing and deploying Nginx to power your web server! However, you may have noticed that NGINX comes in 2 versions, stable and mainline. Most repositories that include NGINX, such as Ubuntu, always comprise NGINX stable repository. However, do not be fooled NGINX mainline can be, in this case, be better to keep up to date than NGINX stable.
Generally, for most other repositories on Ubuntu, this is not advised since its new software contains bugs and potential security flaws. Still, for NGINX Mainline, I recommend it as they usually deploy bug fixes, better features, plus overall improvements than with stable. NGINX stable can be more buggy/prone to issues compared to mainline. Then you have the problem of waiting so long between stable releases.
Even NGINX recommends Mainline over stable!
We recommend that in general you deploy the NGINX mainline branch at all times. The main reason to use the stable branch is that you are concerned about possible impacts of new features, such as incompatibility with third party modules or the inadvertent introduction of bugs in new features.
Now, how do we use Mainline NGINX over Stable when it comes to Ubuntu 20.04? This is a simple process in our guide outlined below.
Back-Up Existing Installation and Remove NGINX
Make sure you back up your NGINX folder. Also, many make a critical mistake not to back up your /etc/nginx/nginx.conf file for later re-use or reference. Follow the commands below:
sudo mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.old
Uninstall existing NGINX installation by using the following command:
sudo apt-get remove nginx nginx-common nginx-full nginx-core
NGINX Mainline GPG Key and Repository
Download NGINX packages and signing key, then we will add the key to the apt program’s keyring:
sudo wget http://nginx.org/keys/nginx_signing.key && sudo apt-key add nginx_signing.key
Once you added the key signing key to your apt program’s keyring, you need to modify the /etc/apt/sources.list file to add NGINX mainline to it so that we can update it in the future, we will use nano editor for this, but any text editor will do:
sudo nano /etc/apt/sources.list
Modify the file by adding these lines sources.list, replace code-name with your version of Ubuntu, which will be FOCAL for Nginx 20.04, so you can receive updates from the apt package manager.
deb http://nginx.org/packages/mainline/ubuntu/ codename nginx
deb-src http://nginx.org/packages/mainline/ubuntu/ codename nginx
Example of it done for focal
deb http://nginx.org/packages/mainline/ubuntu/ focal nginx
deb-src http://nginx.org/packages/mainline/ubuntu/ focal nginx
Install NGINX Mainline Source
Now we are ready to install NGINX mainline from our newly added NGINX Open-Source repository with the below command:
sudo apt-get update && sudo apt-get install nginx -y
If you installed NGINX Mainline with no errors, check to see what version you are on. For this version, we are installing NGINX 1.19.10:
nginx version: nginx/1.9.10
Install NGINX Mainline (PPA by Ondřej Surý)
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 custom PPA is safe and is usually maintained within a few weeks after each update goes out by Nginx.
For the quickest updates to Nginx mainline, compiling yourself and not relying on any other maintainer is always the fastest and can often be the most fun.
The add the PPA for Nginx Mainline execute the following command:
sudo add-apt-repository ppa:ondrej/nginx-mainline && sudo apt update -y
Once the 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
And that is it! Congratulations, you have to install the Nginx mainline either by compiling or using the PPA.
The guide assumes you know how to install NGINX stable already, so we will not touch on the whole setup. Once you have installed NGINX Mainline, you will need to go back and configure your server, this is why we made a copy of our old /etc/nginx/nginx.conf, or you will be starting from scratch again, which can be frustrating.
NGINX is pretty good at releasing mainline version updates. Often you can see updates from once a week to a few weeks at most and usually very fast if a critical bug or CVE is discovered. As I mentioned at the start of this article, I believe using Mainline can sometimes be better for a more “stable” version with better security than stable. Still, you can argue both sides of this statement. It’s really up to you and how you think if it will benefit you or not.
My advice will always be to research before making any big commitments and full back-ups. If any significant change like NGINX Mainline does not work out, you can quickly revert to your original setup.