Build nginx with Pagespeed module
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.
Update the system and install necessary packages
sudo apt-get update && sudo apt-get -y upgrade
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 :
sudo add-apt-repository -y ppa:nginx/stable
Edit the PPA’s sources file and uncomment the deb-src directive.
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:
Install the Nginx build dependencies:
sudo apt-get build-dep nginx
cd to the /usr/local/src and download the Nginx source package:
cd /usr/local/src
sudo apt-get source nginx
Download and unpack the latest ngx_pagespeed and psol modules using the following commands:
cd nginx-1.8.1/debian/modules
NPS_VERSION=1.10.33.6
sudo wget https://github.com/pagespeed/ngx_pagespeed/archive/release-${NPS_VERSION}-beta.zip -O 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
Build Nginx with Pagespeed Module
Switch to the directory where you downloaded the Nginx source packages
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_flagsas shown bellow:
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:
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.
sudo dpkg-buildpackage -b
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:
cd /usr/local/src
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:
sudo mkdir -p /var/ngx_pagespeed_cache
sudo chown -R www-data: /var/ngx_pagespeed_cache
Create a new nginx configuration for the Pagespeed Module.
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.
sudo nginx -t
sudo service nginx restart
That's it. You have successfully installed