In this guide, we will show you how to build and install the latest stable nginx version with the ngx_pagespeed module on an Ubuntu 14.04 VPS. With the Pagespeed module you can combine and minify CSS and JavaScript, optimize and convert images, remove comments and whitespaces from HTML, and perform many other optimizations.
Login to your VPS via SSH
ssh user@vps_IP
Update the system and install necessary packages
To update your VPS and install all necessary dependencies, run:
[user]$ sudo apt-get update && sudo apt-get -y upgrade [user]$ sudo apt-get install software-properties-common nano wget dpkg-dev \ build-essential zlib1g-dev libpcre3 libpcre3-dev unzip
Download Nginx and Pagespeed Sources
Ubuntu 14.04 comes with Nginx version 1.4, to get the latest stable version of Nginx v1.8.1 we’ll need to add the Nginx PPA repository :
[user]$ sudo add-apt-repository -y ppa:nginx/stable
Edit the PPA’s sources file and uncomment the deb-src directive.
[user]$ sudo nano /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
and update the package lists from the repositories:
[user]$ sudo apt-get update
Install the Nginx build dependencies:
[user]$ sudo apt-get build-dep nginx
cd to the /usr/local/src
and download the Nginx source package:
[user]$ cd /usr/local/src sudo apt-get source nginx
download and unpack the latest ngx_pagespeed and psol modules using the following commands:
[user]$ cd nginx-1.8.1/debian/modules [user]$ NPS_VERSION=1.10.33.6 [user]$ sudo wget https://github.com/pagespeed/ngx_pagespeed/archive/release-${NPS_VERSION}-beta.zip -O release-${NPS_VERSION}-beta.zip [user]$ sudo unzip release-${NPS_VERSION}-beta.zip [user]$ cd ngx_pagespeed-release-${NPS_VERSION}-beta/ [user]$ sudo wget https://dl.google.com/dl/page-speed/psol/${NPS_VERSION}.tar.gz [user]$ sudo tar -xzvf ${NPS_VERSION}.tar.gz
Build Nginx with Pagespeed Module
Switch to the directory where you downloaded the Nginx source packages
[user]$ cd /usr/local/src/nginx-1.8.1
The PPA ppa:nginx/stable
we have previously added has multiple Nginx packages available: nginx-common, nginx-full, nginx-light, nginx-extras and nginx-doc. For this guide we will add the Pagespeed Module to the nginx-full package, to do that we need to edit the debian/rules
file and add the module to the full_configure_flags
as shown bellow:
[user]$ cat 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=$(MODULESDIR)/ngx_pagespeed-release-1.10.33.6-beta
If you want to use nginx-light instead of the nginx-full you should add the same line to the light_configure_flags
block.
Now we are ready to build Nginx using the following command:
[user]$ sudo dpkg-buildpackage -b
The build process will take a few minutes to complete.
Once the build process is complete, you will find few .deb files in the /usr/local/src/
directory.
[user]$ ls -1 /usr/local/src/ | grep .deb$
nginx_1.8.1-1+trusty0_all.deb nginx-common_1.8.1-1+trusty0_all.deb nginx-doc_1.8.1-1+trusty0_all.deb nginx-extras_1.8.1-1+trusty0_amd64.deb nginx-extras-dbg_1.8.1-1+trusty0_amd64.deb nginx-full_1.8.1-1+trusty0_amd64.deb nginx-full-dbg_1.8.1-1+trusty0_amd64.deb nginx-light_1.8.1-1+trusty0_amd64.deb nginx-light-dbg_1.8.1-1+trusty0_amd64.deb
We added the Pagespeed Module to the “full” package, so we will install that package including the nginx-common package:
[user]$ cd /usr/local/src [user]$ sudo dpkg -i nginx-full_1.8.1-1+trusty0_amd64.deb nginx-common_1.8.1-1+trusty0_all.deb
Once the installation is complete, to see what modules are installed run nginx -V
.
nginx version: nginx/1.8.1 built with OpenSSL 1.0.2g 1 Mar 2016 TLS SNI support enabled configure arguments: ... --add-module=/usr/local/src/nginx-1.8.1/debian/modules/ngx_pagespeed-release-1.10.33.6-beta ...
Create a directory for the cached files:
[user]$ sudo mkdir -p /var/ngx_pagespeed_cache [user]$ sudo chown -R www-data: /var/ngx_pagespeed_cache
Create a new nginx configuration for the Pagespeed Module.
[user]$ sudo nano /etc/nginx/pagespeed.conf
pagespeed on; pagespeed FileCachePath /var/ngx_pagespeed_cache; location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; } location ~ "^/pagespeed_static/" { } location ~ "^/ngx_pagespeed_beacon$" { } # Minify CSS # pagespeed EnableFilters combine_css,rewrite_css; # Minify JS # pagespeed EnableFilters combine_javascript,rewrite_javascript; # Remove comments and whitespace from HTML # pagespeed EnableFilters remove_comments,collapse_whitespace;
You can uncomment the filters you need for your website or add a new ones, for more info about Pagespeed filters please check the official PageSpeed documentation.
In every Nginx server block where you want to enable the PageSpeed Module add:
include /etc/nginx/pagespeed.conf;
Example:
server { listen 80; server_name your_website; root /path/to/website; include /etc/nginx/pagespeed.conf; # omitted code }
Restart your nginx service and you are ready to go.
[user]$ sudo nginx -t [user]$ sudo service nginx restart
Of course you don’t have to do any of this if you use one of our Linux VPS Hosting services, in which case you can simply ask our expert Linux admins to setup this for you. They are available 24×7 and will take care of your request immediately.
PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.
I’m trying to do this for naxsi and pagespeed on “mainline” v 1.11.1 but the source file is different, for example there’s no modules directory and there’s no “full” section in rules, and it won’t build. Any ideas?
The directory structure is the same as at the stable version , first you need to add the nginx development (v1.11.1) ppa
and then