Create a Shell Script to Install NGINX from Source On Ubuntu

Create a Shell Script to Install NGINX from Source On Ubuntu

Photo by Scott Blake on Unsplash

Why would you want to install NGINX from source code rather than a pre-built package? The most important reason is that the libraries which NGINX depends on (PCRE, zlib, OpenSSL) are part of the pre-built package, and building from source allows you to use the latest versions which may contain vital security patches.

For instance, you will find that the version of NGINX available through your package manager is several major versions behind the current mainline. Note, for Ubuntu systems, you can find pre-built packages which are much closer to the current mainline from Launchpad’s Personal Package Archive service.

Another reason to install from source is that NGINX can be configured in many different ways, and the only way to choose how it is configured is to install from source. When you install NGINX from a pre-built package, you are stuck with whatever set of modules are enabled. In this post, I will show how to include third-party modules and how to enable modules which are disabled by default.

If you just came here for the script, you can download the completed shell script from the gist linked below. This script installs the latest mainline NGINX version along with the latest versions of PCRE, zlib and OpenSSL libraries, and includes two useful third-party modules:

The rest of this post will explain in detail how to install NGINX from source. It does not match my shell script exactly since the script is designed to run non-interactively and uses variables. The steps below require user input and no variables are used.

Expected Results

These steps produce a .deb package which can be used to uninstall this version of NGINX via apt-get (the .deb package can also be used to install this customized version of NGINX on another system with the same architecture). You can find information on the checkinstall program which creates the .deb package here.

After NGINX is built and installed, all source code files are added to a .tar.gz archive file. This allows you to avoid downloading the source code again if you use the .deb package to install this version of NGINX on another system.

Install Prerequisites and Dependancies

This guide is written for Ubuntu, but the steps only need to be changed in minor ways to apply to other Linux distributions.

The steps also assume that you are logged in with a user account that has sudo access. If you are unsure what this means, please perform the steps in this guide from DigitalOcean.

If you are using a different Linux distribution the steps to create a user with sudo access will be similar, try a google search with your distro’s name and “root privileges”.

  1. To begin, add the Maxmind PPA to your list of sources. This PPA has much newer versions of the GeoIP libraries and databases than the default apt source:

    ~$ sudo add-apt-repository ppa:maxmind/ppa -y
    gpg: keyring `/tmp/tmpmku7ishg/secring.gpg' created
    gpg: keyring `/tmp/tmpmku7ishg/pubring.gpg' created
    gpg: requesting key DE742AFA from hkp server keyserver.ubuntu.com
    gpg: /tmp/tmpmku7ishg/trustdb.gpg: trustdb created
    gpg: key DE742AFA: public key "Launchpad PPA for MaxMind" imported
    gpg: Total number processed: 1
    gpg:               imported: 1  (RSA: 1)
    OK
  2. Update your system (Depending on your Ubuntu version and configuration, libgeoip1 may be updated from the PPA added in the previous step):

    ~$ sudo apt update && sudo apt upgrade -y
    #removed messages detailing update process
    The following packages will be upgraded:
      libgeoip1
    1 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
    Need to get 76.7 kB of archives.
    After this operation, 3,072 B disk space will be freed.
    #removed messages detailing upgrade process
  3. Remove any libraries which are no longer needed:

    ~$ sudo apt autoremove -y
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
  4. NGINX is written in C, so we need to install GCC, make and other tools:

    ~$ sudo apt install build-essential -y
    #removed messages detailing install process
  5. We are going to install a third-party GeoIP module, but in order to do so we must first install these libraries:

    ~$ sudo apt install libmaxminddb0 libmaxminddb-dev mmdb-bin -y
    #removed output messages detailing install process
  6. Install checkinstall:

    ~$ sudo apt install checkinstall -y
    #removed output messages detailing install process
  7. Install Uncomplicated Firewall (UFW) which makes it easier to manage rules for ports used by NGINX:

    ~$ sudo apt install ufw -y
    #removed output messages detailing install process

Prepare for Install

  1. Next, create a directory to store source files:

    ~$ sudo mkdir -p /opt/src_files && cd /opt/src_files
    /opt/src_files $

    PLEASE DO NOT USE A FOLDER IN YOUR USER’S HOME DIRECTORY. If you plan on using the .deb file to install your custom version of NGINX on another system, the source files must be located in the same path on each system. If you store the source files in a directory in this user’s home directory, you will only be able to use the .deb package on another system with a user who’s name is exactly the same.

  2. Download and extract the source code for the latest version of NGINX:

    /opt/src_files $ sudo wget http://nginx.org/download/nginx-1.15.6.tar.gz && \
    > sudo tar xzf nginx-1.15.6.tar.gz
    #removed messages detailing download progress
    2018-11-26 16:36:19 (2.98 MB/s) - ‘nginx-1.15.6.tar.gz’ saved [1025761/1025761]
  3. Download dependencies (PCRE, zlib and OpenSSL) and extract the source code:

    /opt/src_files $ sudo wget https://downloads.sourceforge.net/project/pcre/pcre/8.42/pcre-8.42.tar.gz && \
    > sudo tar xzf pcre-8.42.tar.gz
    #removed messages detailing download progress
    2018-11-26 16:40:14 (5.18 MB/s) - ‘pcre-8.42.tar.gz’ saved [2081413/2081413]
    /opt/src_files $ sudo wget http://zlib.net/zlib-1.2.11.tar.gz && \
    > sudo tar xzf zlib-1.2.11.tar.gz
    #removed messages detailing download progress
    2018-11-26 16:41:56 (2.80 MB/s) - ‘zlib-1.2.11.tar.gz’ saved [607698/607698]
    /opt/src_files $ sudo wget https://www.openssl.org/source/openssl-1.1.1a.tar.gz && \
    > sudo tar xzf openssl-1.1.1a.tar.gz
    #removed messages detailing download progress
    2018-11-26 16:42:56 (6.66 MB/s) - ‘openssl-1.1.1a.tar.gz’ saved [8350547/8350547]

    You can find the latest version for each library at the following locations:

    • https://www.pcre.org

      NGINX requires the original PCRE library, NOT PCRE2

    • http://zlib.net
    • https://www.openssl.org/source/

      NGINX can be built with any 1.x.x version of OpenSSL, but using the latest (1.1.1) is strongly recommended since this version includes suport for TLS 1.3, the latest and most secure version of the encryption protocol.

  4. Download third-party NGINX modules: cache purge and GeoIP2:

    Feel free to omit this step or substitute different third-party modules, depending on your website's requirements. In that case, you will need to modify the configure command in Step 13

    If you do not want to install any third-party modules, remove the two lines which start with ––add–module= (lines 49-50). If you are installing different modules, modify these lines to point to folders containing module source code.

    /opt/src_files $ sudo git clone --recursive https://github.com/FRiCKLE/ngx_cache_purge.git
    Cloning into 'ngx_cache_purge'...
    #removed messages detailing download progress
    /opt/src_files $ sudo git clone --recursive https://github.com/leev/ngx_http_geoip2_module.git
    Cloning into 'ngx_http_geoip2_module'...
    #removed messages detailing download progress

    The GeoIP module included with NGINX only works with v1 MaxMind database files. V2 database provide better info than v1, to use v2 files on NGINX you will need to install this module.

Build Configuration

  1. We are ready to begin building NGINX. Delete all .tar.gz files. After doing so, your directory should contain folders for each program/library downloaded in the previous steps. After confirming this is the case, navigate to the folder containing the NGINX source code:

    /opt/src_files $ sudo rm -rf *.tar.gz
    /opt/src_files $ ls -al
    total 32
    drwxr-xr-x  8 root root  4096 Nov 26 17:23 .
    drwxr-xr-x  3 root root  4096 Nov 26 16:34 ..
    drwxr-xr-x  8 1001  1001 4096 Nov  6 13:32 nginx-1.15.6
    drwxr-xr-x  4 root root  4096 Nov 26 17:20 ngx_cache_purge
    drwxr-xr-x  3 root root  4096 Nov 26 17:20 ngx_http_geoip2_module
    drwxr-xr-x 19 root root  4096 Nov 20 13:35 openssl-1.1.1a
    drwxr-xr-x  7 1169  1169 4096 Mar 20  2018 pcre-8.42
    drwxr-xr-x 14  501 staff 4096 Jan 15  2017 zlib-1.2.11
    /opt/src_files $ cd nginx-1.15.6
    /opt/src_files/nginx-1.15.6 $
  2. This is the most important step. With this ./configure command, you enable/disable modules and manage all configuration settings:

    The values below for OpenSSL, PCRE and zlib library folder paths (Line #18, 21 and 23, respectively) are valid for the actions performed in Step 10. If you saved the source code somewhere besides /opt/src_files, modify the config arguments to use the correct location.

     1 /opt/src_files/nginx-1.15.6 $ sudo ./configure \
     2 > --prefix=/usr/share/nginx \
     3 > --sbin-path=/usr/sbin/nginx \
     4 > --modules-path=/usr/lib/nginx/modules \
     5 > --conf-path=/etc/nginx/nginx.conf \
     6 > --error-log-path=/var/log/nginx/error.log \
     7 > --http-log-path=/var/log/nginx/access.log \
     8 > --pid-path=/var/run/nginx.pid \
     9 > --lock-path=/var/lock/nginx.lock \
    10 > --user=www-data \
    11 > --group=www-data \
    12 > --build=Ubuntu \
    13 > --http-client-body-temp-path=/var/lib/nginx/body \
    14 > --http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
    15 > --http-proxy-temp-path=/var/lib/nginx/proxy \
    16 > --http-scgi-temp-path=/var/lib/nginx/scgi \
    17 > --http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
    18 > --with-openssl=/opt/src_files/openssl-1.1.1a \
    19 > --with-openssl-opt=enable-ec_nistp_64_gcc_128 \
    20 > --with-openssl-opt=no-weak-ssl-ciphers \
    21 > --with-pcre=/opt/src_files/pcre-8.42 \
    22 > --with-pcre-jit \
    23 > --with-zlib=/opt/src_files/zlib-1.2.11 \
    24 > --with-compat \
    25 > --with-file-aio \
    26 > --with-threads \
    27 > --
    28 > --with-http_auth_request_module \
    29 > --with-http_dav_module \
    30 > --with-http_flv_module \
    31 > --with-http_gunzip_module \
    32 > --with-http_gzip_static_module \
    33 > --with-http_mp4_module \
    34 > --with-http_random_index_module \
    35 > --with-http_realip_module \
    36 > --with-http_slice_module \
    37 > --with-http_ssl_module \
    38 > --with-http_sub_module \
    39 > --with-http_stub_status_module \
    40 > --with-http_v2_module \
    41 > --with-http_secure_link_module \
    42 > --with-mail \
    43 > --with-mail_ssl_module \
    44 > --with-stream \
    45 > --with-stream_realip_module \
    46 > --with-stream_ssl_module \
    47 > --with-stream_ssl_preread_module \
    48 > --with-debug \
    49 > --add-module=../ngx_http_geoip2_module \
    50 > --add-module=../ngx_cache_purge \

    If you need to disable a module which is enabled by default, please read these instructions. All arguments of the form ––with*_module enable NGINX modules which are by default disabled. If you need to do the opposite (i.e. disable a module which by default is enabled when installing NGINX) (e.g. the fastcgi module), you would add ––without–http_fastcgi_module to the list of arguments.

    For more information on how you can customize your install with arguments supplied to the configure command, see the links below:

  3. When you are 100% confident that every requirement of your NGINX installation is satisfied, run the ./configure command. When the configuration is complete, you should see a report of the configuration settings that will be used to build NGINX.

     1 /opt/src_files/nginx-1.15.6 $ sudo ./configure 
     2 #removed configuration arguments
     3 #removed verification statements
     4 configuring additional modules
     5 adding module in ../ngx_http_geoip2_module
     6 checking for MaxmindDB library ... found
     7 + ngx_geoip2_module was configured
     8 adding module in ../ngx_cache_purge
     9 + ngx_http_cache_purge_module was configure
    10 creating objs/Makefile
    11
    12 Configuration summary
    13   + using threads
    14   + using PCRE library: /opt/src_files/pcre-8.42
    15   + using OpenSSL library: /opt/src_files/openssl-1.1.1a
    16   + using zlib library: /opt/src_files/zlib-1.2.11
    17
    18   nginx path prefix: "/usr/share/nginx"
    19   nginx binary file: "/usr/sbin/nginx"
    20   nginx modules path: "/usr/lib/nginx/modules"
    21   nginx configuration prefix: "/etc/nginx"
    22   nginx configuration file: "/etc/nginx/nginx.conf"
    23   nginx pid file: "/var/run/nginx.pid"
    24   nginx error log file: "/var/log/nginx/error.log"
    25   nginx http access log file: "/var/log/nginx/access.log"

    You should be able to confirm several of the configuration settings have been correctly defined:

    • Lines 4-9 confirm that the third-party modules downloaded in Step 11 (GeoIP2 and cache purge) are configured correctly (also, the MaxMind libraries installed in Step 5 were successfully located).
    • Lines 12-16 confirm that the versions of PCRE, zlib and OpenSSL downloaded in Step 10 will be used to build NGINX
    • Lines 18-25 confirm various settings such as where to store the NGINX binary, location of NGINX configuration files, location of log files, etc.
  4. After finalizing your configuration, build NGINX:

    /opt/src_files/nginx-1.15.6 $ sudo make
    #removed thousands of build statements
    #eventually, you should see a message like this:
    sed -e "s|%%PREFIX%%|/usr/share/nginx|" \
      -e "s|%%PID_PATH%%|/var/run/nginx.pid|" \
      -e "s|%%CONF_PATH%%|/etc/nginx/nginx.conf|" \
      -e "s|%%ERROR_LOG_PATH%%|/var/log/nginx/error.log|" \
      < man/nginx.8 > objs/nginx.8
    make[1]: Leaving directory '/opt/src_files/nginx-1.15.6'

    At this point, you are probably thinking that the next step is to run sudo make install. Doing so would produce a working install of NGINX, finely calibrated to our unique demands, but would deprive us of benefits that we have come to expect when we install a program using apt or any other package management product (pip, npm, brew, etc). We can achieve a few of these benefits by utilizing the checkinstall program that we installed in Step 6.

    checkinstall is a simple program which monitors the installation of files, and creates a Debian package from them. There are two primary benefits to using checkinstall instead of running make install:

    • You can easily remove the package with one step.
    • You can install the resulting package upon multiple machines.

    After building the binary, you are ready to use checkinstall to create a .deb package for your custom NGINX configuration.

Create Debian Install Package

  1. Run checkinstall and specify values for two flags:

    • --install=no will cause ONLY a .deb package to be created, the default behavior is to install the binary and create the .deb package.
    • -y will create the .deb package non-interactively, accepting the default value for all prompts that are normally presented to the user.
    /opt/src_files/nginx-1.15.6 $ sudo checkinstall --install=no -y
    #removed installation messages
    #eventually, you should see a message like this:
    **********************************************************************
    

    Done. The new package has been saved to

    /opt/src_files/nginx-1.15.6/nginx_1.15.6-1_amd64.deb You can install it in your system anytime using:

      dpkg -i nginx_1.15.6-1_amd64.deb
    

    **********************************************************************

  2. You can examine the contents of the .deb package to see the target location of all files that will be copied to your system during installation:

     1 /opt/src_files/nginx-1.15.6 $ dpkg --contents  nginx_1.15.6-1_amd64.deb
     2 drwxr-xr-x root/root         0 2018-11-27 12:24 ./
     3 drwxr-xr-x root/root         0 2018-11-27 12:24 ./etc/
     4 drwxr-xr-x root/root         0 2018-11-27 12:24 ./etc/nginx/
     5 #removed listing of files in nginx config folder
     6 drwxr-xr-x root/root         0 2018-11-14 21:43 ./usr/
     7 drwxr-xr-x root/root         0 2018-11-27 12:24 ./usr/share/
     8 drwxr-xr-x root/root         0 2018-11-27 12:24 ./usr/share/nginx/
     9 drwxr-xr-x root/root         0 2018-11-27 12:24 ./usr/share/nginx/html/
    10 #removed listing of files in default website root
    11 drwxr-xr-x root/root         0 2018-11-27 12:24 ./usr/share/doc/
    12 drwxr-xr-x root/root         0 2018-11-27 12:24 ./usr/share/doc/nginx/
    13 #removed listing of files in nginx documentation folder
    14 drwxr-xr-x root/root         0 2018-11-27 12:24 ./usr/sbin/
    15 -rwxr-xr-x root/root   4820568 2018-11-27 12:24 ./usr/sbin/nginx

    The folder paths listed in the .deb package are taken from values provided to the ./configure command in Step 13:

    • /etc/nginx is the root path for NGINX configuration files, this location is taken from the value --conf-path=/etc/nginx/nginx.conf (Step 13, Line #5)
    • /usr/share/nginx is the folder used to serve html content in the default configuration, this location is taken from the value --prefix=/usr/share/nginx (Step 13, Line #2)
    • /usr/sbin is the location of the NGINX binary and **/usr/sbin/nginx** is the binary file, this location (and the name of the binary itself) is taken from the value --sbin-path=/usr/sbin/nginx (Step 13, Line #3)

    The location used to store NGINX documentation (/usr/share/doc/nginx) is NOT controlled by the ./configure command. This directory was created because we ran checkinstall with the -y flag, answering "yes" to the prompt asking if you want to create the documentation directory. There are several values you can customize when creating the .deb package, please see the official documentation for more details.

Install and Configure NGINX

  1. We are finally ready to install NGINX using the .deb package:

    /opt/src_files/nginx-1.15.6 $ sudo dpkg -i nginx_1.15.6-1_amd64.deb
    (Reading database ... 56508 files and directories currently installed.)
    Preparing to unpack nginx_1.15.6-1_amd64.deb ...
    Unpacking nginx (1.15.6-1) over (1.15.6-1) ...
    Setting up nginx (1.15.6-1) ...

    Installing with the .deb package (rather than the usual sudo make install) allows our custom version of NGINX to be cleanly uninstalled with either command below:

    • sudo apt-get remove nginx
    • sudo dpkg ––remove nginx

    Now that the installation is complete, we are going to create an archive file from the source code we used to build NGINX.

  2. Move the .deb package to a new folder since we want to keep it separate from the source code files. Navigate to the location of the .deb file after you have moved it:

    I created a new folder, /opt/save, and moved the .deb file to this location. Feel free to use any folder other than the directory containing the source files.

    /opt/src_files/nginx-1.15.6 $ sudo mkdir -p /opt/save
    /opt/src_files/nginx-1.15.6 $ sudo mv nginx_1.15.6-1_amd64.deb /opt/save
    /opt/src_files/nginx-1.15.6 $ cd /opt/save
    /opt/save $
  3. Create a tar file from the directory containing the source code files. Modify the command accordingly if you used a different directory:

    /opt/save $ sudo tar -zcf nginx_1.15.6-src_files.tar.gz /opt/src_files
    tar: Removing leading `/' from member names
  4. The current folder contain two files: the .deb package and a .tar.gz file containing the downloaded source code. Make both files executable by all users:

    /opt/save $ sudo chmod 755 nginx*.*
    /opt/save $ ls -al
    total 186452
    drwxr-xr-x 2 root root      4096 Nov 28 17:43 .
    drwxr-xr-x 4 root root      4096 Nov 28 17:37 ..
    -rwxr-xr-x 1 root root   1841212 Nov 27 12:24 nginx_1.15.6-1_amd64.deb
    -rwxr-xr-x 1 root root 189067874 Nov 28 17:47 nginx_1.15.6-src_files.tar.gz
  5. For some reason NGINX does not create a folder at /var/lib/nginx even though this location is specified several times in our list of configure arguments in Step 13. Create this directory to ensure the config file passes verification in the next step:

    /opt/save $ sudo mkdir -p /var/lib/nginx
  6. The nginx -t command checks the nginx.conf file for syntax errors and performs serveral other helpful tests as well. It looks at all included config files and reports if there are any issues with permissions, file-not-found, etc. I recommend making a habit of running this command everytime you modify your web server's configuration to check for errors you may have introduced.

    Running the command at this point should report that the config file syntax is error-free and passes the verification tests:

    /opt/save $ sudo nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful

    Another usefu command is nginx -V. This will report the version number of NGINX as well as the version of OpenSSL that was used to build the binary. The values that were provided to configure the build (Step 13) are also reported:

    /opt/save $ sudo nginx -V
    nginx version: nginx/1.15.6 (Ubuntu)
    built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.10)
    built with OpenSSL 1.1.1a  20 Nov 2018
    TLS SNI support enabled
    configure arguments: --prefix=/usr/share/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/lock/nginx.lock
    --user=www-data --group=www-data --build=Ubuntu --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi
    --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi
    --with-openssl=/opt/src_files/openssl-1.1.1a --with-openssl-opt=enable-ec_nistp_64_gcc_128 --with-openssl-opt=no-weak-ssl-ciphers --with-pcre=/opt/src_files/pcre-8.42
    --with-pcre-jit --with-zlib=/opt/src_files/zlib-1.2.11 --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module
    --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module
    --with-http_realip_module --with-http_slice_module --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --with-http_v2_module
    --with-http_secure_link_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module
    --with-debug --add-module=../ngx_http_geoip2_module --add-module=../ngx_cache_purge
    --with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2'
    --with-ld-opt='-Wl,-z,relro -Wl,--as-needed'
  7. If your config file passes validation, create two folders as shown below which will contain config settings for the website(s) which this server is hosting:

    /opt/save $ sudo mkdir /etc/nginx/sites-available
    /opt/save $ sudo mkdir /etc/nginx/sites-enabled
  8. To easily manage the ports NGINX will use for HTTP/HTTPS traffic, create a UFW profile (feel free to use a different text editor than nano):

    /opt/save $ sudo nano /etc/ufw/applications.d/nginx

    Copy the text below into the file:

    [Nginx HTTP]
    title=Web Server (Nginx, HTTP)
    description=Small, but very powerful and efficient web server
    ports=80/tcp
    
    [Nginx HTTPS]
    title=Web Server (Nginx, HTTPS)
    description=Small, but very powerful and efficient web server
    ports=443/tcp
    
    [Nginx Full]
    title=Web Server (Nginx, HTTP + HTTPS)
    description=Small, but very powerful and efficient web server
    ports=80,443/tcp
    

    Save and close the file.

  9. Verify the UFW profiles for NGINX are available:

    /opt/save $ sudo ufw app list
    Available applications:
      Nginx Full
      Nginx HTTP
      Nginx HTTPS
      OpenSSH
  10. Create a systemd unit file for NGINX:

    /opt/save $ sudo nano /etc/systemd/system/nginx.service

    Copy the text below into the file. Verify the location of the PIDFile and NGINX binaries, these should match the settings specified in Step 13:

    [Unit]
    Description=A high performance web server and a reverse proxy server
    After=network.target
    
    [Service]
    Type=forking
    PIDFile=/run/nginx.pid
    ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
    ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
    ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
    ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
    TimeoutStopSec=5
    KillMode=mixed
    
    [Install]
    WantedBy=multi-user.target
    

    Save and close the file.

  11. Remove all source code files:

    /opt/save $ sudo rm -rf /opt/src_files
  12. Start and enable NGINX

    /opt/save $ sudo systemctl start nginx.service && sudo systemctl enable nginx.service
  13. Verify NGINX is running:

    /opt/save $ sudo systemctl status nginx.service
    ● nginx.service - A high performance web server and a reverse proxy server
       Loaded: loaded (/etc/systemd/system/nginx.service; enabled; vendor preset: enabled)
       Active: active (running) since Thu 2018-11-29 15:08:48 UTC; 9s ago
     Main PID: 3118 (nginx)
       CGroup: /system.slice/nginx.service
               ├─3118 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
               └─3119 nginx: worker process
    

    Nov 29 15:08:48 ip-10-10-0-225 systemd[1]: Starting A high performance web server and a reverse proxy server… Nov 29 15:08:48 ip-10-10-0-225 systemd[1]: Started A high performance web server and a reverse proxy server.

  14. Reboot your server to check NGINX starts automatically:

    /opt/save $ sudo shutdown -r now
  15. Log in after the reboot is complete, and verify NGINX is running:

    ~$ sudo systemctl status nginx.service
    ● nginx.service - A high performance web server and a reverse proxy server
       Loaded: loaded (/etc/systemd/system/nginx.service; enabled; vendor preset: enabled)
       Active: active (running) since Thu 2018-11-29 15:10:57 UTC; 1min 53s ago
      Process: 1150 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
      Process: 1083 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
     Main PID: 1159 (nginx)
        Tasks: 2
       Memory: 4.7M
          CPU: 31ms
       CGroup: /system.slice/nginx.service
               ├─1159 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
               └─1162 nginx: worker process
    

    Nov 29 15:10:56 ip-10-10-0-225 systemd[1]: Starting A high performance web server and a reverse proxy server… Nov 29 15:10:57 ip-10-10-0-225 systemd[1]: Started A high performance web server and a reverse proxy server.

Conclusion

Congratulations! You are now running the latest version of NGINX which includes the latest versions of the PCRE, zlib and OpenSSL libraries. Pre-built NGINX packages often include older versions of these libraries, which may contain high-severity bugs. By building NGINX from source, your web server contains the most up-to-date features of these libraries along with any bug fixes and security patches that have been released.