Building debian package for Nginx + pagespeed + passenger modules

Nginx, unlike Apache, doesn't support dynamically load additional modules. If we installed Nginx from distro's package repository, like installing for apt-get under Ubuntu, we cannot have additional nginx-somemodule package for easily installing more modules. If we need more than a standard Nginx, we have to remove the one installed with package manger, then build and install again from source. But installing via this method means we lose some advantage of package manager.

So, this tutorial will show you how to build and combine Google PageSpeed and Phusion Passenger in the same package as Nginx, to let it installed and controlled by Debian/Ubuntu's APT package manger.

Why building debian package?

  • Can revert to original Nginx by uninstall our deb package.
  • The Nginx include & config files are in standard location. Normally, when we re-build Nginx from source and install right away, the binaries and config files will be in non-standard location (/opt, /usr/local). This make difficult to find and adjust Nginx config, when you already get used to the location of the one come with Ubuntu and other tutorial on Internet also refer to those standard locations.
  • The Nginx installed from source cannot be started/stopped in standard way (sudo service start)

The debian package will install Nginx in proper location and make it usable with Ubuntu process init tools.

This tutorial is developed from http://www.devops.zone/webserver/installing-nginx-1-7-9-with-google-pagespeed-on-ubuntu-14-04-trusty/, with addition of Phusion Passenger module support. Note that we won't build deb package for Passenger itself, we only build its module for Nginx. So you still have to install Passenger separately by some way. This tutorial is specifically applied for the case that Ruby and Passenger are installed via RVM (I haven't tried Rbenv, or system-wide Ruby).

Install RVM, Ruby and Phusion Passenger

gem install passenger

Install building tools

We are adding repo and install dependencies as well as tools for later building

sudo add-apt-repository -s -y ppa:nginx/development
sudo apt-get update
sudo apt-get -y install build-essential zlib1g-dev libpcre3 libpcre3-dev unzip devscripts
sudo apt-get -y build-dep nginx

Get sources

Create and enter folder where we will work in

mkdir ~/Build
cd ~/Build

Get Nginx source

apt-get -y source nginx

Please note the version number of downloaded nginx. For me, at the time I'm writing this, it is 1.7.11. You can find it in the result of the command above.

Now go to the debian/modules subfolder of that source, we will download and place PageSpeed there (change the version number with yours):

cd nginx-1.7.11/debian/modules/

Before downloading PageSpeed, let's find its URL. Go to https://github.com/pagespeed/ngx_pagespeed, switch the view of source branch to the latest "Tags". At the time of this writing, it is "v1.9.32-3-beta". After you switch to latest "Tags", the link of the Download Zip button will change. Copy its URL. For me, it is https://github.com/pagespeed/ngx_pagespeed/archive/v1.9.32.3-beta.zip

Pick tag

Let's download, extract and delete the archive:

wget https://github.com/pagespeed/ngx_pagespeed/archive/v1.9.32.3-beta.zip
unzip v1.9.32.3-beta.zip && rm v1.9.32.3-beta.zip

In my case, the zip is extracted and create a folder named ngx_pagespeed-1.9.32.3-beta. This folder need to be noted to be used later.

We will change the build command in the debian/rules file, appending config flags to include PageSpeed and Passenger module.

Configure for building

Before that, let's determine the location of installed Passenger, by issuing this command:

passenger-config --root

for me, the result is:

/home/quan/.rvm/gems/ruby-2.2.0/gems/passenger-5.0.5

this one will be used below.

Looking for full_configure_flags := block in debian/rules file, change the last line of this block:

--add-module=$(MODULESDIR)/ngx_http_substitutions_filter_module

to

--add-module=$(MODULESDIR)/ngx_http_substitutions_filter_module --with-file-aio \
--add-module=$(MODULESDIR)/ngx_pagespeed-1.9.32.3-beta \
--add-module=/home/quan/.rvm/gems/ruby-2.2.0/gems/passenger-5.0.5/ext/nginx

Note that for the parameter to inlcude Passenger module, we use the subfolder ext/nginx under the location we get from passenger-config --root before.

Also note that using Passenger from passenger-config --root is a must, because the building process we do later will generate buildout/support-binaries/PassengerAgent under it, which is needed by Nginx-Passenger module.

We do similarly with extras_configure_flags := block.

Doing in full_configure_flags := and extras_configure_flags := means that the PageSpeed and Passenger support will be added to nginx-full and nginx-extras packages.

##Build

Now, cd to the root level of nginx source folder. If we still stay in where we started to download ngx_pagespeed, go to the root level by:

cd ../..

Run this command to build debian package:

debuild --preserve-envvar PATH  --preserve-envvar "rvm_*" -i -us -uc -b

Note that we add theses parameters to the normal command:

--preserve-envvar PATH  --preserve-envvar "rvm_*"

because we are using Ruby in RVM. These parameters are to tell debuild to keep the PATH and other environment variables related to RVM. These variables are needed by Passenger build script.

The *.deb files will be generated in the parent folder of nginx source tree.

Now we can install Nginx and configure PageSpeed, Passenger as normal.