Installing and Configuring Nginx on CentOS 7

Step 2: Installing Nginx

Using the configured EPEL repository, we can install Nginx with the following command:

sudo yum install nginx

Step 3: Starting the Nginx Service

After installation, start the Nginx service and enable it to start automatically on boot:

sudo systemctl start nginx
sudo systemctl enable nginx

Step 4: Verifying Nginx Service Status

Confirm that the Nginx service is running properly:

sudo systemctl status nginx

A successfully installed and running Nginx service will display an active (running) status.

Step 5: Configuring Firewall (Skip if Firewall is Disabled)

To allow network access to the Nginx service, we need to open HTTP and HTTPS ports in the firewall:

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

Additional Operations

Reloading Nginx Configuration

After modifying Nginx configuration files, reload the service to apply changes without restarting the service. You can also use nginx -t to validate the configuration:

sudo systemctl reload nginx

Restarting Nginx Service

If you need to completely restart the Nginx service (for example, after making significant changes or updates), use the following command:

sudo systemctl restart nginx

Nginx Configuration File Guide

The main Nginx configuration file is located at /etc/nginx/nginx.conf, which includes additional configuration files from the /etc/nginx/conf.d directory. These configuration files control Nginx's behavior, including server blocks (similar to virtual hosts in Apache) that define how differant websites or applications are handled.

Basic Examples

1. Modifying the Default Listening Port

By default, Nginx listens on port 80 for HTTP requests. To change to a different port, modify the listen directive in the apppropriate server block:

server {
    listen 8080;  # Changed to port 8080
    server_name example.com;
    location / {
        root /usr/share/nginx/html;
        index index.html index.htm;
    }
}

2. Configuring Reverse Proxy

Nginx is commonly used as a reverse proxy to forward requests to internal applications:

server {
    listen 80;
    server_name example.com;
    
    location /app {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        # Additional proxy settings can be added here
    }
}

Tags: nginx centos Web Server Linux System Administration

Posted on Sun, 27 Sep 2026 16:30:23 +0000 by tnkannan