For those using Rocky Linux, 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 bugs or security updates until the subsequent major distribution.
The following tutorial will cover installing the last stable or mainline versions of Nginx on Rocky Linux 9 or Rocky Linux 8 using the official repository from Nginx.org with the command line terminal. The tutorial will cover importing both versions and enabling or disabling the repositories depending on your preference.
Table of Contents
Update Rocky Linux – System Packages Upgrade
Before you begin, update your system to ensure all packages are up-to-date to avoid conflicts.
sudo dnf upgrade --refresh
Remove Previous Nginx Installation
First, remove any previous Nginx installations before installing the new 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 dnf autoremove nginx*
Import Nginx.org Repositories
The task is to import the Nginx repository, which will always give you the latest version. This is the best method of installing Nginx over all other techniques for those wanting the latest versions.
Use the following command to import the Nginx mainline and stable. By default, the stable will be enabled, but later on, the tutorial will show you how to enable the mainline over the stable version.
Do not forget to copy the correct repository for your version of Rocky Linux.
Import Nginx Mainline Repository for EL9
sudo tee /etc/yum.repos.d/nginx-mainline.repo<<EOF
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/9/x86_64/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
Users with aarch architecture, replace in the above command baseurl=http://nginx.org/packages/mainline/centos/9/x86_64/ with baseurl=http://nginx.org/packages/mainline/centos/9/aarch64/.
Import Nginx Stable Repository EL9
sudo tee /etc/yum.repos.d/nginx-stable.repo<<EOF
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/9/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
Users with aarch architecture, replace in the above command baseurl=http://nginx.org/packages/mainline/centos/9/x86_64/ with baseurl=http://nginx.org/packages/mainline/centos/9/aarch64/.
Import Nginx Mainline Repository for EL8
sudo tee /etc/yum.repos.d/nginx-mainline.repo<<EOF
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/8/x86_64/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
Users with aarch architecture, replace in the above command baseurl=http://nginx.org/packages/mainline/centos/8/x86_64/ with baseurl=http://nginx.org/packages/mainline/centos/8/aarch64/.
Import Nginx Stable Repository EL8
sudo tee /etc/yum.repos.d/nginx-stable.repo<<EOF
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/8/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
Users with aarch architecture, replace in the above command baseurl=http://nginx.org/packages/mainline/centos/8/x86_64/ with baseurl=http://nginx.org/packages/mainline/centos/8/aarch64/.
Install Nginx
By default, the stable version is enabled. The first task is to enable the repository you imported. First, you need to install the DNF-utils package.
sudo dnf install dnf-utils -y
Next, the tutorial will enable the mainline version; users that want to install the stable version do not need to use the following command.
sudo yum-config-manager --enable nginx-mainline
Now, install the Nginx mainline.
sudo dnf install nginx
If you want to revert to stable from the mainline, remove Nginx, disable the mainline branch using the following command, and re-install Nginx, which will install the latest stable version from Nginx.org.
sudo yum-config-manager --disable nginx-mainline
Configure FirewallD for Nginx
If you are not replacing an existing Nginx service and installing Nginx for the first time, you may need to configure the firewall for HTTP and HTTPS traffic. An example of how to do this is below:
Allow HTTP traffic to use the following command:
sudo firewall-cmd --permanent --zone=public --add-service=http
Allow HTTPS traffic using the following command.
sudo firewall-cmd --permanent --zone=public --add-service=https
Once done, you need to make the changes effective by reloading the firewall:
sudo firewall-cmd --reload
Conclusion
At this point, you have installed the latest version of Nginx from its official repository with the tutorial installing the mainline version. Still, as the tutorial has demonstrated, you can keep the stable version of Nginx but this time, grab it directly from Nginx.org so you will always have the latest up-to-date version no matter what branch you decide.