Install ngx_pagespeed module with nginx on ubuntu 14.04

29 / Jun / 2014 by Prashant Sharma 3 comments

PageSpeed speeds up your site and reduces page load time. This open-source webserver module automatically applies web performance best practices to pages and associated assets (CSS, JavaScript, images) without requiring that you modify your existing content or workflow.Click here for more technical information .

By Default , nginx does not support dynamic loading of modules ,therefore we have to rebuild the Ngnix .

Please refer given below steps

Add Debian wheezy backports repository

open  “/etc/sources.list” file in you system and append the following two lines, at  the end of  file.

{.....}
deb http://ftp.de.debian.org/debian/ wheezy main
  deb-src http://ftp.de.debian.org/debian/ wheezy main

 

Update repositories package lists:

sudo apt-get update

Download backport source package & build dependencies

apt-get install dpkg-dev build-essential zlib1g-dev libpcre3 libpcre3-dev

Now create  a  folder in tmp directory to download the nginx source package into:

cd /tmp && mkdir pagespeed && cd pagespeed

Download the nginx sources and the build dependencies:

apt-get source nginx
apt-get build-dep nginx

Once nginx backport source package is downloaded , let’s add the ngx_pagespeed module and extract it under modules folder

cd pagespeed/nginx-1.4.6/debian/modules
wget https://github.com/pagespeed/ngx_pagespeed/archive/v1.7.30.1-beta.zip
unzip v1.7.30.1-beta.zip
cd ngx_pagespeed-1.7.30.1-beta/
wget  -xzvf 1.7.30.1.tar.gz

 

Now, you have added ngx_pagespeed module successfully to the modules folder of the nginx , source package ready to be build with the other nginx modules . Before you can do that , one important step is  pending .

Let’s edit the debian/rules file:

/tmp/pagespeed/nginx-1.4.6/debian/rules

 

In that file you will find four config.status sections (config.status.fullconfig.status.lightconfig.status.extrasconfig.status.naxsi). Add the line –add-module=$(MODULESDIR)/ngx_pagespeed \ to each of them, right before the $(CONFIGURE_OPTS) >$@ line, e.g. as follows:

module

Now build the custom nginx package

/tmp/pagespeed/nginx-1.4.6 && sudo dpkg-buildpackage -b

You can now let the package building tools do their work while you relax.

Once the command prompt appears again, you know that the building process is finished. Now the generated packages are located right outside the source package folder:

/tmp/pagespeed/ && ls  -l 

You will see something similar to the following:

md2

Install the custom nginx package

We can now install nginx with PageSpeed . As you can see i will install two more packages , Those two are package dependencies that you must install along any of the four nginx packages

dpkg -i nginx-full_1.4.6-1ubuntu3_amd64.deb nginx-common_1.4.6-1ubuntu3_all.deb nginx_1.4.6-1ubuntu3_all.deb

In case you encounter the following error message:

dpkg: dependency problems prevent configuration of nginx-common:
 nginx-common depends on init-system-helpers

then just execute the following command: which will fix any dependency problems you might you will face.

sudo apt-get -f install

Let’s check if the PageSpeed module was built successfully:Create a new file in /etc/apt/preferences.d and add the following lines in it and save

sudo nginx -V
nginx version: nginx/1.4.6 (Ubuntu)
built by gcc 4.8.2 (Ubuntu 4.8.2-19ubuntu1) 
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_geoip_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=/root/pagespeed/nginx-1.4.6/debian/modules/nginx-auth-pam --add-module=/root/pagespeed/nginx-1.4.6/debian/modules/nginx-dav-ext-module --add-module=/root/pagespeed/nginx-1.4.6/debian/modules/nginx-echo --add-module=/root/pagespeed/nginx-1.4.6/debian/modules/nginx-upstream-fair --add-module=/root/pagespeed/nginx-1.4.6/debian/modules/ngx_http_substitutions_filter_module --add-module=/root/pagespeed/nginx-1.4.6/debian/modules/ngx_pagespeed

Configrue ngx_pagespeed module

we need to create a folder, which will cache your websites’ files into:

sudo mkdir -p /var/ngx_pagespeed_cache

 sudo chown -R www-data:www-data /var/ngx_pagespeed_cache

After that open “nginx.conf” file

sudo vi /etc/nginx/nginx.conf

add the lines “pagespeed on “and pagespeed FileCachePath “/var/ngx_pagespeed_cache” before any vhosts:

pagespeed on;
        pagespeed FileCachePath /var/ngx_pagespeed_cache;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;

Now restart the nginx

/etc/init.d/nginx restart

we can configure PageSpeed individually or each vhost, e.g. like this and

vi /etc/nginx/sites-available/defaults

add the following lines

location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; }
location ~ "^/ngx_pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon" { }
location /ngx_pagespeed_statistics { allow 127.0.0.1; deny all; }
location /ngx_pagespeed_global_statistics { allow 127.0.0.1; deny all; }
location /ngx_pagespeed_message { allow 127.0.0.1; deny all; }
location /pagespeed_console { allow 127.0.0.1; deny all; }
}

To stop your custom nginx package from being upgraded  in the future by ‘apt’ with a more recent release of nginx,

Create a new file in /etc/apt/preferences.d and add the following lines in it and save

sudo vim /etc/apt/preferences.d/nginx
Package: nginx
Pin: version 1.4.6-1ubuntu3
Pin-Priority: 1001
 
Thanks
Prashant Sharma
FOUND THIS USEFUL? SHARE IT

comments (3)

  1. Akpan

    I try using it, it is compiled already.
    but when i check curl -D- factwide.com | less
    I can’t find X-Pagespeed there.

    Please help me.

    Reply
  2. RpR

    Hi,

    I’ve used your tutorial to compile nginx from the apt-source on a ubuntu server.
    I’ve gotten these files after the compilation:
    nginx-1.4.6
    nginx_1.4.6-1ubuntu3.1_all.deb
    nginx_1.4.6-1ubuntu3.1_amd64.changes
    nginx_1.4.6-1ubuntu3.1.debian.tar.gz
    nginx_1.4.6-1ubuntu3.1.dsc
    nginx_1.4.6.orig.tar.gz
    nginx-common_1.4.6-1ubuntu3.1_all.deb
    nginx-core_1.4.6-1ubuntu3.1_amd64.deb
    nginx-core-dbg_1.4.6-1ubuntu3.1_amd64.deb
    nginx-doc_1.4.6-1ubuntu3.1_all.deb
    nginx-extras_1.4.6-1ubuntu3.1_amd64.deb
    nginx-extras-dbg_1.4.6-1ubuntu3.1_amd64.deb
    nginx-full_1.4.6-1ubuntu3.1_amd64.deb
    nginx-full-dbg_1.4.6-1ubuntu3.1_amd64.deb
    nginx-light_1.4.6-1ubuntu3.1_amd64.deb
    nginx-light-dbg_1.4.6-1ubuntu3.1_amd64.deb
    nginx-naxsi_1.4.6-1ubuntu3.1_amd64.deb
    nginx-naxsi-dbg_1.4.6-1ubuntu3.1_amd64.deb
    nginx-naxsi-ui_1.4.6-1ubuntu3.1_all.deb

    Installed nginx-common_1.4.6-1ubuntu3.1_all.deb & nginx-full_1.4.6-1ubuntu3.1_amd64.deb
    But now when I try to apt-get upgrade -s I got:
    apt-get upgrade -s
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    Calculating upgrade… Done
    The following packages have been kept back:
    linux-generic linux-headers-generic linux-image-generic
    The following packages will be upgraded:
    nginx-full
    1 to upgrade, 0 to newly install, 0 to remove and 3 not to upgrade.
    Inst nginx-full [1.4.6-1ubuntu3.1] (1.4.6-1ubuntu3.1 Ubuntu:14.04/trusty-updates [amd64])
    Conf nginx-full (1.4.6-1ubuntu3.1 Ubuntu:14.04/trusty-updates [amd64]

    My nginx file in /etc/apt/preferences.d/nginx contains:
    Package: nginx
    Pin: version 1.4.6-1ubuntu3.1
    Pin-Priority: 1001

    But still the pinning isn’t working. What did I do wrong?

    Reply
  3. click for more info

    you are truly a just right webmaster. The web site loading velocity is incredible.

    It kind of feels that you’re doing any distinctive trick.
    Furthermore, The contents are masterpiece. you’ve performed a
    magnificent process in this topic!

    Reply

Leave a Reply to RpR Cancel reply

Your email address will not be published. Required fields are marked *