Nginx web server is one of the most famous web servers that needs hardly any introduction. It is a very fast lightweight application that can work not only as a web server but also as a cache server, HTTP proxy/reverse proxy, mail proxy as well as a load balancer.

In this tutorial, we will learn how to install Nginx from source on Linux i.e on Ubuntu (20.04 in particular) & CentOS  (7 in particular). Let’s first start by installing the pre-requisites first.

Recommended Read: How to install Nginx on CentOS/RHEL

Also Read:  How to install NGINX on Ubuntu


Pre-requisites

For Ubuntu

We need to install the following packages on the Ubuntu systems before we can compile 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 libgd2-xpm-dev libgeoip-dev libgoogle-perftools-dev libperl-dev libssl-dev libcurl4-openssl-dev libatomic-ops-dev

For CentOS

We need to install the following packages CentOS systems,

$ yum install gcc zlib-devel openssl-devel make pcre-devel libxml2-devel libxslt-devel libgcrypt-devel gd-devel perl-ExtUtils-Embed GeoIP-devel

Now we can move to install nginx from source packages. 


Install Nginx from source


The installation of nginx from source packages is similar for both Ubuntu & CentOS. So follow the steps mentioned below.

First, we need to download the latest stable package onto our systems (currently V-1.18.0). Download it using the following command,

$ wget https://nginx.org/download/nginx-1.18.0.tar.gz

Now extract the tar file, 

$ tar zxf nginx-1.18.0.tar.gz

$ cd nginx-1.18.0

The next step is to compile the package with the following command,

$ ./configure \

–user=root \

–group=root \

–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:- Here we have used ‘User’ & ‘Group’ as ‘root’ but we can create a dedicated user also & use that.

Note:- We might need to add ‘sudo’ before we run this command on Ubuntu.

Now run the following commands to complete the installation,

$ make

$ make install

Note:- 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,

$ vi /lib/systemd/system/nginx.service

& 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,

$ 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,

$ 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.

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter | Linkedin

TheLinuxGURUS are thankful for your continued support.