For those using CentOS 8 Stream, you might have noticed that installing Nginx directly from its Appresteam does not install the latest stable or mainline version. It is pretty far behind where Nginx is stable, and Mainline is at the current time of its development.
For most, using the default Nginx that comes bundled with the CentOS App stream will be preferred. Still, the following tutorial will cover the steps needed to use newer versions for the latest features.
Table of Contents
- Recommended OS: Any current CentOS 8 Stream.
- User account: root access
Updating Operating System
Update your CentOS Stream operating system to make sure all existing packages are up to date:
sudo dnf upgrade --refresh -y
The tutorial will be using the sudo command and assuming you have sudo status.
To verify sudo status on your account:
Example output showing sudo status:
[joshua@centos-stream ~]$ sudo whoami root
To set up an existing or new sudo account, visit our tutorial on adding a User to Sudoers on CentOS Stream.
To use the root account, use the following command with the root password to log in.
The tutorial will utilize the terminal, which can be found in your show applications menu.
Situational. Remove Previous Nginx Installation
First, you need to remove any previous Nginx installations before installing Nginx Mainline.
Stop Nginx using the systemctl command as follows:
sudo systemctl stop nginx
Next, remove Nginx using the following command:
sudo dnf remove nginx
Create & Install Ngnix Repository
Now that you have successfully removed the old Nginx version, if you had it installed, to install Nginx mainline, you need to install the dependency for it first, which is dnf-utilities with the following command:
sudo dnf install dnf-utils -y
Once installed, use your favorite text editor, create the following file:
sudo nano /etc/yum.repos.d/nginx.repo
Next, you need to add the following code, which specifies the Nginx repository which we will use to install the latest Nginx mainline version:
[nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true
To save use (CTRL+O), then exit (CTRL+X).
Note, the latest stable directory repository is also added for those preferring the alternative option.
Install Latest Nginx Mainline
By default, the latest repository for stable Nginx packages is used first. However, you are going to install Nginx mainline, so you will need to run the following command to enable the mainline repository as follows:
sudo dnf config-manager --enable nginx-mainline
Next, install Nginx mainline as follows:
sudo dnf install nginx
Notice the version number that is being installed as below:
Type Y, then press the ENTER KEY to proceed with the installation.
Note that you will see a pop-up that notifies you about importing the GPG key during the installation.
Type Y, then press the ENTER KEY to complete the installation.
To verify the Nginx mainline version, use the following command to confirm:
nginx version: nginx/1.21.4
By default, Nginx does not come enabled and is deactivated on installation. To activate your Nginx service, use:
sudo systemctl start nginx
To enable Nginx to be started on boot, use the following command:
sudo systemctl enable nginx
Lastly, check the status to verify that Nginx is working correctly:
systemctl status nginx
Congratulations, you have successfully installed the latest Nginx Mainline build on your server.
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:
To allow HTTP traffic use the following command:
sudo firewall-cmd --permanent --zone=public --add-service=http
To allow HTTPS traffic use 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
Comments and Conclusion
The tutorial has shown you how to install the latest Nginx Mainline build on your CentOS 8 Stream desktop or server. Overall, stability is always to use what comes officially in the App stream. However, often, these lack features since they are so outdated.
A note to make that 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.