Search form

Install the Latest Nginx From Source on Ubuntu 16.04

So you want to install Nginx from source on Ubuntu. However, you want Nginx to work like you had installed it with the package manager. This means the startup file (/etc/init.d/nginx), binary (/usr/sbin/nginx), config directory (/etc/nginx/) and log files (/var/log/nginx/) will all be found in the usual Debian/Ubuntu locations. Fortunately this is pretty easy to do using the Debian dpkg utility and Nginx repos.

To install Nginx from source here, we'll use the latest developmental version of Nginx (1.11.2 as of this writing) on Ubuntu 16.04. As an example, we'll also add the nginx-push-stream module to the base install.

Note: Before you install Nginx from source, you might review how to recompile Nginx installed using the package manager.

Set Up the Prerequisites

Add the Nginx development/mainline repo:

 
root@ubuntu:/# add-apt-repository ppa:nginx/development

Edit /etc/apt/sources.list.d/nginx-ubuntu-development-xenial-list and uncomment the source line:

 
deb-src http://ppa.launchpad.net/nginx/development/ubuntu xenial main

Save the repo file, then grab the Nginx preqrequisites and source:

 
root@ubuntu:/# apt-get update
root@ubuntu:/# apt-get build-dep nginx
root@ubuntu:/# cd /opt
root@ubuntu:/# apt-get source nginx

The Nginx source downloads accordingly:

 
Reading package lists... Done
NOTICE: 'nginx' packaging is maintained in the 'Git' version control system at:
git://anonscm.debian.org/collab-maint/nginx.git
Please use:
git clone git://anonscm.debian.org/collab-maint/nginx.git
to retrieve the latest (possibly unreleased) updates to the package.
Need to get 1,929 kB of source archives.
Get:1 http://ppa.launchpad.net/nginx/development/ubuntu xenial/main nginx 1.11.2-0+xenial0 (tar) [925 kB]
Get:2 http://ppa.launchpad.net/nginx/development/ubuntu xenial/main nginx 1.11.2-0+xenial0 (diff) [1,001 kB]
Get:3 http://ppa.launchpad.net/nginx/development/ubuntu xenial/main nginx 1.11.2-0+xenial0 (dsc) [2,765 B]
Fetched 1,929 kB in 5s (346 kB/s)
gpgv: Signature made Mon 11 Jul 2016 09:28:22 AM CDT using RSA key ID F569EF55
gpgv: Can't check signature: public key not found
dpkg-source: warning: failed to verify signature on ./nginx_1.11.2-0+xenial0.dsc
dpkg-source: info: extracting nginx in nginx-1.11.2
dpkg-source: info: unpacking nginx_1.11.2.orig.tar.gz
dpkg-source: info: unpacking nginx_1.11.2-0+xenial0.debian.tar.gz
dpkg-source: info: applying perl-use-dpkg-buildflags.patch
W: Can't drop privileges for downloading as file 'nginx_1.11.2.orig.tar.gz' couldn't be accessed by user '_apt'. - pkgAcquire::Run (13: Permission denied)

The 'W: Can't drop privileges' warning here is a known Apt bug that doesn't affect successful delivery of our Nginx code. If we inspect the newly-created nginx-1.11.2 directory, we'll see the debian subdirectory containing our rules file and modules:

 
root@ubuntu:/opt/nginx-1.11.2# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  debian  html  LICENSE  man  README  src

Also, doing git clone git://anonscm.debian.org/collab-maint/nginx.git as suggested above gives you the latest patch level of the stable branch -- 1.10.1 as of this writing. Which may be what you want, but here we want the bleeding-edge mainline/development version.

Next, we put our push-stream module in the Debian modules directory:

 
root@ubuntu:/opt/nginx-1.11.2/debian/modules# git clone http://github.com/wandenberg/nginx-push-stream-module.git

Edit our rules file at /opt/nginx-1.11.2/debian/rules to reference our new module:

 
full_configure_flags := \
                       $(common_configure_flags) \
                       --with-http_addition_module \
                       --with-http_dav_module \
                       --with-http_geoip_module \
                       --with-http_gunzip_module \
                       --with-http_gzip_static_module \
                       --with-http_image_filter_module \
                       --with-http_v2_module \
                       --with-http_sub_module \
                       --with-http_xslt_module \
                       --with-stream \
                       --with-stream_ssl_module \
                       --with-mail \
                       --with-mail_ssl_module \
                       --with-threads \
                       --add-module=$(MODULESDIR)/nginx-auth-pam \
                       --add-module=$(MODULESDIR)/nginx-dav-ext-module \
                       --add-module=$(MODULESDIR)/nginx-echo \
                       --add-module=$(MODULESDIR)/nginx-upstream-fair \
                       --add-module=$(MODULESDIR)/ngx_http_substitutions_filter_module \
                       --add-module=$(MODULESDIR)/nginx-push-stream-module    <-----------

Install Nginx From Source

Build our .deb packages:

 
root@ubuntu:/opt/nginx-1.11.2/# dpkg-buildpackage -uc -b

After the build has completed, we can look in /opt and see all our .deb packages:

 
root@ubuntu:/opt# ls -al
total 15632
drwxr-xr-x  4 root root    4096 Aug  5 14:52 .
drwxr-xr-x 23 root root    4096 Aug  5 14:18 ..
drwxr-xr-x 10 root root    4096 Aug  5 14:48 nginx-1.11.2
-rw-r--r--  1 root root   24702 Aug  5 14:52 nginx_1.11.2-0+xenial0_all.deb
-rw-r--r--  1 root root    3755 Aug  5 14:52 nginx_1.11.2-0+xenial0_amd64.changes
-rw-r--r--  1 root root 1001234 Jul 11 10:48 nginx_1.11.2-0+xenial0.debian.tar.gz
-rw-r--r--  1 root root    2765 Jul 11 10:48 nginx_1.11.2-0+xenial0.dsc
-rw-r--r--  1 root root  924979 Jul 11 10:48 nginx_1.11.2.orig.tar.gz
-rw-r--r--  1 root root   44842 Aug  5 14:52 nginx-common_1.11.2-0+xenial0_all.deb
-rw-r--r--  1 root root   36188 Aug  5 14:52 nginx-doc_1.11.2-0+xenial0_all.deb
-rw-r--r--  1 root root  658394 Aug  5 14:52 nginx-extras_1.11.2-0+xenial0_amd64.deb
-rw-r--r--  1 root root 5991190 Aug  5 14:52 nginx-extras-dbg_1.11.2-0+xenial0_amd64.deb
-rw-r--r--  1 root root  511026 Aug  5 14:52 nginx-full_1.11.2-0+xenial0_amd64.deb
-rw-r--r--  1 root root 3988500 Aug  5 14:52 nginx-full-dbg_1.11.2-0+xenial0_amd64.deb
-rw-r--r--  1 root root  340332 Aug  5 14:52 nginx-light_1.11.2-0+xenial0_amd64.deb
-rw-r--r--  1 root root 2442640 Aug  5 14:52 nginx-light-dbg_1.11.2-0+xenial0_amd64.deb

Now that we have the Nginx full .deb package, we need to install it. First, install the common dependency:

 
root@ubuntu:/opt# dpkg --install nginx-common_1.11.2-0+xenial0_all.deb

Then install the Nginx full version (not the debug/dbg version):

 
root@ubuntu:/opt# dpkg --install nginx-full_1.11.2-0+xenial0_amd64.deb

Using .deb packages isn't the only way to install Nginx from source, of course. You could run ./configure - make - make install from the /opt/nginx-1.11.2 directory using the --add-module flag for push-stream, but you won't get all the Debian-specific setup rules in the debian/rules file. Instead you'd have to specify them as compiler flags when you run ./configure.

Inspect the Results

If everything goes right, we've installed the latest mainline version of Nginx from source, in a Debian/Ubuntu-specific setup, outside the control of the Ubuntu package manager.

 
root@ubuntu:/# which nginx
/usr/sbin/nginx

Our startup file is in /etc/init.d/ :

 
root@ubuntu:/# ls -al /etc/init.d/nginx
-rwxr-xr-x 1 root root 4579 Jan 26  2016 /etc/init.d/nginx

Our config files are in /etc/nginx/ :

 
root@ubuntu:/# ls -al /etc/nginx
total 64
drwxr-xr-x  6 root root 4096 Aug  5 14:56 .
drwxr-xr-x 88 root root 4096 Aug  5 14:56 ..
drwxr-xr-x  2 root root 4096 Aug  5 14:52 conf.d
-rw-r--r--  1 root root 1077 Jan 26  2016 fastcgi.conf
-rw-r--r--  1 root root 1007 Jan 26  2016 fastcgi_params
-rw-r--r--  1 root root 2837 Jan 26  2016 koi-utf
-rw-r--r--  1 root root 2223 Jan 26  2016 koi-win
-rw-r--r--  1 root root 3957 Jan 26  2016 mime.types
-rw-r--r--  1 root root 1462 Jan 26  2016 nginx.conf
-rw-r--r--  1 root root  180 Jan 26  2016 proxy_params
-rw-r--r--  1 root root  636 Jan 26  2016 scgi_params
drwxr-xr-x  2 root root 4096 Aug  5 14:56 sites-available
drwxr-xr-x  2 root root 4096 Aug  5 14:56 sites-enabled
drwxr-xr-x  2 root root 4096 Aug  5 14:56 snippets
-rw-r--r--  1 root root  664 Jan 26  2016 uwsgi_params
-rw-r--r--  1 root root 3071 Jan 26  2016 win-utf

Our log files are in /var/log/nginx :

root@ubuntu:/# ls -al /var/log/nginx
total 16
drwxr-x--- 2 www-data adm    4096 Aug  5 14:57 .
drwxrwxr-x 8 root     syslog 4096 Aug  5 14:56 ..
-rw-r--r-- 1 root     root    673 Aug  5 15:26 access.log
-rw-r--r-- 1 root     root    965 Aug  5 14:57 error.log

And, finally, we verify our version and compilation flags:

 
root@ubuntu:/etc/init.d# nginx -V
nginx version: nginx/1.11.2
built with OpenSSL 1.0.2g-fips  1 Mar 2016
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security 
-Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' 
--prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log 
--error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid 
--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-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module 
--with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module 
--with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module 
--with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module 
--with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threads 
--add-module=/opt/nginx-1.11.2/debian/modules/nginx-auth-pam 
--add-module=/opt/nginx-1.11.2/debian/modules/nginx-dav-ext-module 
--add-module=/opt/nginx-1.11.2/debian/modules/nginx-echo 
--add-module=/opt/nginx-1.11.2/debian/modules/nginx-upstream-fair 
--add-module=/opt/nginx-1.11.2/debian/modules/ngx_http_substitutions_filter_module 
--add-module=/opt/nginx-1.11.2/debian/modules/nginx-push-stream-module

Recompile Nginx from source

Categories: