How to Install PageSpeed Module on Ubuntu Linux

This post presents a step-by-step guide on how to install and configure the Google PageSpeed module on Ubuntu 20.04 | 18.04 with Nginx HTTP support. PageSpeed optimizes websites for better user experience. The tutorial covers installation of prerequisite packages, the Nginx HTTP server, and the PageSpeed module, followed by PageSpeed configurations. Lastly, tests are conducted…

This brief tutorial shows students and new users how to install the Google PageSpeed module on Ubuntu 20.04 | 18.04 with Nginx HTTP support.

PageSpeed module is an open-source, Apache-backed, created by Google to help make web pages load faster by rewriting them to improve latency and decrease bandwidth.

The PageSpeed module was created long ago, and you probably won’t see it installed on too many online web pages today. But the concept behind it is still relevant.

The primary goal behind the PageSpeed project was to optimize websites automatically, thus creating a better web experience on any device for users.

If you’re running a website today and want to give your users a better experience by optimizing your web pages, then the PageSpeed module should be a great place to start.

I use it on this site, and it’s performing great for my audience.

For more about the PageSpeed module, please check its website here.

To get started with installing and configuring the PageSpeed module on Ubuntu with Nginx support, follow the steps below:

Install required packages

These packages are required to compile and build PageSpeed modules in Ubuntu. Run the commands below to install the packages below:

sudo apt update
sudo apt install libssl-dev libxslt-dev libgd-dev curl nano

After installing the packages above, continue below.

Install Nginx HTTP Server

Now that the packages above are installed run the commands below to install the Nginx HTTP server in Ubuntu. Installing Nginx in Ubuntu is pretty straightforward. Nginx stable packages are available in Ubuntu default repositories.

Simply run the commands below to install Nginx.

sudo apt install nginx

For some reason, the Ubuntu-installed Nginx didn’t play well when I built PageSpeed modules. In that case, upgrade to the version from the Nginx official repository.

Run the commands below to install the supported package.

sudo apt install gnupg2 ca-certificates lsb-release

Then create the repository file in Ubuntu.

echo "deb http://nginx.org/packages/ubuntu lsb_release -cs nginx" | sudo tee/etc/apt/sources.list.d/nginx.list

And add the repository key by running the commands below:

curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add -

After that, upgrade by running the commands below:

sudo apt update
sudo apt install nginx

After installing Nginx, you’ll also want to get the version installed. You’ll need this version number to build the correct PageSpeed packages for Nginx.

To find the Nginx version number, run the commands below:

nginx -v

That will display similar lines as shown below:

nginx version: nginx/1.20.0

You’ll also want to know what modules and arguments are enabled and built with Nginx. We want to use the identical configuration when recompiling Nginx with PageSpeed support.

To find Nginx current install modules and arguments, run the commands below:

nginx -V

That should display a similar line as below:

--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 -fdebug-prefix-map=/data/builder/debuild/nginx-1.20.0/debian/debuild-base/nginx-1.20.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'

Copy the entire line above and store it somewhere you can quickly get to. We’ll use the line above below when compiling the PageSpeed module in Nginx.

Install PageSpeed module

Now that you have all the necessary info run the commands below to start compiling PageSpeed with Nginx.

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

Replace the Nginx version number with the corresponding number currently installed on your system. When you run the command from your home directory, it will download PageSpeed packages inside your home folder.

The packages were downloaded in my /home/richard folder called incubator-pagespeed-ngx-latest-stable for this tutorial.

/home/richard/incubator-pagespeed-ngx-latest-stable

You’ll need the path above to add the PageSpeed module when building Nginx.

You’ll be prompted to configure additional arguments for Nginx during the build. Remember the PageSpeed folder created in your home directory?

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.
> 

Include the folder in the build process. So it will look like this:

--add-dynamic-module=/home/richard/incubator-pagespeed-ngx-latest-stable

When prompted during the build, combine the path above and Nginx’s original module and arguments output, as shown above. The entire line should look like this:

--add-dynamic-module=/home/richard/incubator-pagespeed-ngx-latest-stable --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 -fdebug-prefix-map=/data/builder/debuild/nginx-1.20.0/debian/debuild-base/nginx-1.20.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'

When prompted again, choose Y for yes.

You have set --with-debug for building nginx, but precompiled Debug binaries for
PSOL, which ngx_pagespeed depends on, aren't available.  If you're trying to
debug PSOL you need to build it from source.  If you just want to run nginx with
debug-level logging you can use the Release binaries.

Use the available Release binaries? [Y/n] Y

With for the build to complete.

After the build is complete, the PageSpeed module should be included with Nginx. You can test by running the commands below:

nginx -V

It should now output a line with your home directory pointing to the PageSpeed folder.

nginx version: nginx/1.20.0
built by gcc 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04) 
built with OpenSSL 1.1.1f  31 Mar 2020
TLS SNI support enabled
configure arguments: --add-module=/home/richard/incubator-pagespeed-ngx-latest-stable --add-dynamic-module=/home/richard/incubator-pagespeed-ngx-latest-stable --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules

Next, enable PageSpeed modules and create a symbolic link for Nginx modules.

sudo cp ~/nginx-1.20.0/objs/*.so /etc/nginx/modules
sudo ln -s /usr/lib/nginx/modules /etc/nginx/modules

Finally, add this initial PageSpeed block to the Nginx server block of the website you wish to improve. In every server block where PageSpeed is enabled, add:

server {
.
pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;

location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
  add_header "" "";
}
location ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }
.
}

Save the exit from the file and reload Nginx

sudo systemctl reload nginx

Test PageSpeed settings.

curl -I -p http://example.com

You should see an output similar to the one below

HTTP/1.1 200 OK
Server: nginx/1.20.0
Date: Sat, 15 May 2021 14:58:16 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 20 Apr 2021 13:35:47 GMT
X-Page-Speed: 1.13.35.2-0 
Cache-Control: max-age=0, no-cache

That should do it. More PageSpeed configurations are available on its website. Visit the site to find more configuration options.

PageSpeed Filters (modpagespeed.com)

Conclusion:

This post showed you how to install the PageSpeed module on Ubuntu with the Nginx HTTP server. If you find any error above, please use the comment form below to report.

Richard Avatar

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *


Exit mobile version