Nginx is a widely famous and open-source web server that you can use as a web server, reverse proxy, cache server and load balancer among other things. It is very powerful and lightweight and takes very few resources. In this tutorial, you will learn how to build Nginx web server from source on Ubuntu 20.04 and Centos 8.0.
Compiling NGINX from source provides more flexibility than prebuilt packages. You can add particular modules (from NGINX or third parties), and apply latest security patches.
Stable version vs. Mainline version
NGINX Source is available in two versions.
- Stable – This version doesn’t include all of the latest features, but has critical bug fixes. It is recommended for Production environment.
- Mainline – This version includes the latest features and bug fixes and is always up to date. It is reliable, but it may include some experimental modules.
Pre-requisites
- For Ubuntu 18.04/20.04
You need to install the following packages on the Ubuntu systems before compiling nginx package from the source
$ sudo apt-get install -y curl build-essential make gcc libpcre3 libpcre3-dev libpcre++-dev zlib1g-dev libbz2-dev libxslt1-dev libxml2-dev libgeoip-dev libgoogle-perftools-dev libperl-dev libssl-dev libcurl4-openssl-dev libatomic-ops-dev
- For CentOS 7/8
You need to install the following packages CentOS systems before compiling nginx package from the source
$ yum install gcc zlib-devel openssl-devel make pcre-devel libxml2-devel libxslt-devel libgcrypt-devel gd-devel perl-ExtUtils-Embed GeoIP-devel
Now, you can go ahead to install nginx from source packages.
Installing NGINX Dependencies
Before compiling NGINX from source, you need to install below libraries for its dependencies.
- PCRE – This package supports regular expressions, required by the NGINX Core and Rewrite modules.
$ wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.44.tar.gz $ tar -zxf pcre-8.44.tar.gz $ cd pcre-8.44 $ ./configure $ make $ sudo make install
- zlib – This package supports header compression, required by the NGINX Gzip module.
$ wget http://zlib.net/zlib-1.2.11.tar.gz $ tar -zxf zlib-1.2.11.tar.gz $ cd zlib-1.2.11 $ ./configure $ make $ sudo make install
- OpenSSL – This package supports the HTTPS protocol, required by the NGINX SSL module and others.
$ wget http://www.openssl.org/source/openssl-1.1.1g.tar.gz $ tar -zxf openssl-1.1.1g.tar.gz $ cd openssl-1.1.1g $ ./Configure darwin64-x86_64-cc --prefix=/usr $ make $ sudo make install
Downloading Nginx Source Code
Nginx source files can be downloaded for both the stable and mainline versions from nginx.org.
- Mainline Version – To download and extract the source for the latest mainline version, run the following commands.
$ wget https://nginx.org/download/nginx-1.19.0.tar.gz $ tar zxf nginx-1.19.0.tar.gz $ cd nginx-1.19.0
- Stable Version – To download and extract source files for the latest stable version, run the following commands.
$ wget https://nginx.org/download/nginx-1.18.0.tar.gz $ tar zxf nginx-1.18.0.tar.gz $ cd nginx-1.18.0
Configuring Nginx Source Build Options
Configure options are specified with the ./configure script that sets up various NGINX parameters, including paths to source and configuration files, compiler options, connection processing methods, and the list of modules. The script finishes by creating the Makefile required to compile the code and install NGINX Open Source.
For help, you can see full list of up-to-date NGINX compile time options by running:
./configure --help # To see want core modules can be build as dynamic run: ./configure --help | grep -F =dynamic
The next step is to compile the package with the following command,
$ ./configure \ –user=nginx \ –group=nginx \ –prefix=/etc/nginx \ –sbin-path=/usr/sbin/nginx \ –modules-path=/usr/lib/nginx/modules \ –conf-path=/etc/nginx/nginx.conf \ –error-log-path=/var/log/nginx/error.log \ –http-log-path=/var/log/nginx/access.log \ –pid-path=/var/run/nginx.pid \ –lock-path=/var/run/nginx.lock \ –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-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 \ –with-http_xslt_module=dynamic \ –with-http_image_filter_module \ –with-http_image_filter_module=dynamic \ –with-http_geoip_module \ –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 \ –with-http_perl_module=dynamic \ –with-mail \ –with-mail=dynamic \ –with-mail_ssl_module \ –with-stream \ –with-stream=dynamic \ –with-stream_ssl_module \ –with-stream_realip_module \ –with-stream_geoip_module \ –with-stream_geoip_module=dynamic \ –with-stream_ssl_preread_module \ –with-google_perftools_module \ –with-cpp_test_module \ –with-compat \ –with-pcre \ –with-pcre-jit \ –with-zlib-asm=CPU \ –with-libatomic \ –with-debug \ –with-ld-opt=”-Wl,-E”
Note:- You might need to add ‘sudo’ before we run this command on Ubuntu.
Now run the below commands to complete the installation,
$ make $ make install
Note:- Please add ‘sudo’ for Ubuntu OS.
This will install the nginx from source on Linux. Now we need to create the startup service so that we can manage the service using systemctl.
Creating service file for Ubuntu
To create the service file Ubuntu, run the following command,
$ nano /lib/systemd/system/nginx.service
And add the following content to this file,
[Unit] Description=The NGINX HTTP server After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/var/run/nginx.pid ExecStartPre=/usr/sbin/nginx -t ExecStart=/usr/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
Save the file & exit. Now reload the daemon service to load the service file for nginx,
$ systemctl daemon-reload
Now we can start the nginx service,
$ systemctl start nginx
Creating service file for CentOS
Create the following file on CentOs,
$ vi /etc/systemd/system/nginx
& add the following content to the file,
[Unit] Description=The NGINX HTTP server After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/var/run/nginx.pid ExecStartPre=/usr/sbin/nginx -t ExecStart=/usr/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
Save the file & exit. Now reload the daemon service to load the service file for nginx,
$ systemctl daemon-reload
Now we can start the nginx service,
$ sudo systemctl start nginx
This completes our tutorial on how to install nginx from source. Please feel free to send in any questions or queries using the comment box below
Conclusion
Hence, you have learned here how to build NGINX from source on Ubuntu 20.04 and Centos 8.0.
Read Also : Install and Configure Kubernetes Cluster (k8s) on CentOS 8/RHEL 8