How To Add ngx_pagespeed to Nginx on Debian 8

Published on September 20, 2015
How To Add ngx_pagespeed to Nginx on Debian 8
Not using Debian 8?Choose a different version or distribution.
Debian 8


ngx_pagespeed, or just pagespeed, is an Nginx module designed to optimize your site automatically by reducing the size of its resources and hence the time the clients’ browsers need to load it. If you are not acquainted with it already, please check its official site.

This article will guide you through the installation and configuration of the pagespeed module for Nginx. It’s important to know that Nginx does not support Dynamic Loading of Modules available in other web servers such as Apache. Since Nginx doesn’t support this feature, you need to build Nginx from source to add the module.

Having your own custom package comes with one disadvantage — you are solely responsible for updating it when there is a new version. Take this into account when weighing the pros and cons of using ngx_pagespeed.


This guide has been written for Debian 8. An Ubuntu 14.04 version and a CentOS 7 version are available as well.

Before following this tutorial, please make sure you complete the following prerequisites:

Except otherwise noted, all of the commands that require root privileges in this tutorial should be run as a non-root user with sudo privileges.

Step 1 — Download the Source and Its Dependencies

Before anything else, we have to satisfy all the dependencies needed to run Nginx. For this purpose run the command:

  1. sudo apt-get build-dep nginx

Next, create a folder in your home directory to download the source package for Nginx:

  1. mkdir ~/custom-nginx

Change to this newly created directory:

  1. cd ~/custom-nginx

Then, download the Nginx source package in this directory with the command:

  1. sudo apt-get source nginx

To confirm we are on the same page, list the content of the folder ~/custom-nginx:

  1. ls ~/custom-nginx

The result should look like this:

Output of ls ~/custom-nginx
nginx-1.6.2 nginx_1.6.2-5.debian.tar.xz nginx_1.6.2-5.dsc nginx_1.6.2.orig.tar.gz

As you can see, the version of the Nginx source package is 1.6.2 at the time of writing this tutorial. To start adding the ngx_pagespeed module, you first need to go to the modules folder within the extracted folder nginx-1.6.2:

  1. cd nginx-1.6.2/debian/modules

In this directory, download the latest ngx_pagespeed source archive from its Github repository with the command:

  1. sudo wget https://github.com/pagespeed/ngx_pagespeed/archive/master.zip

Once the download completes, you will need the unzip utility to extract it. If you don’t already have unzip, install it with the command:

  1. sudo apt-get install unzip

After that extract the downloaded file with the command:

  1. sudo unzip master.zip

This will create a new directory called ngx_pagespeed-master inside your ~/nginx-1.6.2/debian/modules directory. For convenience rename this directory to just ngx_pagespeed with the command:

  1. sudo mv ngx_pagespeed-master ngx_pagespeed

Go inside the new ngx_pagespeed directory:

  1. cd ngx_pagespeed

From there, download the PageSpeed Optimization Libraries (psol) which are required for the compilation:

  1. sudo wget https://dl.google.com/dl/page-speed/psol/

If the link to the psol archive is not working at the time you are reading this article, just skip this step. If you are missing the libraries during the compilation in the next steps, you will see an error with updated instructions for how to get the package later.

Finally, extract the psol package inside the ~/custom-nginx/nginx-1.6.2/debian/modules/ngx_pagespeed directory:

  1. sudo tar -xzvf

Step 2 — Customize the Source

At this point you are ready to customize the compilation rules and include ngx_pagespeed in the installation. For this purpose edit the file ~/custom-nginx/nginx-1.6.2/debian/rules with your favorite editor:

  1. sudo nano ~/custom-nginx/nginx-1.6.2/debian/rules

There you have four different scenarios for building Nginx’s packages: common, full, light, and extras. As their names suggest, common contains the common Nginx files without a server, full includes a server with the most popular modules, light creates a server with only the essential modules, and extras is for a server with some extra fancy modules in it.

Let’s assume that you need a light Nginx setup plus ngx_pagespeed. Thus, add the end of the light_configure_flags configuration block add the line:


Please don’t forget to add a backslash (\) on the previous row. The whole configuration block should look like this:

light_configure_flags := \
                        $(common_configure_flags) \
                        --with-http_gzip_static_module \
                        --without-http_browser_module \
                        --without-http_geo_module \
                        --without-http_limit_req_module \
                        --without-http_limit_zone_module \
                        --without-http_memcached_module \
                        --without-http_referer_module \
                        --without-http_scgi_module \
                        --without-http_split_clients_module \
                        --without-http_ssi_module \
                        --without-http_userid_module \
                        --without-http_uwsgi_module \
                        --add-module=$(MODULESDIR)/nginx-echo \

You could add the same line to the other build scenarios too if you find a different Nginx setup more convenient.

Next, increase the source package version, since this will help you pin the package later. To achieve this, open the changelog file with a text editor:

  1. sudo nano ~/custom-nginx/nginx-1.6.2/debian/changelog

The first line of the changelog file represents the current package version (1.6.2-5) and the Debian distribution name (unstable). Add a custom tag such as pagespeed at the end of the version number preceded by a hyphen like this:

nginx (1.6.2-5-pagespeed) unstable; urgency=medium

Step 3 — Build and Install Nginx with Pagespeed Module

Now that you have customized the build to include the ngx_pagespeed module, you are ready to build Nginx.

Go to the directory ~/custom-nginx/nginx-1.6.2/ with the command:

  1. cd ~/custom-nginx/nginx-1.6.2/

From here, run the command to build the new custom Nginx packages:

  1. sudo dpkg-buildpackage -b

The build process takes around 10 minutes at most. If you are worried that you might be disconnected during this time you could try using screen as described in this article.

If you have followed all the instructions, the build process should complete without any errors. To find the new custom Nginx packages go one directory up to ~/custom-nginx/ with the command:

  1. cd ~/custom-nginx/

List the contents of the ~/custom-nginx/ directory:

  1. ls ~/custom-nginx/

You should find a lot of .deb packages. The ones you need are called nginx-common_1.6.2-5-pagespeed_all.deb (containing the common Nginx files) and nginx-light_1.6.2-5-pagespeed_amd64.deb (containing your custom light server). The pagespeed part may vary if you have specified a different custom tag in the changelog file.

To install your custom Nginx with pagespeed module run the command:

  1. sudo dpkg -i nginx-common_1.6.2-5-pagespeed_all.deb nginx-light_1.6.2-5-pagespeed_amd64.deb

Step 4 — Enable the Pagespeed Module

You now have Nginx installed. The next step is to enable the ngx_pagespeed module.

Before enabling the module, you have to create a folder, where it will cache the files for your website:

  1. sudo mkdir -p /var/ngx_pagespeed_cache

Make sure to change the ownership of this folder to the Nginx user so that the web server can store files in it:

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

Then, open the main Nginx configuration file nginx.conf in your favorite text editor like this:

  1. sudo nano /etc/nginx/nginx.conf

In this file add the following lines to the http block and save the changes:

# Pagespeed Settings

pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;

You can add these lines anywhere in the http block, but in our example, we are adding it to the end of the block.

This is how the /etc/nginx/nginx.conf file should look now:

http {

        # Basic Settings

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        # Pagespeed Settings

        pagespeed on;
        pagespeed FileCachePath /var/ngx_pagespeed_cache;

Also, you need to add pagespeed configuration lines to every server block file located in /etc/nginx/sites-available. For example, edit the /etc/nginx/sites-available/default file:

  1. sudo nano /etc/nginx/sites-available/default

Add the following to the server block:

#  Ensure requests for pagespeed optimized resources go to the pagespeed
#  handler and no extraneous headers get set.
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; }
location ~ "^/ngx_pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon" { }

The above pagespeed configuration lines ensure that pagespeed will optimize every site’s resources.

Finally, restart Nginx server for the changes to take effect:

  1. sudo service nginx restart

Step 5 — Test the Installation

To check if ngx_pagespeed module has been installed successfully, run the Nginx binary like this:

  1. sudo /usr/sbin/nginx -V

If the installation was successful, you should see the ngx_pagespeed module listed among the other modules:

nginx version: nginx/1.6.2 ... --add-module=/home/your_user/custom-nginx/nginx-1.6.2/debian/modules/ngx_pagespeed

The above doesn’t mean yet that the pagespeed is enabled and works for your site. To confirm this you can use curl, a tool and a library for client-side URL transfers. If you don’t have curl already installed, then install it with the command:

  1. sudo apt-get install curl

After that check for the X-Page-Speed header like this:

  1. curl -I -p http://localhost| grep X-Page-Speed

If the ngx_pagespeed module works fine, you should see it in the output along with its version:


If you don’t see this header, make sure that you have enabled pagespeed as per the instructions from the previous step.

Step 6 — Pin Your Custom Nginx Package

To prevent your custom Nginx package from being replaced in the future by apt with a more recent release of Nginx, you should pin (hold) your package from being upgraded by the following steps:

Create a new nginx file in /etc/apt/preferences.d:

  1. sudo nano /etc/apt/preferences.d/nginx

Then paste the following lines in it and save it:

Package: nginx-light
Pin: version 1.6.2-5-pagespeed
Pin-Priority: 1001

Please make sure to specify the Nginx package you have decided to use. In our example, it was nginx-light. Also, specify the exact version along with your custom tag like 1.6.2-5-pagespeed.


That’s how you can build Nginx with a custom module, pagespeed. These steps are valid for any other module that is not already available in Nginx. Furthermore, the whole process for installing a package from source is similar for other software packages you might need to customize. Just don’t forget that you will have to maintain and re-install these packages by yourself when there is a new version.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar


Default avatar
Tammy Fox


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

New to this still, so excuse me if my question is simple.

If performing this on a machine with nginx already installed and configured, will this cleanly replace the binaries and leave the config untouched? Naturally, I’ll make a backup either way.

Also, in the documentation for ngx_pagespeed, there is a script for installation.

"To automatically install dependencies and build the latest mainline version of nginx with the latest stable version of ngx_pagespeed, run:

bash <(curl -f -L -sS https://ngxpagespeed.com/install) \
     --nginx-version latest

" (source: https://www.modpagespeed.com/doc/build_ngx_pagespeed_from_source) Is this recommended?

Brilliant, thank you!

ngx_pagespeed should probably be installed from the latest-beta branch now:

wget https://github.com/pagespeed/ngx_pagespeed/archive/latest-beta.zip
unzip latest-beta.zip
mv ngx_pagespeed-latest-beta ngx_pagespeed

To download and extract the right version of the PSOL libraries you can now use these commands:

wget -Opsol.tar.gz $(./scripts/format_binary_url.sh PSOL_BINARY_URL)
tar xvf psol.tar.gz

If you use master.zip, make sure you have a look at config file to find out which psol version to use, otherwise you’ll get an error.

As of today for example, this won’t work:

wget https://dl.google.com/dl/page-speed/psol/

but this will:

wget https://dl.google.com/dl/page-speed/psol/

Why don’t you just use dotdeb repository?

ubuntu 14?

Running sudo dpkg-buildpackage -b fails with collect2: error: ld returned 1 exit status for me.

stdout: http://sleepanarchy.com/p/H7TQN7 stderr: http://sleepanarchy.com/p/z400xb

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel