How to Set Up Nginx with Google Pagespeed Module on Debian 11

Google Pagespeed Module is an open-source module for optimizing your websites under the Nginx and Apache web-server. The Pagespeed module is a server-level module installed alongside the Nginx and Apache webserver.

The Pagespeed module improves the performance and speed of your website by optimizing static files on your websites. The Pagespeed module optimizes images on your websites, minify static files such as HTML, CSS, and JavaScript, and optimize file caching.

In this tutorial, we will show you how to build the Nginx Pagespeed module on the latest Debian 11 Bullseye. This guide can be applied to the current of your Nginx installation because you just build the Nginx module.

Prerequisites

Before you start, ensure you have got following requirements.

  • Operating System: Debian 11 Bullseye
  • Root privileges

Now let's get started.

Installing Packages Dependencies

First, you will be installing packages dependencies for compiling the Nginx module.

1. Execute the apt command below to update current repositories on your system.

sudo apt update

2. Next, install basic packages dependencies for compiling programs using the apt command below.

sudo apt install curl dpkg-dev build-essential zlib1g-dev git libpcre3 git libpcre3-dev unzip uuid-dev -y

3. After that, install build dependencies for compiling the Nginx module.

sudo apt build-dep nginx -y

Checking Current Nginx Version

To build a custom Nginx Pagespeed module, be sure that your current Nginx web server keeps the same version of the Nginx source code that you will be using to build the Nginx module.

1. Execute the following command to check the Nginx version.

nginx -v

For this example, the current Nginx version installed on our machine is '1.18.0'.  Below is the similar output you will see.

nginx version: nginx/1.18.0

Optionally, if you don't have the Nginx package on your system, install it from the official Debian repository using the apt command below.

sudo apt install nginx -y

2. Next, create a new temporary environment variable for your Nginx version using the command below.

export NG_VER=1.18.0

Download Nginx and Pagespeed Source Code

In this step, you will be downloading the Nginx source code and Pagespeed module to your server. This Nginx source code will have the same version as your current Nginx package.

1. Create a new project directory '/usr/src/nginx' and go into it.

mkdir -p /usr/src/nginx; cd /usr/src/nginx

2. Now execute the following command to download Nginx source code using and extract it.

wget http://nginx.org/download/nginx-$NG_VER.tar.gz
tar -xzvf nginx-$NG_VER.tar.gz

3. Next, clone the Nginx Pagespeed module to the current directory, and after the process completes, you will see a new directory 'incubator-pagespeed-ngx'.

git clone https://github.com/apache/incubator-pagespeed-ngx.git

4. Move to the 'incubator-pagespeed-ngx' directory and switch to the stable branch of the Nginx Pagespeed module.

cd incubator-pagespeed-ngx/
git checkout latest-stable

5. Check the file 'PSOL_BINARY_URL' to get the download link of the Page Optimization Library (PSOL). This library is required for the Nginx Pagespeed module.

cat PSOL_BINARY_URL

You will see a similar output as below. The variable '$BIT_SIZE_NAME' is your server architecture.

wget https://dl.google.com/dl/page-speed/psol/1.13.35.2-$BIT_SIZE_NAME.tar.gz

6. Now create a new temporary environment variable 'BIT_SIZE_NAME' with the value of your server architecture. The below example is using 'x64' or 64-bit server architecture.

export BIT_SIZE_NAME=x64

7. Now download the PSOL source code and extract it.

wget https://dl.google.com/dl/page-speed/psol/1.13.35.2-$BIT_SIZE_NAME.tar.gz
tar -xzvf 1.13.35.2-$BIT_SIZE_NAME.tar.gz

Now you're ready to build the Nginx Pagespeed module.

Compiling Nginx Pagespeed Module

In this step, you will be compiling the Nginx Pagespeed module. In the end, you will get the Nginx module with the format '.so' that you can apply to your current Nginx web server.

1. Change the working directory to the Nginx project directory.

cd /usr/src/nginx/nginx-$NG_VER

2. Execute the following command to configure your Nginx module compilation. This command also checks your system requirements, be sure you don't have any errors.

./configure --with-compat --add-dynamic-module=../incubator-pagespeed-ngx

Below is the output you should get.

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

3. Next, compile the Nginx Pagespeed module using the following command.

make modules

This command will take some time, depending on your system's CPUs and memory.

4. After the compilation process finishes, your Pagespeed module is available as 'objs/ngx_pagespeed.so'. Check the module using the following command.

ls objs/ngx_pagespeed.so

You should get the Nginx Pagespeed module 'ngx_pagespeed.so' as below.

-rwxr-xr-x 1 root root 19M Dec  8 21:44 objs/ngx_pagespeed.so

Adding Pagespeed Module to the Current Nginx Installation

In this step, you will implement the Nginx Pagespeed module to your current Nginx installation. To do that, copy the Pagespeed module 'ngx_pagespeed.so' to the Nginx module directory and add a new configuration to enable it.

1. Copy the module 'ngx_pagespeed.so' to the default Nginx module directory '/usr/share/nginx/modules'.

cp /usr/src/nginx/nginx-$NG_VER/objs/ngx_pagespeed.so /usr/share/nginx/modules

2. Create a new configuration file to enable the Pagespeedd module.

nano /usr/share/nginx/modules-available/ngx-pagespeed.conf

Copy and paste the following configuration.

load_module modules/ngx_pagespeed.so;

Save the configuration and exit.

3. Activate the Pagespeed module by creating a symlink configuration 'ngx-pagespeed.conf' to the directory '/etc/nginx/modules-enabled/'.

ln -s /usr/share/nginx/modules-available/ngx-pagespeed.conf /etc/nginx/modules-enabled/70-ngx-pagespeed.conf

4. Next, create a new configuration 'pagespeed.conf' on the '/etc/nginx' directory.

nano /etc/nginx/pagespeed.conf

Copy and paste the following configuration.

pagespeed on;

# Needs to exist and be writable by nginx.  Use tmpfs for best performance.
pagespeed FileCachePath /var/ngx_pagespeed_cache;

# Ensure requests for pagespeed optimized resources go to the pagespeed handler
# and no extraneous headers get set.
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
  add_header "" "";
}
location ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }

Save the configuration and exit.

5. Next, create a new directory for storing the Nginx Pagespeed cache and be sure the directory is writable by the Nginx 'www-data' user.

mkdir -p /var/ngx_pagespeed_cache
chown www-data:www-data /var/ngx_pagespeed_cache

Now the Nginx Pagespeed module is added to your current Nginx installation. Go to the next step to enable it on your virtual hosts (server blocks).

Adding Nginx Pagespeed Module on Your Virtual Host

To enable the Pagespeed module to your virtual host, add the configuration 'pagespeed.conf' to your virtual host configuration file using the 'include' option.

1. For this example, add enable the Pagespeed module to the virtual host 'default'. Edit the configuration '/etc/nginx/sites-available/default' using nano editor.

nano /etc/nginx/sites-available/default

Add the configuration 'include /etc/nginx/pagespeed.conf;' inside the section 'server {...}' as below.

server {

  .....
  ....

  include /etc/nginx/pagespeed.conf;
}

save the configuration and exit.

2. Verify your Nginx configuration and make sure you don't get any errors. After that, restart the Nginx service to apply a new configuration.

nginx -t
sudo systemctl restart nginx

Verifying Nginx Pagespeed Module

To verify the installation and implementation of the Nginx Pagespeed module, you can use the curl command to get the HTTP headers of your Nginx web server or use the Inspect element on your web browser.

1. Execute the curl command below to get the HTTP headers of your Nginx web server.

curl -I https://domain.com/

Now you should get the output as below. The additional header section 'X-Page-Speed' means the Nginx Pagespeed module is enabled.

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

Nginx Pagespeed Module

2. To check from your web browser, visit your domain name and right-click on the page, then select the 'Inspect' menu.

Reload your web page again and you will see detailed HTTP headers as below.

Nginx Pagespeed Module

Conclusion

Congratulation! You've learned how to compile the Nginx Pagespeed module and implement it to the current Nginx installation. You just need to include the configuration 'pagespeed.conf' to your virtual host configuration.

Share this page:

Suggested articles

0 Comment(s)

Add comment