Nginx is a high-performance web server and reverse proxy widely used in production Linux environments. On RHEL 8, Nginx is available through the AppStream repository, making installation straightforward with dnf. Once installed, you will need to configure the firewall and SELinux to allow web traffic. This tutorial walks you through a complete Nginx installation and basic configuration on RHEL 8.
Prerequisites
- A RHEL 8 server with a non-root sudo user or root access
- A registered and subscribed RHEL 8 system (or access to AppStream via
dnf) - Basic familiarity with the Linux command line
- Port 80 and 443 available on the server
Step 1 — Install Nginx
Install Nginx from the AppStream repository using dnf:
sudo dnf install -y nginx
After installation, verify the version to confirm it installed correctly:
nginx -v
Step 2 — Enable and Start the Nginx Service
Enable Nginx to start automatically at boot, then start it immediately:
sudo systemctl enable --now nginx.service
Confirm the service is active and running:
sudo systemctl status nginx.service
You should see Active: active (running) in the output.
Step 3 — Configure the Firewall
RHEL 8 uses firewalld to manage network traffic rules. Allow HTTP and HTTPS traffic through the firewall:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
Verify the rules are active:
sudo firewall-cmd --list-services
Step 4 — Review the Nginx Configuration
The main Nginx configuration file is located at /etc/nginx/nginx.conf. Open it to review its structure:
sudo cat /etc/nginx/nginx.conf
Key sections to note:
- worker_processes — typically set to
autoto match CPU cores - server block — defines the default virtual host listening on port 80
- include /etc/nginx/conf.d/*.conf; — loads additional server block files from the
conf.ddirectory
The default document root is /usr/share/nginx/html. Place your HTML files there to serve them immediately.
Step 5 — Test the Default Page with curl
Test that Nginx is serving the default welcome page:
curl -I http://localhost
You should receive a 200 OK HTTP response. To view the full default page content:
curl http://localhost
From a remote machine, replace localhost with your server’s IP address or domain name.
Step 6 — Set SELinux Context for a Custom Document Root
If you serve files from a custom directory outside of /usr/share/nginx/html, SELinux will block Nginx from reading them unless you apply the correct file context. For example, to use /var/www/mysite as a document root:
sudo mkdir -p /var/www/mysite
sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/mysite(/.*)?"
sudo restorecon -Rv /var/www/mysite
If Nginx needs to act as a reverse proxy and connect to upstream services, enable the following SELinux boolean:
sudo setsebool -P httpd_can_network_connect 1
Verify the boolean is now on:
getsebool httpd_can_network_connect
Conclusion
You have successfully installed Nginx on RHEL 8, configured the firewall to allow HTTP and HTTPS traffic, and learned how to manage SELinux contexts for custom document roots. Nginx is now serving the default welcome page and is ready for further configuration.
Next steps: How to Configure Nginx Server Blocks on RHEL 8, How to Secure Nginx with Let’s Encrypt and Certbot on RHEL 8, and How to Set Up Nginx as a Reverse Proxy on RHEL 8.