In this tutorial, we will build Nginx with all available modules in the open-source version of Nginx.

NGINX can be used as an HTTP/HTTPS server, reverse proxy server, mail proxy server, load balancer, TLS terminator, or caching server.

Initial Steps

Update system.

# dnf update
# cat /etc/redhat-release 
CentOS Linux release 8.1.1911 (Core)

Build NGINX from source

Install “Development Tools” .

# dnf groupinstall 'Development Tools'

Install Extra Packages for Enterprise Linux (EPEL).

# dnf install epel-release

Download the latest NGINX source code and untar it.

# wget && tar zxvf nginx-1.17.8.tar.gz

Download the mandatory NGINX dependencies source code and untar them.

# PCRE version 8.44
# wget && tar zxvf pcre-8.44.tar.gz

# zlib version 1.2.11
# wget && tar xzvf zlib-1.2.11.tar.gz

# OpenSSL version 1.1.1d
# wget && tar zxvf openssl-1.1.1d.tar.gz

Install optional Nginx dependencies.

# dnf install perl perl-devel perl-ExtUtils-Embed libxslt libxslt-devel libxml2 libxml2-devel gd gd-devel GeoIP GeoIP-devel

Enter the Nginx source directory.

# cd nginx-1.17.8/

Configure, compile and install NGINX.

./configure --prefix=/etc/nginx \
            --sbin-path=/usr/sbin/nginx \
            --modules-path=/usr/lib64/nginx/modules \
            --conf-path=/etc/nginx/nginx.conf \
            --error-log-path=/var/log/nginx/error.log \
            --pid-path=/var/run/ \
            --lock-path=/var/run/nginx.lock \
            --user=nginx \
            --group=nginx \
            --build=CentOS \
            --builddir=nginx-1.17.8 \
            --with-select_module \
            --with-poll_module \
            --with-threads \
            --with-file-aio \
            --with-http_ssl_module \
            --with-http_v2_module \
            --with-http_realip_module \
            --with-http_addition_module \
            --with-http_xslt_module=dynamic \
            --with-http_image_filter_module=dynamic \
            --with-http_geoip_module=dynamic \
            --with-http_sub_module \
            --with-http_dav_module \
            --with-http_flv_module \
            --with-http_mp4_module \
            --with-http_gunzip_module \
            --with-http_gzip_static_module \
            --with-http_auth_request_module \
            --with-http_random_index_module \
            --with-http_secure_link_module \
            --with-http_degradation_module \
            --with-http_slice_module \
            --with-http_stub_status_module \
            --with-http_perl_module=dynamic \
            --with-perl_modules_path=/usr/lib64/perl5 \
            --with-perl=/usr/bin/perl \
            --http-log-path=/var/log/nginx/access.log \
            --http-client-body-temp-path=/var/cache/nginx/client_temp \
            --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
            --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
            --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
            --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
            --with-mail=dynamic \
            --with-mail_ssl_module \
            --with-stream=dynamic \
            --with-stream_ssl_module \
            --with-stream_realip_module \
            --with-stream_geoip_module=dynamic \
            --with-stream_ssl_preread_module \
            --with-compat \
            --with-pcre=../pcre-8.44 \
            --with-pcre-jit \
            --with-zlib=../zlib-1.2.11 \
            --with-openssl=../openssl-1.1.1d \
            --with-openssl-opt=no-nextprotoneg \

# make
# make install

Symlink /usr/lib64/nginx/modules to /etc/nginx/modules directory, so that you can load dynamic modules in Nginx configuration like this load_module modules/;

# ln -s /usr/lib64/nginx/modules /etc/nginx/modules

Check NGINX version and installed modules.

# nginx -V
nginx version: nginx/1.17.8 (CentOS)
built by gcc 8.3.1 20190507 (Red Hat 8.3.1-4) (GCC) 
built with OpenSSL 1.1.1d 10 Sep 2019
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/ --lock-path=/var/run/nginx.lock --user=nginx --group=nginx --build=CentOS --builddir=nginx-1.17.8 --with-select_module --with-poll_module --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-perl_modules_path=/usr/lib64/perl5 --with-perl=/usr/bin/perl --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --with-mail=dynamic --with-mail_ssl_module --with-stream=dynamic --with-stream_ssl_module --with-stream_realip_module --with-stream_geoip_module=dynamic --with-stream_ssl_preread_module --with-compat --with-pcre=../pcre-8.44 --with-pcre-jit --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-1.1.1d --with-openssl-opt=no-nextprotoneg --with-debug

Create Nginx system group and user.

# useradd --system --home /var/cache/nginx --shell /sbin/nologin --comment "nginx user" --user-group nginx

Сreate directory.

# mkdir -p /var/cache/nginx

Check syntax and potential errors.

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Create Nginx systemd unit file.

# nano /etc/systemd/system/nginx.service

Copy the next to it.

Description=nginx - high performance web server

ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID


Start and enable the NGINX service.

# systemctl start nginx.service && sudo systemctl enable nginx.service

Check if NGINX is running.

# systemctl status nginx.service
 nginx.service - nginx - high performance web server
Loaded: loaded (/etc/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2020-02-18 10:14:54 CET; 1min 5s ago
Main PID: 30581 (nginx)
Tasks: 2 (limit: 24434)
Memory: 1.7M
CGroup: /system.slice/nginx.service
├─30581 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─30582 nginx: worker process

Feb 18 10:14:54 uni systemd[1]: Starting nginx - high performance web server...
Feb 18 10:14:54 uni nginx[30578]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Feb 18 10:14:54 uni nginx[30578]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Feb 18 10:14:54 uni systemd[1]: Started nginx - high performance web server.

You can open your browser and enter your domain/IP address to see the default Nginx page. 

Now, you have the latest version of Nginx installed by building it from source code.