Installing Nginx on Rocky Linux 9

How to install Nginx on Rocky Linux 9, understand its directory structure, and test the config correctly — including why nginx -t is the most important habit you'll build.

Quick answer

How do I install Nginx on Rocky Linux 9?

Run: sudo dnf install nginx -y. Then enable and start it with: sudo systemctl enable --now nginx. Verify it's running with systemctl status nginx, and open port 80 in firewalld if you haven't already. Visit your server IP in a browser — you should see the Nginx welcome page.

Terminal showing nginx -t output with 'syntax is ok' and 'test is successful' in green

The install itself is one command. The part that takes time is understanding what you’re looking at after it’s installed — the directory structure, the config format, how to test changes safely. That’s what this article covers.


Step 1 — Install Nginx

sudo dnf install nginx -y

Step 2 — Enable and Start Nginx

sudo systemctl enable --now nginx

enable makes Nginx start automatically at boot. --now starts it immediately. One command instead of two.

Verify it’s running:

sudo systemctl status nginx

You should see active (running) in green. If you see failed or inactive, check the error in the status output and look at the error log:

sudo tail -20 /var/log/nginx/error.log

Step 3 — Confirm It’s Accessible

If port 80 isn’t open in firewalld yet, open it now:

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload

Open a browser and navigate to your server’s IP address:

http://your-server-ip

You should see the Nginx welcome page — a simple HTML page saying “Welcome to nginx!”. This confirms Nginx is installed, running, and accessible from the internet.

Browser showing the Nginx welcome page — 'Welcome to nginx!' with the default Rocky Linux styling
The Nginx welcome page. Seeing this means Nginx is installed, running, and port 80 is open. The actual welcome page content varies slightly by OS.

Understanding the Nginx Directory Structure

On Rocky Linux, Nginx uses a different file layout than Ubuntu. Understanding it prevents a lot of confusion when following tutorials written for other systems.

/etc/nginx/
├── nginx.conf          ← Main config — rarely edited directly
├── conf.d/             ← Site configs go here
│   └── default.conf    ← Default site (can delete or replace)
└── mime.types          ← File type mappings

/var/log/nginx/
├── access.log          ← All requests
└── error.log           ← Errors and warnings

/usr/share/nginx/html/  ← Default web root (the welcome page)

The key difference from Ubuntu:

Rocky LinuxUbuntu
/etc/nginx/conf.d/*.conf/etc/nginx/sites-available/
Files auto-loadedRequires symlink to sites-enabled/
No symlink step neededExtra step per site

Rocky Linux’s approach is simpler. Any .conf file you create in /etc/nginx/conf.d/ is automatically included when Nginx starts or reloads. No symlinks, no enable/disable commands.


The Most Important Nginx Habit — nginx -t

Before every single Nginx reload, run this:

sudo nginx -t

If the config is valid:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If there’s an error:

nginx: [emerg] unknown directive "servername" in /etc/nginx/conf.d/site.conf:3
nginx: configuration file /etc/nginx/nginx.conf test failed

The error output tells you the file and line number. Fix it, run nginx -t again, repeat until you see “test is successful.”

Only then reload:

sudo systemctl reload nginx

Why this habit matters: Nginx won’t reload a broken config — it keeps the old working config in memory and reports the error. But if you restart instead of reload, and the config has an error, Nginx fails to start entirely and your site goes down. nginx -t + reload is always safer than restart.


The nginx -t Moment

When you’re starting out with Nginx, config errors happen constantly. A missing semicolon. A typo in a directive name. A wrong file path. The error messages are accurate but require context to interpret.

The experience before knowing about nginx -t:

Make a change → reload → site breaks → no idea why → check error log → decipher the message → fix → reload → repeat.

The experience after:

Make a change → nginx -t → error visible immediately → fix → nginx -t → “test is successful” → reload confidently.

The second version is faster and less stressful. Run nginx -t every time. It takes one second and has prevented more headaches than any other single habit in server management.


Useful Nginx Commands Reference

# Test config for errors
sudo nginx -t

# Reload config with zero downtime (use this)
sudo systemctl reload nginx

# Full restart — drops connections briefly
sudo systemctl restart nginx

# Check running status
sudo systemctl status nginx

# Start / stop
sudo systemctl start nginx
sudo systemctl stop nginx

# View error log live
sudo tail -f /var/log/nginx/error.log

# View access log live
sudo tail -f /var/log/nginx/access.log

# Check which config files are loaded
sudo nginx -T | grep "configuration file"

Troubleshooting Common Issues

Port 80 not accessible from browser:

sudo firewall-cmd --list-services
# Should include 'http'
# If not: sudo firewall-cmd --permanent --add-service=http && sudo firewall-cmd --reload

Nginx fails to start:

sudo systemctl status nginx
sudo tail -20 /var/log/nginx/error.log
# Read the error — usually a config syntax issue or port already in use

403 Forbidden on the welcome page:

ls -la /usr/share/nginx/html/
# Check that index.html exists and is readable
# SELinux on Rocky Linux can cause this — check: sudo ausearch -m avc -ts recent

The next article installs MariaDB. Leave Nginx running — it will stay up while you install the database layer.

Frequently Asked Questions

What is nginx -t and why does it matter?
nginx -t tests the Nginx configuration files for syntax errors without restarting the server. Running it before every reload is the habit that prevents config mistakes from taking down your site. If it shows 'syntax is ok' and 'test is successful', it's safe to reload.
What's the difference between nginx reload and nginx restart?
Reload (systemctl reload nginx) applies new configuration with zero downtime — existing connections continue while new ones use the updated config. Restart (systemctl restart nginx) stops and restarts the service completely, briefly dropping all connections. Use reload whenever possible.
Where are Nginx config files on Rocky Linux?
Main config: /etc/nginx/nginx.conf. Site configs go in: /etc/nginx/conf.d/ — files ending in .conf are loaded automatically. Logs are at /var/log/nginx/. This is different from Ubuntu, which uses /etc/nginx/sites-available/ and requires symlinks.
Why does Nginx show a 403 after installation?
Usually a permissions issue — the Nginx user (nginx on Rocky Linux) doesn't have read access to the web root directory. Check with: ls -la /var/www/ and verify ownership matches the nginx user.