For those using AlmaLinux 8, 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 AlmaLinux 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
Prerequisites
- Recommended OS: AlmaLinux 8.
- User account: A user account with sudo privilages or root access (su command).
Updating Operating System
Update your AlmaLinux 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:
sudo whoami
Example output showing sudo status:
[joshua@localhost ~]$ sudo whoami
root
To set up an existing or new sudo account, visit our tutorial on How to Add a User to Sudoers on AlmaLinux.
To use the root account, use the following command with the root password to log in.
su
Remove Previous Nginx Installation
First, you need to remove any previous Nginx installations that are active 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).
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 yum-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 proceed with the installation.
nginx -v
To verify the Nginx mainline version, use the following command to confirm:
Example output:
nginx version: nginx/1.21.3
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:
sudo systemctl status nginx
Example output:
Congratulations, you have successfully installed the latest Nginx Mainline build on your server.
Configure Firewall
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 AlmaLinux 8 server. Stability is always to use what comes officially in the App stream. However, often, these lack features since they are so outdated.
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.