prjct.net

Building Debian NGINX with Google PageSpeed support

A short summary of steps needed to grab the mainline NGINX source, add the PageSpeed module, build the deb and make sure it stays installed, using a package pin.

Adding the nginx.org repository

I prefer the mainline (1.9.x) NGINX builds and luckely these are available as debs. Create (as root) a file named /etc/apt/sources.list.d/nginx.list containing the following:

deb http://nginx.org/packages/mainline/debian/jessie nginx
deb-src http://nginx.org/packages/mainline/debian/ jessie nginx

You'll also need to download the signing key from: http://nginx.org/keys/nginx_signing.key to some place and install it:

sudo apt-key add nginx_signing.key

Optionally we can prefer the packages from nginx.org, by adding a package pin file called /etc/apt/preferences.d/nginx-full.pref:

Package: nginx nginx-full nginx-common nginx-light nginx-extras nginx-naxsi
Pin: origin nginx.org
Pin-Priority: 1050

But I prefer to hold the package for manual installation / updating.

Getting the source

Decide where you want to build the package and fetch the source file. Note that you have to use -t wheezy to obtain the correct source version. After obtaining the source file we make sure dependencies are installed:

apt-get source -t wheezy nginx
sudo apt-get build-dep nginx
sudo apt-get install build-essential devscripts debhelper libparse-debcontrol-perl zlib1g-dev libpcre3 libpcre3-dev unzip wget

Obtaining PageSpeed

We are going to add pagespeed as a Debian package module. Replace version numbers as appropriate.

cd nginx-1.9.4/debian/
mkdir modules && cd modules
wget https://github.com/pagespeed/ngx_pagespeed/archive/v1.9.32.6-beta.tar.gz
tar xvf v1.9.32.6-beta.tar.gz
cd ngx_pagespeed-1.9.32.6-beta/
wget https://dl.google.com/dl/page-speed/psol/1.9.32.6.tar.gz
tar -xzvf 1.9.32.6.tar.gz

Modifying the Debian rules file to include PageSpeed

We need to make a few modifications to debian/rules in your NGINX source directory, starting by adding a line MODULESDIR = $(CURDIR)/debian/modules as shown:

#!/usr/bin/make -f

#export DH_VERBOSE=1
MODULESDIR = $(CURDIR)/debian/modules
CFLAGS ?= $(shell dpkg-buildflags --get CFLAGS)
LDFLAGS ?= $(shell dpkg-buildflags --get LDFLAGS)

Next we need to include mod_pagespeed in the build process. Edit debian/rules and include the line --add-module=$(MODULESDIR)/ngx_pagespeed-1.9.32.6-beta \ under the override_dh_auto_build: and configure_debug: sections, as shown:

override_dh_auto_build:
  dh_auto_build
  mv objs/nginx objs/nginx.debug
  CFLAGS="" ./configure \
    --prefix=/etc/nginx \
    --sbin-path=/usr/sbin/nginx \
    --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 \
    --user=nginx \
    --group=nginx \
    --with-http_ssl_module \
    --with-http_realip_module \
    --with-http_addition_module \
    --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_random_index_module \
    --with-http_secure_link_module \
    --with-http_stub_status_module \
    --with-http_auth_request_module \
    --with-mail \
    --with-mail_ssl_module \
    --with-file-aio \
    $(WITH_SPDY) \
    --add-module=$(MODULESDIR)/ngx_pagespeed-1.9.32.6-beta \
    --with-cc-opt="$(CFLAGS)" \
    --with-ld-opt="$(LDFLAGS)" \
    --with-ipv6
  dh_auto_build

Next we add a small fix for the package control file, telling Debian our new package provides not only httpd, but also nginx-common, nginx-minimal and nginx-full. Open the file debian/control and locate the Package: nginx section. There change Provides as shown below:

Package: nginx
Architecture: any
Depends: ${misc:Depends}, ${shlibs:Depends}, lsb-base, adduser
Provides: httpd, nginx-minimal, nginx-full, nginx-common
Description: high performance web server
 nginx [engine x] is an HTTP and reverse proxy server, as well as
 a mail proxy server.

Building the source

Let's add a new entry to debian/changelog:

dch -i "Added mod_pagespeed"

This will create a well formatted entry in the debian/changes file.

Optionally you can do an optimized build specifically for your CPU. Be sure to only use this package locally!

export DEB_CFLAGS_APPEND="-O3 -march=native"

And finally we build the new package:

dpkg-buildpackage -b -us -uc

Installing the fresly built packages

sudo dpkg -i nginx_1.9.4-1~{YOURVER}_amd64.deb

To make sure your local version stays installed we set the package status to 'hold':

sudo apt-mark hold nginx
sudo dpkg --get-selections | grep nginx

That should be it!

If you want to check your version number and installed features you can run:

nginx -V 2>&1 | sed s/" --"/"\n\t--"/g

The bit of sed in there is just to format the output a bit nicer.

If you haven't worked with PageSpeed yet, have a look at the official configuration guide: https://developers.google.com/speed/pagespeed/module/configuration