In this tutorial, we will show you how to install and configure Nginx with the ngx_pagespeed module on Ubuntu 16.04 LTS. For those of you who didn’t know, PageSpeed (ngx_pagespeed) is an 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 the source 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 the step-by-step installation of Nginx with page speed module on a Ubuntu 16.04 (Xenial Xerus) server.
Prerequisites
- A server running one of the following operating systems: Ubuntu 16.04 (Xenial Xerus).
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
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 the 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 the terminal to compile 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 the 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 the ngx_pagespeed module, we also need the 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 the 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 the 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 the Nginx server block config file:
nano /etc/nginx/nginx.conf
Add the following pagespeed directives in the 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;
Finally, restart the Nginx service:
systemctl reload nginx
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
Congratulations! 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 check the official Nginx website.