How to Build and Install PageSpeed as a Dynamic Module with Nginx (Debian/CentOS/RHEL)



ngx_pagespeed, or just pagespeed, is an Nginx module designed to optimize your site automatically by reducing the size of its resources and hence the time the clients’ browsers need to load it. If you are not acquainted with it already, please check its official site.

In this tutorial we are going to install pagespeed with Nginx as a dynamic module and I believe that Nginx is already installed on your server. Let’s begin;

Before installation we are going to execute some commands and need some tools, so follow the steps.

Step 1: Install required libs

For Ubuntu/Debian

$ sudo apt-get update
$ sudo apt-get install build-essential zlib1g-dev libpcre3 libpcre3-dev unzip uuid-dev
$ cd /opt

For CentOS/RHEL/Oracle/Fedora

$ sudo yum update -y
$ sudo yum install gcc-c++ pcre-devel zlib-devel make unzip libuuid-devel
$ cd /opt

Also create a directory for PageSpeed cache;

$ sudo mkdir -p /var/ngx_pagespeed_cache
$ chown –R nobody:nobody /var/ngx_pagespeed_cache

You can change the nobody:nobody for the chown command to the user/group that (if) you have configured for Nginx. Otherwie, default is “nobody”.

Step 2: Download Nginx Source

Download the exact version that is already installed on your server. First check the version.

$ nginx -v
nginx version: nginx/1.17.2

As the version installed is 1.17.2, thus we are going to download the same version

$ wget -qO - http://nginx.org/download/nginx-1.17.2.tar.gz | tar xzvf -

Step 3: Download the latest version of ngx_pagespeed

$ cd /opt/nginx-1.17.2/src/http/modules
$ wget -qO - https://github.com/apache/incubator-pagespeed-ngx/archive/v1.13.35.2-stable.tar.gz | tar xzvf -

Step 4: Download the PSOL library

It should have same version as of ngx_pagespeed, in this case 1.13.35.2-stable

$ cd /opt/nginx-1.17.2/src/http/modules/incubator-pagespeed-ngx-1.13.35.2-stable
$ wget https://dl.google.com/dl/page-speed/psol/1.13.35.2-x64.tar.gz
$ tar -xzvf 1.13.35.2-x64.tar.gz

Step 5: Configure the Source and compile

Now you are ready to compile and install Nginx along with the PageSpeed module. Hence, move back to the parent directory.

$ cd /opt/nginx-1.17.2

Here, it is important to use exact arguments for the compiling process that you used before during your Nginx installation (on the server previously). If you are not aware of that, don’t worry! You can execute the below command and get it.

$ Nginx –V
nginx version: nginx/1.17.2
built by gcc …
built with OpenSSL …
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx…

Copy these arguments and paste it at the end of the below command and replace [YOUR_ARGUMENTS] with your arguments.

$ sudo ./configure --add-dynamic-module=./src/http/modules/incubator-pagespeed-ngx-1.13.35.2-stable/ [YOUR_ARGUMENTS]

It is important to note the “–add-dynamic-module” here, it will ensure that pagespeed module will be installed dynamically and we will move the executable to our current directory of Nginx.

Once the configuration completes please execute the following commands.

$ sudo make
$ sudo make install

Now go back to your nginx config directory, in my case its (/etc/nginx/) and add the following line to the top of your main nginx configuration file i.e. ‘nginx.conf’.

load_module “modules/ngx_pagespeed.so”;

We will now add the ‘ngx_pagespeed.so’ to the ‘modules’ directory, thus execute;

$ cp objs/ngx_pagespeed.so /etc/nginx/modules/

Finally add the following configuration to your Nginx config file ‘http’ block.

pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;
pagespeed RewriteLevel CoreFilters;
pagespeed XHeaderValue "ngx_pagespeed";

For further configuration of pagespeed filters and whatever you need for your server, you can read about the PageSpeed Filters here.

Finally, add the below code within your “server” block of the main Nginx configuration file (nginx.conf)

location ~ “.pagespeed.([a-z].)?[a-z]{2}.[^.]{10}.[^.]+” { add_header “” “”; }

location ~* .(jpg|jpeg|png|gif|ico|css|js|pdf)$ {
expires 7d;
}

location ~ “^/pagespeed_static/” { }
location ~ “^/ngx_pagespeed_beacon$” { }

Reload your nginx.

$ sudo service nginx restart

Test your PageSpeed installation

$ curl -I -p http://localhost

You can replace localhost with your domain if you prefer.

Conclusion

That’s how you can install and build the PageSpeed module dynamically with pre-installed nginx server without any trouble of uninstalling Nginx etc. These steps are valid for any other module that you wish to install.

You can also read about installing ModSecurity as a dynamic module with Nginx and it also follows the same steps. Just don’t forget that you will have to maintain and re-install these packages by yourself when there is a new version.