PageSpeed Optimization Libraries (PSOL) – Open source C++ libraries that are webserver-independent (if you can’t/won’t use the PageSpeed Modules for Apache/Nginx).

Caveats

Apache uses dynamic modules. Essentially, the core Apache binary is always present, and modules are loaded and unloaded by the administrator as needed, on the fly. This makes Apache very flexible, since a module can be loaded at any time (even after Apache is compiled/installed/running). However, Nginx doesn’t have this luxury. Nginx modules need to be included at compile-time. Some distributions (like Debian or Ubuntu) offer versions of Nginx that are pre-compiled with extra modules installed, but otherwise, you need to compile Nginx yourself (with your extra modules). The sections below will describe how to do this.

Prerequisites

You’ll need to install the necessary tools to compile software. In Ubuntu, this is the build-essential package.

sudo apt-get update && sudo apt-get install build-essential

This guide assumes you already have Nginx installed. If that’s the case, you’ll need to remove it. This shouldn’t remove your config files, but you should back them up to be sure.

sudo apt-get remove '^nginx.*'

Nginx build script

You can copy/paste each command below, but I wrote a quick script to compile Nginx with the PageSpeed Module, as well at the HeadersMore module (to add/remove HTTP headers). A script is useful in this scenario because you won’t be receiving Nginx updates from the regular repositories, so you’ll need to repeat this process each time a new version of Nginx (or your modules) come out. This script is essentially a modified version of the official Nginx build instructions. This script is tested on Ubuntu 16.04, and it should be executed as root (or using sudo).

The script is over at GitHub, but also pasted below (check GitHub first).

Init script

Since we’re compiling from source, we need to create an init script so that we can start/stop/restart Nginx from the command line (e.g., sudo systemctl stop nginx.service). Depending on which version of Ubuntu you’re running, you’ll need to choose an init script.

Ubuntu 14.10 and older use upstart, while Ubuntu 15.04 and newer use systemd. Check which init system you’re using with the command below.

readlink /sbin/init

Then, depending which you have, you’ll need to choose an init script. I’m using the init script that ships with the nginx-extras Ubuntu package, below.

Test Nginx

At this point, Nginx should be installed and you should be able to test it. Use the commands below to make sure the binary is in your PATH, check which Nginx version is installed, that your existing configurations are ok, and that systemd works. You should also visit your website to make sure it’s loading.

Enable PageSpeed

Now that Nginx is installed and working, we need to enable the PageSpeed Module. The first thing we’ll do is create a location to store files that PageSpeed creates, then give our Nginx user permission on that directory.

PageSpeed Filters

At the time of this writing PageSpeed offers 68 total filters. Each filter is a different adjustment that PageSpeed makes to your site. Since choosing which ones to use is not an easy task, PageSpeed offers three preconfigured options to make it easier to start: PassThrough, CoreFilters, and OptimizeForBandwidth.

PassThrough – turns PageSpeed off and ignores anything related to it

CoreFilters (34 filters) – the PageSpeed team considers this safe for most websites, and you shouldn’t have any problems with it

OptimizeForBandwidth (16 filters) – a more narrow selections of filters and provides a stronger guarantee of safety

In the example above, I’m using CoreFilters. In addition, I’ve enabled some extra filters, as shown below.