Including modules into Nginx deb package.

Usually in Ubuntu/Debian we install Nginxa with ppa:

$ sudo add-apt-repository ppa:nginx/stable
$ sudo apt-get update
$ sudo apt-get install nginx

However, if you need to add additional modules, in this case Pagespeed, you have to recompile Nginx with this module. It is possible to compile from source, however you lose a lot of additions prepared for Ubuntu/Debian         (ie. the startup script). There is a way to use deb package and compile necessary modules. Process looks like this:

  1. Get the sources/dependencies used to
  2. Add our modules to the ppa deb package
  3. Rebuild the packages
  4. Re-install Nginx

Get Pagespeed module

Before we start to create custom deb package, we'll first download the module files. Get the Pagespeed module (latest version is 1.9.32.6):

$ mkdir /opt/pagespeed
$ cd /opt/pagespeed
$ NPS_VERSION=1.9.32.6
$ sudo wget https://github.com/pagespeed/ngx_pagespeed/archive/release-${NPS_VERSION}-beta.zip
$ sudo unzip release-${NPS_VERSION}-beta.zip
$ cd ngx_pagespeed-release-${NPS_VERSION}-beta/
$ sudo wget https://dl.google.com/dl/page-speed/psol/${NPS_VERSION}.tar.gz
$ sudo tar -xzvf ${NPS_VERSION}.tar.gz  # extracts to psol/

Get Nginx sources

We have to make sure that the directive deb-src is uncommented:

$ cat /etc/apt/sources.list.d/nginx-stable-trusty.list
deb http://ppa.launchpad.net/nginx/stable/ubuntu trusty main
# deb-src http://ppa.launchpad.net/nginx/stable/ubuntu trusty main

Edit /etc/apt/sources.list.d/nginx-stable-trusty.list and uncomment the deb-src. The file should now look like this:

$ cat /etc/apt/sources.list.d/nginx-stable-trusty.list
deb http://ppa.launchpad.net/nginx/stable/ubuntu trusty main
deb-src http://ppa.launchpad.net/nginx/stable/ubuntu trusty main

Then run update:

$ sudo apt-get update

The next step is to download the packages necessary to manually create deb file.

$ sudo apt-get install -y dpkg-dev
$ sudo mkdir /opt/customnginx
$ cd /opt/customnginx

Get Nginx source files:

$ sudo apt-get source nginx

Install the build dependencies:

$ sudo apt-get build-dep nginx

Now we will add their custom modules. For this purpose we need to edit the file debian/rules from /opt/customnginx/nginx-1.8.0 folder. The ppa:nginx/stable has several versions of Nginx: common, light, full and extras. When you run apt-get install nginx, you're installing the full version. For this example, we'll simply edit the full version to add our modules. This assumes we'll keep using the full package as normal when installing Nginx. From the rules file, here's the full original version configuration file:

$ cat /opt/customnginx/nginx-1.8.0/debian/rules
...

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_spdy_module \
                        --with-http_sub_module \
                        --with-http_xslt_module \
                        --with-mail \
                        --with-mail_ssl_module \
                        --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 needed modules, in this case Pagespeed:

--add-module=/opt/pagespeed/ngx_pagespeed-release-1.9.32.6-beta

The file should look like this::

$ cat /opt/customnginx/nginx-1.8.0/debian/rules
...

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_spdy_module \
                        --with-http_sub_module \
                        --with-http_xslt_module \
                        --with-mail \
                        --with-mail_ssl_module \
                        --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=/opt/pagespeed/ngx_pagespeed-release-1.9.32.6-beta

Then we proceed to build the package:

$ cd /opt/customnginx/nginx-1.8.0
$ sudo dpkg-buildpackage -b

Installing

When the building completes the package will be available in the /opt/customnginx/. We need to use them for installation. We are interested in the full package

$ ls -la | grep full
nginx-full_1.8.0-1+trusty1_amd64.deb
nginx-full-dbg_1.8.0-1+trusty1_amd64.deb

The dgb isa a debug version, so wee'll use the nginx-full_1.8.0-1+trusty1_amd64.deb package.

$ cd /opt/customnginx
$ sudo dpkg --install nginx-full_1.8.0-1+trusty1_amd64.deb

Nginx is installed! We can use nginx -V to see which flags were used during compilation, letting us know which modules are installed:

$ nginx -V
nginx version: nginx/1.8.0
built with OpenSSL 1.0.1f 6 Jan 2014
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fPIE -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -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_spdy_module --with-http_sub_module --with-http_xslt_module --with-mail --with-mail_ssl_module --add-module=/opt/customnginx/nginx-1.8.0/debian/modules/nginx-auth-pam --add-module=/opt/customnginx/nginx-1.8.0/debian/modules/nginx-dav-ext-module --add-module=/opt/customnginx/nginx-1.8.0/debian/modules/nginx-echo --add-module=/opt/customnginx/nginx-1.8.0/debian/modules/nginx-upstream-fair --add-module=/opt/customnginx/nginx-1.8.0/debian/modules/ngx_http_substitutions_filter_module --add-module=/opt/pagespeed/ngx_pagespeed-release-1.9.32.6-beta

Thats all!

Comments