How to Install Nginx on RHEL 7
Nginx is a high-performance, open-source web server and reverse proxy known for its speed, stability, and low memory footprint. On Red Hat Enterprise Linux 7, Nginx is not available in the default repositories, so you must either enable the EPEL (Extra Packages for Enterprise Linux) repository or configure the official Nginx repository before installation. This tutorial walks you through the full process: adding the appropriate repository, installing Nginx, configuring the firewall, reviewing the default configuration, and verifying that your web server is running correctly.
Prerequisites
- A running RHEL 7 server with root or
sudoaccess - A registered and subscribed RHEL 7 system (or access to a RHEL 7-compatible mirror)
- Basic familiarity with the command line
- An active network connection to download packages
Step 1: Enable the EPEL Repository
The EPEL repository provides many open-source packages not included in the standard RHEL repositories, including Nginx. Install the EPEL release package with the following command:
sudo yum install epel-release -y
If epel-release is not available directly on your RHEL 7 system (which is common on officially registered systems), download the RPM manually and install it:
sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm -y
Alternatively, you can use the official Nginx repository. Create the repo file:
sudo tee /etc/yum.repos.d/nginx.repo <<'EOF'
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/rhel/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
Using the official Nginx repo gives you access to the latest stable releases directly from the Nginx project. Choose whichever method suits your environment best.
Step 2: Install Nginx
Once the repository is enabled, install Nginx using yum:
sudo yum install nginx -y
Verify the installed version to confirm the installation succeeded:
nginx -v
Expected output will resemble:
nginx version: nginx/1.24.0
Step 3: Start and Enable Nginx
On RHEL 7, services are managed through systemctl. Start the Nginx service and configure it to start automatically at boot:
sudo systemctl start nginx
sudo systemctl enable nginx
Confirm that the service is running:
sudo systemctl status nginx
You should see output indicating that the service is active (running). The output will also show the process ID and recent log entries from the journal.
Step 4: Configure the Firewall
RHEL 7 uses firewalld as its default firewall management tool. By default, HTTP traffic on port 80 and HTTPS traffic on port 443 are blocked. Open these ports permanently and reload the firewall rules:
sudo firewall-cmd --zone=public --add-service=http --permanent
sudo firewall-cmd --zone=public --add-service=https --permanent
sudo firewall-cmd --reload
Verify the active rules to confirm the services are listed:
sudo firewall-cmd --zone=public --list-all
The output should include http and https in the services: line.
Step 5: Review the Default Nginx Configuration
The main Nginx configuration file on RHEL 7 is located at /etc/nginx/nginx.conf. Open it to review the default settings:
sudo cat /etc/nginx/nginx.conf
Key directives to be aware of in the default configuration include:
worker_processes auto;— Nginx automatically sets the number of worker processes based on the number of CPU cores.error_log /var/log/nginx/error.log warn;— Errors are logged at the warning level and above.access_log /var/log/nginx/access.log main;— All HTTP requests are logged in the combined format.include /etc/nginx/conf.d/*.conf;— Additional virtual host and server block configurations are loaded from the/etc/nginx/conf.d/directory.
The default server block listens on port 80 and serves files from /usr/share/nginx/html. You can place a test HTML file there to verify Nginx is serving content correctly.
Step 6: Test the Configuration Syntax
Before restarting or reloading Nginx after any configuration change, always test the configuration syntax to catch errors:
sudo nginx -t
If the configuration is valid, you will see:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
If there are syntax errors, Nginx will report the file and line number where the problem occurred, making it straightforward to diagnose and fix issues before they affect the running server.
Step 7: Test the Web Server with curl
From the server itself, use curl to verify that Nginx is serving the default page:
curl -I http://localhost
You should receive an HTTP response with a 200 OK status and a Server: nginx header. To retrieve the full page content:
curl http://localhost
The default Nginx welcome page HTML will be returned. If you have a public IP address or DNS name pointing to your server, you can also test from a remote machine:
curl -I http://your-server-ip
Step 8: Review Nginx Log Files
Nginx writes two primary log files on RHEL 7:
- Access log:
/var/log/nginx/access.log— Records all incoming HTTP requests with status codes, response sizes, referrers, and user agents. - Error log:
/var/log/nginx/error.log— Records errors, warnings, and diagnostic messages from the Nginx process.
Tail the access log in real time to watch incoming requests:
sudo tail -f /var/log/nginx/access.log
To monitor errors as they occur:
sudo tail -f /var/log/nginx/error.log
These logs are invaluable for debugging configuration issues, tracking down unexpected behavior, and monitoring traffic patterns. You can adjust the logging level in /etc/nginx/nginx.conf by changing the error_log directive level from warn to info or debug for more verbose output during troubleshooting.
Step 9: Reload Nginx After Configuration Changes
When you modify any configuration file, reload Nginx to apply the changes without dropping active connections. This is preferable to a full restart during production use:
sudo systemctl reload nginx
If you need a full service restart (for example, after major changes or upgrades):
sudo systemctl restart nginx
Conclusion
You have successfully installed and configured Nginx on RHEL 7. The server is running, the firewall has been opened for HTTP and HTTPS traffic, and you have reviewed the key configuration files and log locations. Nginx is now ready to serve web content, act as a reverse proxy, or be configured with virtual hosts for multiple domains. The next steps typically involve setting up server blocks in /etc/nginx/conf.d/ for hosting multiple sites, configuring SSL with Let’s Encrypt, and tuning worker processes and connection limits for your specific workload.