Comprehensive Guide: Building NGINX from Source Code
NGINX is a high-performance tool that can function as an HTTP/HTTPS server, reverse proxy, mail proxy, load balancer, TLS termination point, or a caching engine. Its architecture is modular, supporting both built-in and third-party extensions. As it’s developed in C, it delivers excellent speed and low memory usage.
Version Types of NGINX
NGINX maintains two concurrent release branches: the mainline and the stable versions. Either can be deployed on production environments, though the mainline release is generally recommended for its up-to-date features.
Overview of Installation from Source
The process of installing NGINX from its source code is straightforward. Simply download the latest version, configure it according to your needs, compile the source, and then install it. This tutorial demonstrates the installation using the mainline version 1.13.2. If newer versions are available, ensure you update the version numbers accordingly.
Essential Prerequisites for Compiling NGINX
Mandatory Components:
- OpenSSL library version ranging from 1.0.2 to 1.1.0
- zlib compression library, version 1.1.3 through 1.2.11
- PCRE (Perl Compatible Regular Expressions), between version 4.4 and 8.40
- GCC compiler suite
Optional Components:
- PERL language support
- libatomic_ops library
- LibGD image processing library
- MaxMind GeoIP library
- libxml2 XML parser
- libxslt XSLT processor
Initial Preparation Steps
Before proceeding with the installation process, carry out the following preliminary tasks:
Create a Sudo-Enabled User Account
Begin by setting up a non-root user account with administrative privileges.
Switch to the New User
su - <username>
Update the Operating System
sudo yum check-update || sudo yum update -y
How to Build and Install NGINX from Source on CentOS
Install Required Development Tools
Start by installing the necessary development packages along with the Vim text editor:
sudo yum groupinstall -y 'Development Tools' && sudo yum install -y vim
Enable EPEL Repository
Install the Extra Packages for Enterprise Linux (EPEL) repository:
sudo yum install -y epel-release
Install Optional Dependencies
Install optional packages required by NGINX:
sudo yum install -y perl perl-devel perl-ExtUtils-Embed libxslt libxslt-devel libxml2 libxml2-devel gd gd-devel GeoIP GeoIP-devel
Download and Unpack NGINX Source Code
Fetch the mainline version and extract it:
wget https://nginx.org/download/nginx-1.13.2.tar.gz && tar zxvf nginx-1.13.2.tar.gz
Download Required Libraries
NGINX relies on PCRE, zlib, and OpenSSL. Download and extract their sources:
# PCRE version 8.40
wget https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz && tar xzvf pcre-8.40.tar.gz
# zlib version 1.2.11
wget https://www.zlib.net/zlib-1.2.11.tar.gz && tar xzvf zlib-1.2.11.tar.gz
# OpenSSL version 1.1.0f
wget https://www.openssl.org/source/openssl-1.1.0f.tar.gz && tar xzvf openssl-1.1.0f.tar.gz
Clean Up Archive Files
rm -rf *.tar.gz
Change Directory and Inspect
cd ~/nginx-1.13.2
ls
# auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
Install the NGINX Manual Page
sudo cp ~/nginx-1.13.2/man/nginx.8 /usr/share/man/man8
sudo gzip /usr/share/man/man8/nginx.8
man nginx
Review Configure Options
./configure --help
./configure --help | grep -F =dynamic
Configure, Compile and Install
./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/nginx.pid \
--lock-path=/var/run/nginx.lock \
--user=nginx \
--group=nginx \
--build=CentOS \
--builddir=nginx-1.13.2 \
--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 \
--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.40 \
--with-pcre-jit \
--with-zlib=../zlib-1.2.11 \
--with-openssl=../openssl-1.1.0f \
--with-openssl-opt=no-nextprotoneg \
--with-debug
make
sudo make install
Set Up Module Symlink
sudo ln -s /usr/lib64/nginx/modules /etc/nginx/modules
Verify Build and Version
sudo nginx -V
Create NGINX User and Validate Config
sudo useradd --system --home /var/cache/nginx --shell /sbin/nologin --comment "nginx user" --user-group nginx
sudo nginx -t
sudo mkdir -p /var/cache/nginx && sudo nginx -t
Create systemd Service File
sudo vim /usr/lib/systemd/system/nginx.service
Paste the following configuration:
[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
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
[Install]
WantedBy=multi-user.target
Start and Enable NGINX
sudo systemctl start nginx.service && sudo systemctl enable nginx.service
sudo systemctl is-enabled nginx.service
sudo systemctl status nginx.service
ps aux | grep nginx
curl -I 127.0.0.1
Reboot and Cleanup
sudo shutdown -r now
sudo rm /etc/nginx/koi-utf /etc/nginx/koi-win /etc/nginx/win-utf
Enable NGINX Syntax Highlighting in Vim
mkdir ~/.vim/
cp -r ~/nginx-1.13.2/contrib/vim/* ~/.vim/
Remove Default Configuration Backups
sudo rm /etc/nginx/*.default
Conclusion
And that’s a wrap. You’ve now installed the latest NGINX release successfully. This version is statically linked to crucial libraries such as OpenSSL. Since operating systems often ship with outdated OpenSSL packages, compiling it this way allows you to benefit from recent enhancements. This includes support for advanced encryption methods like CHACHA20_POLY1305 and newer protocols such as TLS 1.3, which will be introduced in OpenSSL 1.1.1 (still pending release at the time this was written).