How to Install Nginx on Rocky Linux

Install Nginx on Rocky Linux

Are you looking to set up a powerful, lightweight web server on Rocky Linux? You’ve come to the right place! Nginx (pronounced “engine-x”) has become one of the most popular web servers in the world, and for good reason. In this comprehensive guide, I’ll walk you through everything you need to know about installing and configuring Nginx on Rocky Linux.

Whether you’re a system administrator managing enterprise infrastructure or a developer setting up your first web server, this tutorial will provide you with clear, actionable steps to get Nginx up and running on your Rocky Linux system.

What is Nginx and Why Choose It for Rocky Linux?

Nginx is an open-source, high-performance web server that’s responsible for hosting some of the largest and highest-traffic sites on the internet. It’s not just a web server though – it can also function as a reverse proxy, load balancer, and HTTP cache, making it incredibly versatile for modern web architectures.

Understanding Nginx’s Architecture

What sets Nginx apart from traditional web servers like Apache is its event-driven, asynchronous architecture. Instead of creating a new process or thread for each connection, Nginx uses an efficient event loop that can handle thousands of concurrent connections with minimal resource usage. This makes it particularly well-suited for high-traffic websites and applications.

Benefits of Using Nginx on Rocky Linux

Rocky Linux, being an enterprise-grade distribution that’s binary-compatible with Red Hat Enterprise Linux, provides an excellent foundation for running Nginx. Here’s why this combination works so well:

  • Stability: Rocky Linux offers long-term support and stability, perfect for production environments
  • Performance: Nginx’s lightweight footprint complements Rocky Linux’s efficient resource management
  • Security: Both platforms prioritize security with regular updates and robust default configurations
  • Community Support: Extensive documentation and community resources for both platforms

Prerequisites for Installing Nginx on Rocky Linux

Before we dive into the installation process, let’s make sure you have everything you need to successfully install Nginx on your Rocky Linux system.

System Requirements

Nginx is remarkably lightweight, but here are the minimum requirements:

  • Rocky Linux 8 or 9 (this guide works for both versions)
  • At least 512MB of RAM (1GB recommended)
  • 100MB of free disk space
  • Network connectivity for downloading packages

User Privileges and Access

You’ll need either root access or a user account with sudo privileges. If you’re using a non-root user, make sure they’re added to the wheel group, which grants sudo access on Rocky Linux.

Checking Your Rocky Linux Version

Before proceeding, let’s verify your Rocky Linux version:

cat /etc/redhat-release

This will display your exact Rocky Linux version, which helps ensure compatibility with the installation steps.

Method 1: Installing Nginx from Default Rocky Linux Repositories

The easiest way to install Nginx on Rocky Linux is using the default repositories. This method ensures you get a stable version that’s been tested with your specific Rocky Linux release.

Updating Your System

First, let’s make sure your system is up to date. This is crucial for security and compatibility:

sudo dnf update

If there are kernel updates, you might need to reboot your system after the update completes.

Installing Nginx Package

Nginx is available in Rocky’s default repositories, so you can install it with a single command using the dnf package manager:

sudo dnf install nginx

When prompted, enter y to confirm the installation. The dnf package manager will automatically resolve and install any required dependencies.

Verifying the Installation

Once the installation completes, you can verify that Nginx was installed correctly by checking its version:

nginx -v

This command will display the installed Nginx version, confirming that the installation was successful.

Method 2: Installing Nginx Mainline Version

If you need the latest features and improvements, you might want to install the Nginx mainline version instead of the stable version from the default repositories.

Adding the Official Nginx Repository

The mainline version isn’t available in Rocky’s default repositories, so you’ll need to add the official Nginx repository:

sudo dnf install epel-release

Then add the Nginx mainline repository configuration.

Installing Latest Nginx Mainline

After adding the repository, you can install the mainline version:

sudo dnf install nginx

The mainline version typically includes the latest features and bug fixes, though it may be less stable than the version in the default repositories.

Starting and Enabling Nginx Service

Now that Nginx is installed, we need to start the service and configure it to start automatically when your system boots.

Managing Nginx with Systemctl

Rocky Linux uses systemd for service management. You can start and enable Nginx in one command:

sudo systemctl enable --now nginx

This command does two things:

  • Starts the Nginx service immediately
  • Enables it to start automatically on system boot

Alternatively, you can run these commands separately:

sudo systemctl start nginx
sudo systemctl enable nginx

Checking Service Status

To verify that Nginx is running correctly, check its status:

sudo systemctl status nginx

You should see output indicating that Nginx is “Active: active (running)”. This confirms that your web server is up and running.

Configuring Firewall for Nginx

Rocky Linux comes with a firewall enabled by default, so you’ll need to configure it to allow web traffic to reach your Nginx server.

Opening HTTP Port 80

To allow HTTP traffic on port 80, run:

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

Enabling HTTPS Port 443

If you plan to use SSL/TLS certificates (which you should for production sites), also open port 443:

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

After making these changes, reload the firewall to apply them:

sudo firewall-cmd --reload

Verifying Firewall Rules

You can verify that the HTTP service was added correctly:

sudo firewall-cmd --permanent --list-all

You should see “http” (and “https” if you added it) listed under services.

Testing Your Nginx Installation

Now comes the exciting part – testing whether your Nginx installation is working correctly!

Accessing the Default Welcome Page

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

http://your-server-ip

If everything is configured correctly, you should see the default Nginx welcome page. This page confirms that Nginx is successfully serving web content.

Command Line Testing Methods

You can also test Nginx from the command line using curl:

curl -I localhost

This should return HTTP headers showing that Nginx is responding to requests. You should see something like:

HTTP/1.1 200 OK
Server: nginx/1.20.1

Understanding Nginx Configuration Files

Understanding Nginx’s configuration structure is crucial for customizing your web server to meet your specific needs.

Main Configuration File Location

The main Nginx configuration file is located at:

/etc/nginx/nginx.conf

This file contains global settings that affect the entire Nginx installation.

Directory Structure Overview

Nginx organizes its configuration files in several directories:

  • /etc/nginx/conf.d/ – Additional configuration files
  • /etc/nginx/sites-available/ – Available site configurations (may need to be created)
  • /etc/nginx/sites-enabled/ – Enabled site configurations (may need to be created)
  • /usr/share/nginx/html/ – Default document root

Log Files and Their Locations

Nginx stores its log files in /var/log/nginx/:

  • access.log – Records all requests to your server
  • error.log – Contains error messages and diagnostic information

These logs are invaluable for troubleshooting and monitoring your web server’s performance.

Basic Nginx Configuration and Customization

Once you have Nginx running, you’ll likely want to customize it for your specific needs.

Creating Your First Server Block

Server blocks (similar to Apache’s virtual hosts) allow you to host multiple websites on a single server. Here’s a basic example:

server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com;
    index index.html index.php;
    
    location / {
        try_files $uri $uri/ =404;
    }
}

Setting Up Document Root

Create a directory for your website files:

sudo mkdir -p /var/www/example.com
sudo chown -R nginx:nginx /var/www/example.com

Then create a simple HTML file to test your configuration.

Common Issues and Troubleshooting

Even with careful installation, you might encounter some issues. Let’s address the most common ones.

Permission Problems

If you’re getting permission denied errors, check:

  • File ownership (should be nginx:nginx)
  • Directory permissions (typically 755 for directories, 644 for files)
  • SELinux contexts (use restorecon -R /var/www/)

Port Conflicts

If another service is using port 80, you’ll need to either:

  • Stop the conflicting service
  • Configure Nginx to use a different port
  • Resolve the conflict by reconfiguring both services

SELinux Considerations

Rocky Linux has SELinux enabled by default, which can sometimes interfere with web server operations. If you’re having issues, check SELinux status:

getenforce

You can view SELinux denials in the audit log:

sudo ausearch -m AVC -ts recent

Performance Optimization Tips

To get the best performance from your Nginx installation, consider these optimization techniques.

Worker Process Configuration

Adjust the number of worker processes based on your CPU cores:

worker_processes auto;

This automatically sets the number of worker processes to match your CPU core count.

Connection Limits and Timeouts

Optimize connection handling:

worker_connections 1024;
keepalive_timeout 65;
client_max_body_size 8M;

These settings help balance performance with resource usage.

Security Best Practices

Security should be a top priority when running any web server.

Hiding Nginx Version

Prevent version disclosure by adding this to your configuration:

server_tokens off;

SSL/TLS Configuration

Always use HTTPS in production. You can obtain free SSL certificates from Let’s Encrypt:

sudo dnf install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com

Managing Nginx Service

Understanding how to manage your Nginx service is essential for ongoing maintenance.

Starting, Stopping, and Restarting

Common service management commands:

sudo systemctl start nginx    # Start the service
sudo systemctl stop nginx     # Stop the service
sudo systemctl restart nginx  # Restart the service

Reloading Configuration

When you make configuration changes, you can reload Nginx without stopping it:

sudo systemctl reload nginx

This is particularly useful for production servers where you want to avoid downtime.

Frequently Asked Questions (FAQs)

1. Can I install Nginx alongside Apache on Rocky Linux?

Yes, you can run both Nginx and Apache on the same server, but they’ll need to use different ports. Typically, you’d configure Nginx on port 80/443 and Apache on alternative ports like 8080/8443, or use Nginx as a reverse proxy in front of Apache.

2. How do I check if Nginx is using too much memory on my Rocky Linux server?

You can monitor Nginx memory usage with commands like htop, ps aux | grep nginx, or systemctl status nginx. Nginx is generally very memory-efficient, but if you notice high usage, check your worker_processes and worker_connections settings in the configuration file.

3. What’s the difference between the stable and mainline versions of Nginx?

The stable version receives only bug fixes and is recommended for production environments. The mainline version includes the latest features, improvements, and bug fixes but may be less stable. For most users, the stable version from Rocky Linux repositories is the best choice.

4. How do I completely uninstall Nginx from Rocky Linux if needed?

To completely remove Nginx, stop the service first (sudo systemctl stop nginx), then remove the package (sudo dnf remove nginx), and finally clean up configuration files (sudo rm -rf /etc/nginx /var/log/nginx). Be careful with the rm command and ensure you’ve backed up any important configurations.

5. Why can’t I access my Nginx server from external networks even though it’s running?

This is usually a firewall issue. Ensure you’ve opened the necessary ports (sudo firewall-cmd --permanent --add-service=http) and reloaded the firewall (sudo firewall-cmd --reload). Also, check if your hosting provider or network has additional firewalls that might be blocking traffic.

Marshall Anthony is a professional Linux DevOps writer with a passion for technology and innovation. With over 8 years of experience in the industry, he has become a go-to expert for anyone looking to learn more about Linux.

Related Posts