--- title: Building debian package for Nginx + pagespeed + passenger modules date: 2015-03-29 20:02:59.088387 UTC --- [Nginx](http://nginx.org/), unlike [Apache](http://httpd.apache.org/), 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](https://developers.google.com/speed/pagespeed/) and [Phusion Passenger](https://www.phusionpassenger.com/) 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/](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](https://www.ruby-lang.org/) and Passenger are installed via [RVM](https://rvm.io/) (I haven't tried [Rbenv](http://rbenv.org/), or system-wide Ruby). ## Install RVM, Ruby and Phusion Passenger - Install RMV + Ruby following this: [https://rvm.io/rvm/install](https://rvm.io/rvm/install) - Install 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](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](https://github.com/pagespeed/ngx_pagespeed/archive/v1.9.32.3-beta.zip) ![Pick tag](http://i.imgur.com/jHQpEMP.png) 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.