How to Install Nginx on AlmaLinux/Rocky Linux

This guide covers installing and configuring Nginx on AlmaLinux or Rocky Linux, including the firewalld and SELinux considerations specific to RHEL-family distributions.

Prerequisites

  • AlmaLinux 9 or Rocky Linux 9 VPS
  • Root or sudo access

Step 1 — Install Nginx

sudo dnf install nginx -y

Step 2 — Enable and Start Nginx

sudo systemctl enable --now nginx

Step 3 — Allow HTTP/HTTPS Through firewalld

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

Step 4 — Test in a Browser

http://YOUR_SERVER_IP

You should see the default Nginx welcome page.

Key File Locations (Same as Ubuntu/Debian)

PathPurpose
/etc/nginx/nginx.confMain configuration
/etc/nginx/conf.d/Virtual host definitions (differs from Ubuntu's sites-available/sites-enabled pattern)
/usr/share/nginx/htmlDefault website root

Note: AlmaLinux/Rocky Linux's Nginx package uses /etc/nginx/conf.d/*.conf directly, rather than Ubuntu/Debian's sites-available/sites-enabled symlink pattern.

Creating a Virtual Host

sudo nano /etc/nginx/conf.d/example.com.conf
server {
    listen 80;
    server_name example.com;
    root /var/www/example.com/html;
    index index.html;
}
sudo nginx -t
sudo systemctl reload nginx

Step 5 — Handling SELinux for Custom Web Roots

If you serve content from a directory outside the default web root, SELinux will block access by default even with correct file permissions:

sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/example.com/html(/.*)?"
sudo restorecon -Rv /var/www/example.com/html

If semanage isn't installed:

sudo dnf install policycoreutils-python-utils -y

Allowing Nginx to Make Network Connections (For Reverse Proxy Setups)

SELinux blocks Nginx from proxying to backend applications by default:

sudo setsebool -P httpd_can_network_connect 1

Required if using Nginx as a reverse proxy (see Nginx as a Reverse Proxy for Node.js/Docker Apps) on AlmaLinux/Rocky Linux.

Adding SSL with Certbot

sudo dnf install epel-release -y
sudo dnf install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com

Common Errors

403 Forbidden despite correct file permissions — almost always an SELinux context issue on AlmaLinux/Rocky Linux; run the semanage fcontext/restorecon commands above.

sudo ausearch -m avc -ts recent

Reviewing SELinux audit denials directly confirms whether SELinux is the actual blocker.

502 Bad Gateway on a reverse proxy setup — likely httpd_can_network_connect is disabled; enable it as shown above.

Best Practices

  • Don't disable SELinux to work around permission issues — use the correct SELinux context commands instead
  • Check ausearch -m avc whenever encountering an unexplained permission error
  • Follow the same Nginx tuning and security practices covered for Ubuntu/Debian, adapted to this OS's paths

Continue Reading

Browse more articles in AlmaLinux & Rocky Linux.

  • nginx almalinux, nginx rocky linux, rhel nginx, selinux nginx
  • 0 brukere syntes dette svaret var til hjelp
Var dette svaret til hjelp?

Relaterte artikler

AlmaLinux vs Rocky Linux vs CentOS: What Happened to CentOS?

If you're choosing a RHEL-compatible Linux distribution for your VPS, understanding the CentOS...

How to Get Started with AlmaLinux/Rocky Linux on a VPS

This guide covers the essential first steps after deploying a new AlmaLinux or Rocky Linux VPS...

How to Use dnf: The Package Manager for AlmaLinux & Rocky Linux

dnf (Dandified YUM) is the package manager for AlmaLinux, Rocky Linux, and other RHEL-family...

How to Configure firewalld on AlmaLinux/Rocky Linux

firewalld is the default firewall management tool on AlmaLinux and Rocky Linux, using a...

How to Install Apache (httpd) on AlmaLinux/Rocky Linux

On RHEL-family distributions, Apache is packaged as httpd rather than apache2 — a naming...