In this post, we are going to show that how to Install Google PageSpeed Module with Nginx HTTP server on Ubuntu 18.04 | 20.04. If you want to Install Google PageSpeed Module on your Ubuntu then this post is ideal for you.
The PageSpeed module is an open-source server modules that is used to optimize your site automatically. It is also known as mod_PageSpeed and created by Google. The main use of the PageSpeed Module is to optimize your websites automatically by make web pages load faster.
For more details about PageSpeed module, please visit its official website.
How to Install Google PageSpeed Module
Simply follow below steps to install PageSpeed Module with Nginx HTTP server:
Step 1 : Install required packages
To install required packages, run the commands below to install them:
sudo apt update
sudo apt install libssl-dev libxslt-dev libgd-dev curl nano
The above installed packages are required to compile and build Google PageSpeed modules.
Step 2 : Install Nginx HTTP Server
Run the command below to install Nginx HTTP Server:
sudo apt install nginx
Next, Upgrade Nginx version from Nginx official repository. To do that, install supported package using command below:
sudo apt install gnupg2 ca-certificates lsb-release
After that, create the repository file in your Ubuntu by running command below:
echo "deb http://nginx.org/packages/ubuntu lsb_release -cs nginx" | sudo tee/etc/apt/sources.list.d/nginx.list
Now, add the repository key using commands below:
curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add -
Finally, upgrade Nginx:
sudo apt update
sudo apt install nginx
You will also need to get installed Nginx version number to build the correct PageSpeed packages for Nginx. To get version number, run the command below:
nginx -v
Output
nginx version: nginx/1.20.0
Run the command below to get Nginx current install modules and arguments:
nginx -V
The above command display a output similar like 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'
These installed modules and arguments will be used for identical configuration when compiling PageSpeed module in Nginx. So copy the entire modules and arguments above and save them.
Install PageSpeed module
Run the command below to compiling PageSpeed Module with Nginx:
bash <(curl -f -L -sS https://ngxpagespeed.com/install) --nginx-version 1.20.0
Change the Nginx version number with the currently installed Nginx version number on your system. If you run the above command from your home directory then it will download PageSpeed packages in your home directory.
In this post , the PageSpeed Module packages will downloaded in /home/studiesonline folder called incubator-pagespeed-ngx-latest-stable.
/home/studiesonline/incubator-pagespeed-ngx-latest-stable
The path above used when building Nginx to add PageSpeed module so remember it for later use.
During the build process, you will be also prompted to configure additional arguments for Nginx server as show below:
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.
>
Set the PageSpeed folder(that created in your home directory) in the build process:
--add-dynamic-module=/home/richard/incubator-pagespeed-ngx-latest-stable
When prompted, combine both the path above and Nginx module and arguments. After combine, its look like below:
--add-dynamic-module=/home/studiesonline/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'
Choose Y, when prompt again.
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
Now the build process is complete and PageSpeed module included with Nginx. Run the command below to test it:
nginx -V
Output
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/studiesonline/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
Now, enable the PageSpeed modules and create a symbolic link for Nginx modules using command below;
sudo cp ~/nginx-1.20.0/objs/*.so /etc/nginx/modules
sudo ln -s /usr/lib/nginx/modules /etc/nginx/modules
At the last, add below PageSpeed block to Nginx server block of the website that you want to improve. Add this block in every server block where PageSpeed is enabled:
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 file and exit.
Now, reload the Nginx by running command below:
sudo systemctl reload nginx
To test the PageSpeed settings, run the command below:
curl -I -p http://example.com
Output
X-Page-Speed: 1.13.35.2-0
That’s all
If you face any error and issue in above steps , please use comment box below to report.
If our tutorials helped you, please consider buying us a coffee. We appreciate your support!
Thank you for your support.