Compiling Nginx with Google's Pagespeed module.

Compiling Google’s PageSpeed in Nginx

Compiling Nginx with Google's Pagespeed can be a daunting task, not anymore with this tutorial: from setting everything up to compilation and configuration.

Why is it important

If you want traffic your sites need to rank well in different search engines, and during the past few years your site loading speed has become a ranking factor. This is specially true since Google started its mobile first crawling policy.

One of the best tools to speed up your site is Google’s Pagespeed server mod available for both Nginx and Apache. Installing mod pagespeed is Apache is as easy as downloading a module, but in Nginx you have to compile it along with Nginx itself, and that can a daunting task (not anymore with this tutorial).

Note: compiling software in Linux isn’t hard, it’s basically just a three steps process. If you’ve never compiled software and want to read a short article about the theory on how to compile packages in Linux before trying to compile Nginx with PageSpeed, we recommend reading the following article: How to compile packages in Linux and its advantages.

Compiling Pagespeed in Nginx

Step 1: Choosing your Nginx and Pagespeed versions

First you need to choose your versions and where you’re going to compile it. In order to choose your versions go to Nginx Download site and Pagespeed’s release notes and check the latest version numbers (don’t download anything, you only care about the version numbers).

Once you’ve selected your versions we’ll set two variables with the values (this will prevent us from having to type the version numbers again and again).

We’re using the latest versions available at the moment of this writing, if you’re from the future just change these numbers for the latest ones.

NGINX_VERSION=1.19.0
PS_VERSION=1.13.35.2-beta

Now we’ll choose where we’re going to compile it, you can do it in your used directory, but Linux old conventions prefer doing it inside /usr/local/src/ (it’s just a convention)

Again, we’ll set a variable so we don’t need to keep writing the path again and again,

INSTALL_DIR=/usr/local/src/

Step 3: installing the packages you’ll need

This is very important, Nginx with Pagespeed will not compile if you don’t have uuid-dev installed in your site and will throw a configure error saying it can’t find the PSOL library.

Most of the stuff you’ll install next should already be in your system, like for example gcc, pcre, build-essential, etc. but, just in case you don’t have some of these, we’ll try installing them anyway (if you already have them your package manager will just ignore them)

For distros with APT like Debian or Ubuntu type this:

sudo apt-get install gcc pcre pcre-devel openssl openssl-devel gd gd-devel libssl-dev
sudo apt-get install build-essential zlibc zlib1g zlib1g-dev libpcre3 libpcre3-dev unzip uuid-dev

For distros with RPM like CentOS or Fedore type this:

sudo yum -y install gcc pcre pcre-devel openssl openssl-devel gd gd-devel libssl-dev
sudo yum -y install kernel-devel zlibc zlib1g zlib1g-dev libpcre3 libpcre3-dev unzip uuid-dev

Step 4: getting Pagespeed ready

Just copy paste these commands:

By the way, don’t worry if the URL for Nginx Pagespeed source is in github.com/apache/ for some reason Nginx’s Pagespeed version is in Apache’s Github. incubator-pagespeed-ngx IS for Nginx.

cd "$INSTALL_DIR"
sudo wget https://github.com/apache/incubator-pagespeed-ngx/archive/v${PS_VERSION}.zip
sudo unzip v${PS_VERSION}.zip

nps_dir=/usr/local/src/incubator-pagespeed-ngx-${PS_VERSION}
cd "$nps_dir"
NPS_RELEASE_NUMBER=${PS_VERSION/beta/}
NPS_RELEASE_NUMBER=${PS_VERSION/stable/}

psol_url=https://dl.google.com/dl/page-speed/psol/${NPS_RELEASE_NUMBER}.tar.gz
[ -e scripts/format_binary_url.sh ] && psol_url=$(scripts/format_binary_url.sh PSOL_BINARY_URL)

sudo wget ${psol_url}
sudo tar -xzvf $(basename ${psol_url})  # extracts to psol/

Explaining the process: first you download the Pagespeed for Nginx source code, unzip it, then you create a few variables needed to download and setup the correct PSOL version (Pagespeed’s Optimization Library) and then you download and extract PSOL in its corresponding directory.

Step 4: getting Nginx ready

cd "$INSTALL_DIR"
sudo wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz
sudo tar -xvzf nginx-${NGINX_VERSION}.tar.gz

Step 5: compiling everything

I based these ./configure parameters on Nginx’s default config for Debian, the only changes are passing a flag to configure Pagespeed and also made the –with-cc-opt flag version agnostic.

Were also compiling Pagespeed as a static module.

sudo ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' --add-module=$nps_dir ${PS_NGX_EXTRA_FLAGS} 

Installing everything

sudo make
sudo make install

Step 6: getting everything ready

If everything went OK and you didn’t have any compiling errors, Nginx with Pagespeed should be already installed in your system. Check it my running:

sudo nginx -V

Note: if for some reason the compilation process didn’t set the executable run:

sudo ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx

Setting up the cache directories

Creating the directories:

sudo mkdir /var/cache/nginx
sudo mkdir /var/cache/ngx_pagespeed_cache
sudo mkdir /var/log/pagespeed

Getting the directories ready:

sudo chmod 700 /var/cache/ngx_pagespeed_cache
sudo chown www-data:www-data /var/cache/nginx
sudo chown www-data:www-data /var/cache/ngx_pagespeed_cache

Configure Nginx as a service

Edit:

sudo vi /lib/systemd/system/nginx.service

And paste inside it:

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

Further troubleshooting

If you have Nginx already installed in your system and uninstalled it in order to compile your own, you may get a mask error when you try to start the service. Fix it with this command:

sudo systemctl unmask nginx.service

Starting nginx

sudo systemctl start nginx

Or via SysVinit if you’re in Ubuntu, CentOS, or Debian and using init.d scripts:

sudo service nginx start 

Configuring PageSpeed

Once compiled you’ll have to configure PageSpeed. This guide will teach you how to configure PageSpeed to its optimal performance.

Leave a Reply