How to add pagespeed on existent NGINX - UBUNTU server?

Posted October 20, 2014 30.8k views

I have read many answers here, but my goal it is to install the pagespeed on ubuntu without make disasters. I have see that need to “reinstall” the nginx, but is it possible using the packages system? And which are the steps to do withotu to risk to lose the existing web portal?

Thank you very much and sorry for my poor english

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
3 answers

Unfortunately, the pagespeed module is not available from the official page repositories. In order to use it with Nginx, you’ll need to build Nginx from source. These directions are based on the ones from the ngx_pagespeed Google Code page, but they have been somewhat changed to be a better drop in replacement for the packaged version.

First install the build dependencies:

sudo apt-get install build-essential zlib1g-dev libpcre3 libpcre3-dev unzip

Then download ngx_pagespeed:

unzip release-${NPS_VERSION}
cd ngx_pagespeed-release-${NPS_VERSION}-beta/
tar -xzvf ${NPS_VERSION}.tar.gz  # extracts to psol/

Now we’ll build Nginx with the pagespeed module (note the configure arguments are different from the version provided by upstream):

# check for the latest version
tar -xvzf nginx-${NGINX_VERSION}.tar.gz
cd nginx-${NGINX_VERSION}/
./configure --add-module=$HOME/ngx_pagespeed-release-${NPS_VERSION}-beta \
  --prefix=/usr/local/share/nginx --conf-path=/etc/nginx/nginx.conf \
  --sbin-path=/usr/local/sbin --error-log-path=/var/log/nginx/error.log
sudo make install

It should now be installed and available. Before removing the old Nginx package, we need to do one more thing. Let’s copy the init script that comes with the package and modify it to work with the new version:

cp /etc/init.d/nginx /etc/init.d/nginx-pagespeed
sed -i 's|/usr/sbin/nginx|/usr/local/sbin/nginx|g' /etc/init.d/nginx-pagespeed

Now remove the package:

sudo apt-get remove nginx*

Finally, enable pagespeed by adding the following to /etc/nginx/nginx.conf

        #ngx_pagespeed module settings

        pagespeed on;
        pagespeed FileCachePath /var/ngx_pagespeed_cache;

And start it with:

service nginx-pagespeed start
  • Thank you very much asb!!! I will try end of the week… crossing fingers hehe

  • Thank you for the tutorial, asb! I stumbled upon Pagespeed yesterday and am wondering about the same thing! I have a question, though:

    Do we need to do all those steps again whenever we want to upgrade pagespeed and nginx? Let’s say hypotetically if pagespeed is updated to 1.10.1 and nginx is updated to 1.8.1. If I want to update both, can I just type these below or should I redo all the steps you laid down above?

    sudo apt-get update
    sudo apt-get dist-upgrade
  • @Okidoki Unfortunately, once you build and install it from source, it is outside of the package management system. So you would need to rebuild them all over again yourself. There seem to be a few PPAs that are providing packages of Nginx with pagespeed, but they are unofficial and I can’t personally vouch for their quality.

  • Hi ABC, i followed your guideline and it worked for me, but when i try to recompile with “mod_strip” module then i get error, here is command to recompile

    cd nginx-${NGINXVERSION}/
    ./configure –add-module=$HOME/ngxpagespeed-release-${NPSVERSION}-beta \
    –add-module= $HOME/modstrip \
    –prefix=/usr/share/nginx –conf-path=/etc/nginx/nginx.conf \
    –sbin-path=/usr/sbin –error-log-path=/var/log/nginx/error.log \

    And am getting the following error…
    ./configure: error: invalid option “/root/mod_strip”

    NOTE: i already have mod_strip in root dir.

    Any idea what wrong am doing here?

This is out of date; there’s now an automated installer for ngx_pagespeed: build_ngx_pagespeed_from_source

It’s also suggesting people build ngx_pagespeed version 1.9, which has unpatched security vulnerabilities in HTTPS fetching.