How to Compile Nginx with PageSpeed on Debian 8

February 26, 2020

This tutorial will teach you how to compile Nginx with ngx_pagespeed module on Debian 8 Jessie.

1 Backup Nginx Config Files

During the installation, your Nginx config files will be overriden. The following command will copy config files to current working directory. (Don’t left out the trailing dot. The dot is your current working directory.)

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

2. Add Nginx Repository

Import PGP key from Nginx.

sudo apt-key add nginx_signing.key

Edit source list file.

sudo vi /etc/apt/sources.list

Add the following two lines at the end of the file.

deb jessie nginx
deb-src jessie nginx

Save and close the file. Then update local package index.

sudo apt-get update

3. Download Nginx Source Package

Make a directory in your home folder to store Nginx source package, then cd to the new directory:

mkdir ~/nginx && cd ~/nginx

Download Nginx source package with the below command

sudo apt-get source nginx

4. Download ngx_pagespeed Source Package

Go to Github ngx_pagespeed download

cd ~

unzip it:

sudo apt-get install unzip
unzip v1.9.32.10-beta

cd to the newly-created directory:

cd ngx_pagespeed-

Download PageSpeed Optimization Libraries (psol) and extract it.

tar xvf

It will create a psol directory under ngx_pagespeed- directory.

5. Add ngx_pagespeed Module to Nginx Compilation Rules

Edit Nginx compilation rule file.

sudo vi ~/nginx/nginx-1.9.10/debian/rules

In this file you will see two configuration block override_dh_auto_build and configure_debug. In override_dh_auto_build, add the following line at the end. Replace <username> with your real username.


Please note that you need to append a backslash at the –with-ipv6 line, or –add-module will be ignored.

# some text left out.
     CFLAGS="" ./configure 
# some text left out.

Save and close the file.

6. Compile and Install

cd to the nginx source directory.

cd ~/nginx/nginx-1.9.10/

Install all the dependancies needed to build Nginx:

sudo apt-get build-dep nginx

Use the following command to build the deb package.

sudo dpkg-buildpackage -b

Wait a few minutes. My Debian server completed the task around 5 minutes. When it’s done, there will be two deb files in ~/nginx/ directory. One is the stable version and the other is a debug version. We need to install the stable version. If you have installed nginx before, it’s time to remove old version and then install the new version.

sudo apt-get remove nginx nginx-common nginx-full
sudo dpkg -i nginx_1.9.10-1~jessie_amd64.deb

Once installed, 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.


If your website is down right now, you need to restore your config files.

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 After it’s 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.

sudo mkdir -p /var/ngx_pagespeed_cache
sudo chown -R nginx:nginx /var/ngx_pagespeed_cache

Now open Nginx main config file /etc/nginx/nginx.conf

sudo vi /etc/nginx/nginx.conf

Add the following two lines in http block

pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;

Save and close the file. Then edit your server block file.

sudo vi /etc/nginx/conf.d/

Add the following lines to the server block.

location ~ ".pagespeed.([a-z].)?[a-z]{2}.[^.]{10}.[^.]+" { add_header "" ""; }
location ~ "^/ngx_pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon" { }

Save and close the file. Reload Nginx configuration.

sudo service nginx reload

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 is in 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 http://localhost| 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

If you prefer aptitude:

sudo aptitude hold nginx

Need help?

Do you need help setting up this on your own service?
Please contact us and we’ll provide you the best possible quote!