Build and Install Nginx from Source on CentOS 7

Prerequisites

Ensure the system is running CentOS 7.6 and you intend to install Nginx 1.17.7.

Install Build Dependencies

First, install the necessary compliation tools and librraies required for the Nginx build process:

yum -y install gcc gcc-c++ automake make
yum -y install pcre-devel zlib-devel openssl-devel gd-devel \
    libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed \
    GeoIP-devel libcurl-devel

Download and Extract Source

Fetch the source code archive from the official Nginx repository and extract it:

cd /tmp
wget http://nginx.org/download/nginx-1.17.7.tar.gz
zcat nginx-1.17.7.tar.gz | tar -xvf -
cd nginx-1.17.7

Create Service Account

Create a dedicated system user and group for running the Nginx worker processes. This user will not have login shell access:

groupadd webserver
useradd -r -g webserver -s /sbin/nologin webserver

Configure Build Options

Run the configuration script to set the installation paths and enable specific modules. Adjust the --prefix if you prefer a different installation directory.

./configure \
  --prefix=/etc/webserver \
  --user=webserver \
  --group=webserver \
  --sbin-path=/usr/sbin/nginx \
  --conf-path=/etc/webserver/nginx.conf \
  --error-log-path=/var/log/webserver/error.log \
  --http-log-path=/var/log/webserver/access.log \
  --pid-path=/var/run/nginx.pid \
  --lock-path=/var/run/nginx.lock \
  --with-http_ssl_module \
  --with-http_stub_status_module \
  --with-http_gzip_static_module \
  --with-http_realip_module \
  --with-http_geoip_module \
  --with-stream \
  --with-debug

For a minimal setup, you can use a simplified configuration:

./configure --prefix=/etc/webserver --with-http_ssl_module --with-http_stub_status_module

Compile and Install

Execute the make commands to build the binaries and install them onto the system:

make
make install

Verify Installation

Check the installed version to ensure the binary is correctly compiled:

/usr/sbin/nginx -V

Configure Permissions and Service

Update the main configuration to use the webserver user and set up the systemd service unit.

  1. Update Config:

    cp /etc/webserver/nginx.conf /etc/webserver/nginx.conf.orig
    sed -i 's/#user  nobody;/user  webserver webserver;/' /etc/webserver/nginx.conf
    
  2. Create Systemd Unit:

    cat > /usr/lib/systemd/system/webserver.service << 'EOF'
    

[Unit] Description=High Performance Web Server After=network.target

[Service] Type=forking PIDFile=/var/run/nginx.pid ExecStartPre=/usr/sbin/nginx -t ExecStart=/usr/sbin/nginx ExecReload=/usr/sbin/nginx -s reload ExecStop=/usr/sbin/nginx -s stop PrivateTmp=true

[Install] WantedBy=multi-user.target EOF ```

  1. Set Permissions:
    chown -R webserver:webserver /etc/webserver
    chown -R webserver:webserver /var/log/webserver
    

Manage Service

Reload the systemd daemon, start the service, and enable it to launch at boot:

systemctl daemon-reload
systemctl start webserver
systemctl enable webserver

Add Custom Configuration Directory

To manage separate configuration files (e.g., for a MinIO cluster), create a conf.d directory and include it in the main config.

  1. Modify nginx.conf: Add the following line inside the http {} block: include /etc/webserver/conf.d/*.conf;

  2. Create Directory and Example Config:

    mkdir -p /etc/webserver/conf.d
    

    Example configuration for a load balancer:

    upstream storage_cluster {
        ip_hash;
        server 192.168.10.101:9000;
        server 192.168.10.102:9000;
    }
    
    server {
        listen 19000;
        server_name storage_proxy;
    
        location / {
            proxy_pass http://storage_cluster;
            proxy_set_header Host $host;
            client_max_body_size 1g;
        }
    }
    
  3. Apply Permissions and Reload:

    chown -R webserver:webserver /etc/webserver/conf.d
    systemctl reload webserver
    

Tags: CentOS 7 nginx source installation Linux Administration Web Server

Posted on Fri, 23 Oct 2026 16:12:42 +0000 by Vince