How to Install or Upgrade Nginx (Mainline or Stable) on AlmaLinux 9
Want a fresh copy of Nginx on AlmaLinux 9, or you’re stuck with an old version? This guide walks through adding the official Nginx repo, installing either the mainline or stable release, and making sure it boots right after every reboot. No fluff—just the steps that actually work.
Why the Official Repo Matters
AlmaLinux 9 is a downstream RHEL clone, so its default repos don’t ship Nginx at all. Using the official Nginx repo guarantees you get the latest security patches and the correct binaries for your kernel version. I’ve seen people try to pull from an outdated CentOS‑8 repo and end up with broken libraries—don’t let that be you.
Step 1: Add the Nginx Repository
sudo dnf install -y wget wget https://nginx.org/packages/centos/RPM-GPG-KEY-nginx sudo rpm --import RPM-GPG-KEY-nginx cat <<EOF | sudo tee /etc/yum.repos.d/nginx.repo [nginx] name=nginx repo baseurl=https://nginx.org/packages/mainline/centos/\$releasever/\$basearch/ gpgcheck=1 enabled=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-nginx [nginx-stable] name=nginx stable repo baseurl=https://nginx.org/packages/stable/centos/\$releasever/\$basearch/ gpgcheck=1 enabled=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-nginx EOF
Why this matters? Importing the GPG key prevents a “package not trusted” error, and creating two repo blocks lets you toggle between mainline (default) and stable at any time.
Step 2: Clean Up Metadata
sudo dnf clean all sudo dnf makecache
A stale cache can point you to an old package version. Refreshing it ensures the next install pulls from the latest repo data.
Step 3: Install or Upgrade Nginx
For a fresh install of the mainline build:
sudo dnf install -y nginx
If you need the stable release instead, first enable that section in the repo file (change `enabled=0` to `enabled=1`) and then run:
sudo dnf install -y nginx
On an existing installation, just upgrade:
sudo dnf update -y nginx
The `-y` flag keeps you from having to confirm each prompt—useful for scripts or quick deployments.
Step 4: Enable and Start the Service
sudo systemctl enable --now nginx
Enabling it guarantees Nginx starts on boot, while `--now` kicks it into action immediately. You’ll notice a tiny “started” log line in `journalctl -u nginx`.
Step 5: Verify the Installation
nginx -v # or for more verbose info: ps aux | grep nginx
You should see something like `nginx version: nginx/1.25.x` (for mainline) or a slightly older stable tag.
Step 6: Open the Firewall (Optional)
If you’re running a firewall, expose port 80 and 443:
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload
Without this, external requests will hit a blocked port, which is a common beginner mistake.
Common Pitfall: The Wrong BaseURL
I’ve seen users copy the repo file from an older CentOS‑8 setup. The `$releasever` variable then expands to “8” instead of “9,” leading to 404 errors during install. Double‑check that the URL in your `.repo` file contains `centos/9`. If it doesn’t, correct it or delete the file and recreate.
Optional: Switching Between Mainline and Stable
If you need to switch later, just toggle the `enabled` flag in `/etc/yum.repos.d/nginx.repo`, clean metadata again, then run a normal install. It’s that simple—no manual binary downloads required.
That’s all the steps needed to get Nginx up and running on AlmaLinux 9, whether you’re chasing the bleeding‑edge mainline build or prefer the tried‑and‑true stable release.