DebianLinuxTutorials

How To Install Nginx PageSpeed Module on Debian 11

Install Nginx PageSpeed Module on Debian 11

In this tutorial, we will show you how to install Nginx PageSpeed Module on Debian 11. For those of you who didn’t know, The Google PageSpeed module, also known as mod_PageSpeed, is an open-source Apache HTTP or Nginx server-level package with modules that helps optimize your site. The Pagespeed module improves the performance and speed of your website by optimizing static files on your website. The Pagespeed module optimizes images on your websites and minifies static files such as HTML, CSS, JavaScript, and many more.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of the Matomo open-source analytics platform on a Debian 11 (Bullseye).

Prerequisites

  • A server running one of the following operating systems: Debian 11 (Bullseye).
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Install Nginx PageSpeed Module on Debian 11 Bullseye

Step 1. Before we install any software, it’s important to make sure your system is up to date by running the following apt commands in the terminal:

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

Step 2. Installing Nginx webserver.

Now we download the Nginx source package using the following command:

mkdir -p /usr/src/nginx
cd /usr/src/nginx
wget https://nginx.org/download/nginx-1.20.2.tar.gz

After that, we clone the Nginx Pagespeed module to the current directory:

sudo git clone https://github.com/apache/incubator-pagespeed-ngx.git
cd incubator-pagespeed-ngx/
sudo git checkout latest-stable

Next, check the file ‘PSOL_BINARY_URL‘ to get the download link of the Page Optimization Library (PSOL):

cat PSOL_BINARY_URL

Output:

:/usr/local/incubator-pagespeed-ngx$ cat PSOL_BINARY_URL
https://dl.google.com/dl/page-speed/psol/1.13.35.2-$BIT_SIZE_NAME.tar.gz

Then, download the PageSpeed Optimization Libraries (PSOL), so we can proceed:

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

Step 3. Compiling Nginx Pagespeed Module.

You will need to compile the “nginx_pagespeed module”. Now we change the working directory to the Nginx project directory:

cd /usr/src/nginx/nginx-1.20.2

Next, compile the ngx_pagespeed module using the following command below:

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

After the compilation process finishes, your Pagespeed module is available as ‘objs/ngx_pagespeed.so‘. Next, we copy the newly made “ngx_pagespeed.somodule to your active Nginx server directory:

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

Then, create a new configuration file to enable the Pagespeed module:

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

Add the following configuration:

load_module modules/ngx_pagespeed.so;

Save and close the file, then restart the Nginx server:

sudo nginx -t
sudo systemctl reload nginx

The next steps, now 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

Next, create a new configuration ‘pagespeed.conf‘ on the ‘/etc/nginx‘ directory:

nano /etc/nginx/pagespeed.conf

Add 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 and close the file, then 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

Step 4. Configure Nginx Pagespeed Module on Your Virtual Host.

Now we add to enable the Pagespeed module to the virtual host ‘default‘. Edit the configuration /etc/nginx/sites-available/default' use your favorite editor:

nano /etc/nginx/sites-available/default

Add the following file:

server {

  .....
  ....

  include /etc/nginx/pagespeed.conf;
}

Step 5. Configure Firewall.

By default, the UFW firewall is enabled on Debian. Depending on your Nginx virtual host configuration file, open ports 80 and 443 to allow HTTP and HTTPS traffic:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload

Step 6. Test the Nginx PageSpeed Module.

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

curl -I -p http://your-IP-address

Output:

HTTP/1.1 200 OK
Server: nginx/1.20.2
Content-Type: text/html
Connection: keep-alive
Date: Wed, 14 Feb 2022 11:36:08 GMT
X-Page-Speed: 1.13.35.2-0
Cache-Control: max-age=0, no-cache

Congratulations! You have successfully installed Nginx PageSpeed. Thanks for using this tutorial for installing the latest version of the Nginx PageSpeed Module on Debian 11 Bullseye. For additional help or useful information, we recommend you check the official Nginx website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button