Compile Nginx with ngx_pagespeed Module on Ubuntu 16.04

ngx_pagespeed is an open-source Nginx module that automatically apply best practices to optimize website speed. In this tutorial, we will discuss how to compile Nginx with ngx_pagespeed module on Ubuntu 16.04 LTS.

1. Back up Nginx Configuration Files

Once Nginx is compiled and installed, your original Nginx config files will be overwritten. You can use the following command to back up the main config file and server block files to your home directory. The tilde represents your home directory.

cp /etc/nginx/nginx.conf /etc/nginx/sites-available/*.conf /etc/nginx/conf.d/*.conf ~

2. Add Official Nginx Repository

The tutorial will demonstrate compiling the latest Nginx mainline version, 1.11.1 at the time of this writing. The Ubuntu 16.04 repository has Nginx 1.10. We add the official Nginx repository as shown below.

First fetch the Nginx GPG key and import it to Ubuntu 16.04 system.


sudo apt-key add nginx_signing.key

Then edit the sources.list file.

sudo nano /etc/apt/sources.list

Add the following two lines at the end of this file. The deb-src line allows us to download Nginx source packages with apt source command.

deb xenial nginx
deb-src xenial nginx

Press Ctrl+O to save the file. Press Ctrl+X to close the file. Then update local package index.

sudo apt update

Now the Nginx official repository is added to Ubuntu 16.04.

3 Download Nginx Source Package

We will make a nginx directory under the home directory to store the Nginx sources and then cd into that directory.

mkdir ~/nginx && cd ~/nginx

Install dpkg-dev and download Nginx source package with the below command:

sudo apt install dpkg-dev
sudo apt source nginx

Check out the downloaded files.

ls ~/nginx/


nginx-1.11.1                               nginx_1.11.1-1~xenial.dsc
nginx_1.11.1-1~xenial.debian.tar.xz        nginx_1.11.1.orig.tar.gz

4. Download ngx_pagespeed Source Package

To compile Nginx with ngx_pagespeed module, we also need ngx_pagespeed source package. Go to Github ngx_pagespeed download page. ( ) Download the latest beta release to your home directory.

v1.11.33.2-beta at the time of this writting. You may need to change the version number.

cd ~


unzip it:

sudo apt-get install unzip

unzip v1.11.33.2-beta

cd to the newly-created directory:

cd ngx_pagespeed-

We also need to download the psol library. (PageSpeed Optimization Library) and extract it. The version number corresponds to the page speed version number.


tar xvf

It will create a psol directory under ngx_pagespeed- directory and that’s where it needs to be.

5. Add ngx_pagespeed Module to Nginx Compilation Rules

Edit Nginx compilation rule file.

sudo nano ~/nginx/nginx-1.11.1/debian/rules

At the end of COMMON_CONFIGURE_ARGS section, add the following line. Replace username with your actual username. This line specifies the location of ngx_pagespeed module.


This tutorial downloads the ngx_pagespeed source page to the home directory. If you use root account, then replace /home/username with /root. because the home directory for the root user is /root.

Also notice that you need to append a backslash at the --with-ld-opt line like the screenshot below. If you don’t, the –add-module line you added will be ignored when Nginx is being compiled.

ngx_pagespeed ubuntu 16.04

Now save and close the file.

6. Start the Compilation !

Make sure you are in the Nginx source directory.

cd ~/nginx/nginx-1.11.1/

Install all the needed dependencies to build our Nginx deb package.

sudo apt build-dep nginx

Now use the following command to build the deb package.

sudo dpkg-buildpackage -b

Take a cup of coffee and wait a few minutes. On a single-core OpenVZ VPS this build process took around 20 minutes. When it’s done, there will be 7 deb files in ~/nginx/ directory. We only need to install the nginx_1.11.1-1~xenial_amd64.deb or nginx_1.11.1-1~xenial_i386.deb package, depending on your OS architecture. The others are Nginx dynamic module package and a debug package. You can install them as well if you like.

If you have installed Nginx before, it’s time to remove the old version and then install the new version.

sudo apt remove nginx nginx-common nginx-full

cd ~/nginx

sudo dpkg -i nginx_1.11.1-1~xenial_amd64.deb


sudo dpkg -i nginx_1.11.1-1~xenial_i386.deb

Now let’s start Nginx.

sudo systemctl start nginx

If you see the following error message.

Failed to start nginx.service: Unit nginx.service is masked.

Then unmask nginx and issue the start command again.

sudo systemctl unmask nginx

Note that the Nginx process might run as user nginx or www-data. This can be changed by editing the first line in /etc/nginx/nginx.conf file. Just make sure Nginx run as the same user with PHP-FPM.

Now check the config arguments of Nginx.

sudo nginx -V

If you see the following line at the end then ngx_pagespeed module is successfully added to Nginx.


7. Enable ngx_pagespeed Module

PageSpeed is installed along with Nginx, but it’s disabled by default. Before enable it, I recommend you to test your website speed at or Pay attention to the the page size, number of requests, page load time, etc. After PageSpeed is enabled, do a test again so as to compare the two results.

Create a folder for pagespeed caches and change its ownership to Nginx user (www-data or nginx) so that it can be written by Nginx.

sudo mkdir -p /var/ngx_pagespeed_cache

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

Now edit Nginx server block config file.

sudo nano /etc/nginx/conf.d/your-domain.conf

Add the following pagespeed directives in server section.

# enable pagespeed module on this server block
pagespeed on;

# Needs to exist and be writable by nginx. Use tmpfs for best performance.
pagespeed FileCachePath /var/ngx_pagespeed_cache;

# 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 ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }

pagespeed RewriteLevel CoreFilters;

If you host multiple websites on a single server, add the above pagespeed directives to each server block config file to enable pagespeed on each of them.

The last directive set CoreFilters as the rewrite level. PageSpeed offers 3 rewrite levels: CoreFilter, PassThrough and OptimizeForBandwidth. CoreFilter is the default as it contains filters that are considered safe for most websites.

CoreFilter contains the following filters.


Save and close your server block config file. Then reload Nginx.

sudo systemctl reload nginx

8. Check if PageSpeed is Working

Go to your website. Refresh a few times then check your page source. Hit Ctrl+F key and search pagespeed. You will see that many of your website resource has been processed by pagespeed. Some css files and javascript files are combined into one file. If you use Google Chrome browser, you will see that pictures on your website has been converted to webp format. webp can greatly reduce image file size.

You can also find ngx_pagespeed is working by comparing your website speed test.

Also on you server you can issue the following command:

curl -I -p| grep X-Page-Speed

You will see X-Page-Speed and it’s version number.


9. Hold Nginx from Being Upgraded

If a newer version of Nginx is available in the repository, the apt-get upgrade command will upgrade Nginx by default and you ngx_pagespeed module will be gone. So we need to prevent Nginx from being upgraded. This can be achieved by the following command:

sudo apt-mark hold nginx

To show what packages are hold:

apt-mark showhold

Some pagespeed directives you might want to add in Nginx server block config files that are not in CoreFilters.

pagespeed EnableFilters collapse_whitespace;
pagespeed EnableFilters lazyload_images;
pagespeed EnableFilters insert_dns_prefetch;

ngx_pagespeed filters

Don’t forget to reload Nginx after you modify server block config files.

For a detailed explanation of each filter, go to Google PageSpeed Filter page.

Comments, questions or suggestions are always welcome. If you found this post useful, 🙂 please share it with your friends on social media! Stay tuned for more Linux tutorials.

Rate this tutorial
[Total: 3 Average: 4]
  • Blaine

    I had to add these two line to the `debian/rules` file to get it working.

    The error I was getting was:
    `error: no dependency information found for /usr/local/lib/`