Installing Nginx on CentOS 7: Complete Step-by-Step Guide

Prerequisites Setup

Before installing Nginx, create a dedicated directory for the installation.

cd /usr/local/
mkdir nginx
cd nginx

Downloading Nginx

Navigate to the official Nginx website and download the desired version. Its recommended to use the stable release for production environments.

Uploading and Extracting

Transfer the Nginx archive to the created directory using an SFTP client. Extract the package with the following command:

tar -zxvf nginx-1.24.0.tar.gz

Installing Required Dependencies

Nginx requires several dependencies that must be installed before compilation.

Installing GCC Compiler

GCC (GNU Compiler Collection) compiles C, C++, Ada, Object C, and Java languages.

yum -y install gcc

Installing PCRE Library

PCRE provides Perl-compatible regular expression support, which Nginx uses for parsing regex patterns in HTTP modules.

yum install -y pcre pcre-devel

Installing Zlib Library

Zlib offers various compression and decompression methods. Nginx uses Zlib for gzip compression of HTTP content.

yum install -y zlib zlib-devel

Installing OpenSSL

OpenSSL forms the foundation of secure web communications.

yum install -y openssl openssl-devel

Compiling and Installing Nginx

Navigate to the extracted Nginx directory and execute the build process.

cd nginx-1.24.0
./configure
make
make install

Starting and Verifying Nginx

Launching the Service

cd /usr/local/nginx
./sbin/nginx
ps -ef | grep nginx

Firewall Configuration

Allow traffic through port 80 (the default Nginx port):

firewall-cmd --add-port=80/tcp
firewall-cmd --add-port=80/tcp --permanent

Access the server by entering your public IP address in a web browser.

Security Hardening

Hiding Nginx Version

Edit the Nginx configuration file to enhance security:

cd /usr/local/nginx/conf
vim ./nginx.conf

Add the following in the HTTP block to hide version information:

server_tokens off;

Changing Default Port

Modify the default listening port in the server block:

listen 8080;

Save and exit with :wq, then reload Nginx:

./sbin/nginx -s reload

Access using http://your-server-ip:8080

Additional Security Configurations

Add these security measures in the HTTP block of nginx.conf:

http {
    # IP access control
    deny 192.168.0.1;
    allow 192.168.0.0/24;
    
    # Prevent directory traversal
    location ~ /\. {
        deny all;
    }
    
    # Block SQL injection attempts
    location ~ inj/ {
        deny all;
    }
    
    # Restrict HTTP request methods
    if ($request_method !~ ^(GET|HEAD|POST)$) {
        return 444;
    }
    
    # Disable directory listing
    location ~ ^/\. {
        deny all;
    }
}

Troubleshooting Common Issues

  • If the service fails to start, check error logs in /usr/local/nginx/logs/
  • Verify all dependencies are properly installed
  • Ensure firewall rules allow the configured port
  • Confirm no other service is using the same port

Tags: nginx centos Linux Web Server installation

Posted on Wed, 13 May 2026 01:50:40 +0000 by nicholaspaul