How To Install Nginx With Ngx_Pagespeed Module on Ubuntu 16.04 LTS

PageSpeed (ngx_pagespeed) is a Nginx module created by Google to help Make the Web Faster by rewriting web pages to reduce latency and bandwidth. For the installation, we will need to compile nginx from souce with the PageSpeed module, as Nginx doesn’t support Dynamic module loading (DSO), unless you want to build your own rpm or deb files.

This article assumes you have at least basic knowledge of linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo’ to the commands to get root privileges. I will show you through the step by step installation nginx with pagespeed module on a Ubuntu 16.04 (Xenial Xerus) server.
Install Nginx With Ngx_Pagespeed Module on Ubuntu 16.04 LTS

Step 1. First make sure that all your system packages are up-to-date by running these following apt-get commands in the terminal.

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

Step 2. Add the Nginx repository.

Run the commands below in terminal to compiling the latest Nginx mainline version:

wget http://nginx.org/keys/nginx_signing.key
apt-key add nginx_signing.key

Create a new repository and edit the sources.list file:

nano /etc/apt/sources.list

Add the following two lines at the end of this file:

deb http://nginx.org/packages/mainline/ubuntu/ xenial nginx
deb-src http://nginx.org/packages/mainline/ubuntu/ xenial nginx

Step 3. Download Nginx from source package.

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

mkdir ~/nginx && cd ~/nginx
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

Step 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:

wget https://codeload.github.com/pagespeed/ngx_pagespeed/zip/v1.11.33.4-beta

Unzip into the current directory:

unzip v1.11.33.4-beta
cd ngx_pagespeed-1.11.33.4-beta/

Next, we also need to download the psol library. (PageSpeed Optimization Library) and extract it:

wget https://dl.google.com/dl/page-speed/psol/1.11.33.4.tar.gz
tar xvf 1.11.33.4.tar.gz

Step 5. Configure Nginx to build with Pagespeed.

First, edit Nginx compilation rule file:

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

Add the new line under ‘COMMON_CONFIGURE_ARGS’:

--add-module=/home/username/ngx_pagespeed-1.11.33.2-beta

Step 6. Start the Compilation Nginx Ubuntu package.

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

cd ~/nginx/nginx-1.11.1/
apt build-dep nginx
dpkg-buildpackage -b

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:

cd ~/nginx
dpkg -i nginx_1.11.1-1~xenial_amd64.deb

Now let’s start Nginx:

systemctl start nginx

Step 7. Configure ngx_pagespeed Module in Nginx.

Now edit Nginx server block config file:

nano /etc/nginx/nginx.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;

Step 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 or you can issue the following command:

curl -I -p http://y0ur-domain.com| grep X-Page-Speed

Congratulation’s! You have successfully installed nginx pagespeed. Thanks for using this tutorial for installing nginx with ngx_pagespeed module on Ubuntu 16.04 LTS (Xenial Xerus) system. For additional help or useful information, we recommend you to check the official Nginx web site.

Leave a Reply