11.12.2024

How to Set Up Google PageSpeed Module in Nginx on Ubuntu 20.04

The loading speed of web site pages affects the user experience and the position of the site in search engines results. To analyze and speed up the loading of content, there is a Google PageSpeed module. In this tutorial, we will set up the Google PageSpeed Module in Nginx on Ubuntu 20.04.

In the Serverspace you can create a server with already installed app "Nginx".

Preparing to install the Google PageSpeed module

First, you must have the Nginx web server installed. If you don't have it for some reason, you can install it using the following command:

apt install nginx

You also need the curl package to download the installation script and SSL libraries.

apt install curl libssl-dev

Check which version of Nginx and modules are installed.

nginx -V

Output:

nginx version: nginx/1.18.0 (Ubuntu)
built with OpenSSL 1.1.1f 31 Mar 2020
TLS SNI support enabled
configure arguments:
...

The same version is best specified in the following command to minimize the risk when rebuilding Nginx. Here you can see which modules are installed now and add the necessary ones during the installation process.

Installing the Google PageSpeed module

In the case of Nginx, the PageSpeed module must be built from source. In addition, all dependencies must be installed. To simplify all these processes, the developers suggest using an installation script that will do all this. Nevertheless, it is better to test the operation of sites with this module after rebuilding Nginx on a test server and only then install it on a production server. Use the following command to rebuild Nginx with the Google PageSpeed module. Specify the Nginx version installed on the system in the --nginx-version parameter.

bash <(curl -f -L -sS https://ngxpagespeed.com/install) \
--nginx-version 1.18.0

To see all installation options, run:

bash <(curl -f -L -sS https://ngxpagespeed.com/install) --help

During installation, all questions should be answered positively. After the next request, you can enter what other modules need to be included in the assembly.

About to build nginx. Do you have any additional ./configure
arguments you would like to set? For example, if you would like
to build nginx with https support give --with-http_ssl_module
If you don't have any, just press enter.
>

The default set of parameters is shown below. It is worth using it for a basic installation or adding the modules you need to it.

--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/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-compat --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module --with-http_ssl_module --with-http_v2_module

Once the installation is complete, create the following symbolic link.

ln -s /usr/lib/nginx/modules /etc/nginx/modules

To verify that the installation was successful, enter:

nginx -V

You will see the Google PageSpeed module first among the configuration arguments:

...
configure arguments: --add-module=/root/incubator-pagespeed-ngx-latest-stable ...

Enabling Google PageSpeed Module

Now you need to enable the module in every virtual host on the server. Let's show this process using a default one as an example. Open the virtual host configuration:

nano /etc/nginx/sites-available/default

Add these lines to the server section:

server {

pagespeed on;
pagespeed FileCachePath "/var/cache/ngx_pagespeed/";
pagespeed RewriteLevel OptimizeForBandwidth;
location ~ ".pagespeed.([a-z].)?[a-z]{2}.[^.]{10}.[^.]+" {
add_header "" "";
}
location ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }

}

Check configuration for errors:

nginx -t

If you don't have a website on this server yet, you need to create an index page for the default host with arbitrary content.

echo "Hallo, Linux!" > /var/www/html/index.html

Now, restart Nginx.

service nginx restart

Testing the Google PageSpeed Module

The easiest way to make sure that the Google PageSpeed module is working is to access our website using curl:

curl -I -p http://web-server-IP-or-domain-name

Output:

HTTP/1.1 200 OK
Server: nginx/1.18.0
Content-Type: text/html
Connection: keep-alive
Date: Wed, 13 Jan 2021 13:13:05 GMT
X-Page-Speed: 1.13.35.2-0
Cache-Control: max-age=0, no-cache

The X-Page-Speed entry indicates that the Google PageSpeed module is running and shows its version.