There is a new version of this tutorial available for Ubuntu 20.04 (Focal Fossa).

How to Install nginx and google PageSpeed on Ubuntu 16.04 (Xenial Xerus)

Nginx (engine-x) is an open source and high-performance HTTP server, reverse proxy and IMAP/POP3 proxy server. The outstanding features of Nginx are stability, a rich feature set, simple configuration and low memory consumption. This tutorial shows how to build a Nginx .deb package for Ubuntu 16.04 from source that has Google PageSpeed module compiled in.

PageSpeed is a web server module developed by Google to speed up the website response time, optimize the returned HTML and reduce the page load time. ngx_pagespeed features include:

  • Image optimization: stripping meta-data, dynamic resizing, recompression.
  • CSS & JavaScript minification, concatenation, inlining, and outlining.
  • Small resource inlining.
  • Deferring image and JavaScript loading.
  • HTML rewriting.
  • Cache lifetime extension.

see more


  • Ubuntu Server 16.04 - 64 bit
  • root privileges

What we will do in this tutorial :

  1. Install the prerequisite packages.
  2. Installing nginx with ngx_pagespeed.
  3. Testing.

Install the build dependencies

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

Installing nginx with ngx_pagespeed

Step 1 - Add the nginx repository

Create a new repository file /etc/apt/sources.list.d/nginx.list with vim.

vim /etc/apt/sources.list.d/nginx.list

There you add the lines:

deb xenial nginx
deb-src xenial nginx

Save the file and exit the editor.

Add the key and update the repository:

sudo sudo apt-key adv --keyserver --recv-keys ABF5BD827BD9BF62
sudo apt-get update

Step 2 - Download nginx 1.10 from ubuntu repository

Create a new directory for the nginx source files and download the nginx sources with apt:

cd ~
mkdir -p ~/new/nginx_source/
cd ~/new/nginx_source/
apt-get source nginx

Sometimes, there is an error: 'packages cannot be authenticated'.

You can solve it by typing command below:

rm -rf /var/lib/apt/lists/
apt-get update

Next, install all dependencies to build the nginx package.

apt-get build-dep nginx

Step 3 - Download Pagespeed

Create a new directory for PageSpeed and download the PageSpeed source.

In this tutorial, we will use pagespeed 1.11.33.

mkdir -p ~/new/ngx_pagespeed/
cd ~/new/ngx_pagespeed/
unzip release-${ngx_version}

cd ngx_pagespeed-release-${ngx_version}-beta/
tar -xzvf ${ngx_version}.tar.gz

Step 4 - Configure nginx to build with Pagespeed

Go to the 'nginx_source' directory and edit the 'rules' file with vim.

cd ~/new/nginx_source/nginx-1.10.1/debian/
vim rules

Add the new line under 'COMMON_CONFIGURE_ARGS':

--add-module=../../ngx_pagespeed/ngx_pagespeed-release- \

Save and exit.

Enable pagespeed in Nginx

Step 5 - Build the nginx Ubuntu package and install it

Go to the nginx source directory and build nginx from source with the dpkg-buildpackage command:

cd ~/new/nginx_source/nginx-1.10.1/
dpkg-buildpackage -b

The nginx Ubuntu package will be saved under ~/new/ngix_source/. Once package building is complete, please look in the directory:

cd ~/new/ngix_source/

The Nginx Ubuntu package has been built.

And install nginx and modules deb with dpkg command.

dpkg -i *.deb


Step 1 - Testing with the Nginx Command

Run nginx -V  to check that the ngx_pagespeed module has been built into nginx.

nginx -V

Test nginx configuration

Step 2 - Testing with Curl Command

Go to nginx configuration directory.

cd /etc/nginx/conf.d/

and edit default virtual host configuration file.

vim default.conf

Paste configuration below to enable ngx_pagespeed.

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$" { }

Save and exit.

The Nginx vhost.

Next, test the nginx configuration file and make sure there is no error:

nginx -t

Restart nginx:

systemctl restart nginx

Finally, access the nginx web server with the curl command:

curl -I

Nginx with pagespeed has been installed.


The nginx web server there is a stable and fast open source http server that supports a variety of modules for content delivery optimization. One of these modules is the 'PageSpeed module' which is developed by google. Unlike apache, nginx modules are not dynamically loadable, so you have to select the desired modules when you build the nginx package.

Share this page:

4 Comment(s)