Installing Nginx From Source on Debian Stretch

By | 2019-05-01

You may want a version of Nginx that is newer than available in the repositories. As of today (2019 May 1), the latest version is 1.16.0. The Nginx developers maintain a apt repository with the latest version. I recommend using the repository because it will be easier to keep Nginx up to date. The Nginx Linux packages page explains how to add their repository to your system and install Nginx from it. If you still wish to install from source, keep reading.

The process of installing Nginx from source on Debian has relatively few steps. They are

  • Install a some required packages
  • Download the Nginx source
  • Compile and Install Nginx
  • Create a SystemD unit file
  • Create a SystemD environment file
  • Test the installation
  • Configure Nginx
  • Enable Nginx to start at boot

Install Required Packages

Compiling and running Nginx 1.16.0 requires several packages from the repositories. Install them with this command:

root@debian:~# apt install \
gcc \
libssl-dev \
make \
libpcre3-dev \
zlib1g-dev \
libxml2-dev \
libxslt-dev \
libgd-dev \
libgeoip-dev \
libperl-dev

Download the Nginx Source

Use wget to download the Nginx source code:

root@debian:~# wget https://github.com/nginx/nginx/archive/release-1.16.0.tar.gz 

Extract the source code archive with tar:

root@debian:~# tar xf release-1.16.0.tar.gz

Compile Nginx

Change your working directory to the directory created by the archive file:

root@debian:~# cd nginx-release-1.16.0

The Building Nginx from Sources page on the official website provides descriptions of the options.

Configure the compile time options:

root@debian:~/nginx-release-1.16.0# auto/configure \
--with-pcre \
--prefix=/opt/nginx-1.16.0 \
--user=www-data \
--group=www-data \
--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 \
--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 \
--without-http_charset_module \
--with-http_perl_module \
--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

Compile and install Nginx:

root@debian:~/nginx-release-1.16.0# make
root@debian:~/nginx-release-1.16.0# make install

Create a SystemD Unit

If you are on a minimal system, chances are the only text editor available is vi. If this is the case and you do not want to install another editor, I wrote a short tutorial explaining the basics.

Create the unit file. Add the following to the file /etc/systemd/system/nginx-1.16.0.service:

[Unit]
Description=nginx 1.16.0
After=syslog.target network.target

[Service]
Type=forking
EnvironmentFile=/etc/default/nginx-1.16.0
ExecStart=/opt/nginx-1.16.0/sbin/nginx $CLI_OPTIONS
ExecReload=/opt/nginx-1.16.0/sbin/nginx -s reload
ExecStop=/opt/nginx-1.16.0/sbin/nginx -s quit

[Install]
WantedBy=multi-user.target

Load the unit file into SystemD:

root@debian:~/nginx-release-1.16.0# systemctl daemon-reload

Create an environment file for Nginx. Add the following to /etc/default/nginx-1.16.0:

# Command line options to use when starting nginx
#CLI_OPTIONS=""

Test Your Installation

Start Nginx:

root@debian:~# systemctl start nginx-1.16.0

root@debian:~# wget -o /dev/null -O - http://localhost | grep 'Thank you'
<p><em>Thank you for using nginx.</em></p>

Configure Nginx

Configuring Nginx is beyond the scope of this guide. See the official documentation for guidance.

Enable Nginx to Start at Boot

The following command will ensure Nginx starts automatically when the system boots:

root@debian:~# systemctl enable nginx-1.16.0

References