How to Install/Upgrade Nginx Mainline/Stable on Rocky Linux 8
If your website has been running on the old 1.18 series and you’re ready for faster builds, or if you’ve got a fresh server that needs a rock‑solid web server from day one, this walk‑through will get you up to speed with either the mainline or stable build of Nginx on Rocky 8 without any surprises.
Step 1: Remove Any Existing Nginx Packages
sudo dnf remove nginx nginx-common
A leftover older package can clash with the new repo’s dependencies, causing a cascade of “conflict” errors. Clean slate beats patchy installs.
Step 2: Add the Official Nginx Repository
Create `/etc/yum.repos.d/nginx.repo` and paste:
[nginx-mainline] name=nginx mainline repo baseurl=https://nginx.org/packages/mainline/centos/$releasever/$basearch/ enabled=1 gpgcheck=1 gpgkey=https://nginx.org/keys/nginx_signing.key [nginx-stable] name=nginx stable repo baseurl=https://nginx.org/packages/stable/centos/$releasever/$basearch/ enabled=0 gpgcheck=1 gpgkey=https://nginx.org/keys/nginx_signing.key
Why the two blocks?
The mainline block is enabled by default because it has the latest features. The stable block is disabled so you can switch later if you decide you need a more conservative release.
Step 3: Decide Which Release You Want
- Mainline (recommended for new projects or when you want the newest fixes).
- Stable (when you’re worried about breaking changes on a production site).
To switch, just edit `nginx.repo`:
sudo sed -i 's/enabled=1/enabled=0/' /etc/yum.repos.d/nginx.repo sudo sed -i 's/enabled=0/enabled=1/' /etc/yum.repos.d/nginx.repo
That flips the flags in a single command – no need to delete and re‑create files.
Step 4: Install or Upgrade
sudo dnf clean expire-cache sudo dnf install nginx
If you already had Nginx, this will pull down the latest package from whichever repo is enabled. It also automatically resolves all dependencies for you.
Why `dnf clean`?
It forces DNF to re‑read the metadata so it knows about the new packages in your freshly added repo.
Step 5: Verify the Version
nginx -v
You should see something like `nginx version: nginx/1.25.x`. If you had switched from stable to mainline, the number will jump up a few major/minor releases.
Step 6: Start and Enable Nginx
sudo systemctl enable --now nginx
If you’re on Rocky 8’s firewall, open HTTP/HTTPS:
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload
Real‑World Observation: “The Config Wasn’t Working After the Upgrade”
I once upgraded a staging server from 1.18 to 1.21 and was surprised when `gzip on;` stopped compressing anything. The culprit turned out to be a missing module in the new build—`ngx_http_gzip_module`. The fix? Re‑enable it in `nginx.conf` or compile a custom package with that module. Lesson: always double‑check your modules after an upgrade, especially if you rely on optional ones.
Step 7: Keep It Updated
sudo dnf update nginx
Run this regularly; the mainline repo will push security patches faster than the stable line.
That’s all there is to it. Pick your branch, flip a flag, install, and you’re ready to serve traffic with the latest Nginx on Rocky 8.