How to Install Mod_PageSpeed with Apache on Debian 11 Bullseye

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 using various filters to pages that optimize server stylesheets, JavaScript, and HTML files and images through caching and rewriting among the top features.

In the following tutorial, you will learn how to install and do a basic setup with Apache Mod_PageSpeed Pagespeed on Debian 11 Bullseye with some optimal setups to increase your website speed server-side.

Update Debian

First, update your system to ensure all packages are up-to-date to avoid conflicts while installing the Pagespeed module.

sudo apt update && sudo apt upgrade -y

Install Required Packages

The following dependencies will need to be installed to install PageSpeed successfully. Most of these packages would already be present on your system, but running the command can help ensure they’re installed.

sudo apt install wget curl apt-transport-https gnupg2 -y

Install Apache

For pagespeed, it is a given you are trying to improve your site speed, so with Apache releases, they can fall behind, so why not install the latest stable version? The first step is to import and install the Apache webserver to the newest version is add the repository by Ondřej Surý.

Add the repository with the following command in your terminal.

curl -sSL https://packages.sury.org/apache2/README.txt | sudo bash -x

Update your repository to reflect the new change.

sudo apt update

The repository will often contain dependencies that will also need to be upgraded, which can be done now with the following command.

sudo apt upgrade

Now that you have installed the repository and updated the repository list, install Apache2 with the following.

sudo apt install apache2 -y

Install Apache Mod_Pagespeed module

The next step is to download the Apache Mod_Pagespeed module, found on the Google PageSpeed webpage.

Alternatively, use the following wget command to download the GPG key and repository. This should be done since even if you manually install it, you will have errors since the way PageSpeed adds the repository is incorrect.

First, import the GPG key.

sudo wget -O- https://dl.google.com/linux/linux_signing_key.pub | gpg --dearmor | sudo tee /usr/share/keyrings/apache-pagespeed.gpg

Next, import the PageSpeed repository.

echo deb [signed-by=/usr/share/keyrings/apache-pagespeed.gpg] http://dl.google.com/linux/mod-pagespeed/deb/ stable main | sudo tee /etc/apt/sources.list.d/apache-pagespeed.list

Now run an APT update to reflect the newly imported repository.

sudo apt update

Finally, you can install the Apache Mod-PageSpeed using the command below.

sudo apt install mod-pagespeed-stable -y

During the installation, an additional source file is created that is incorrect, you must delete this, or else updates will break.

sudo rm /etc/apt/sources.list.d/mod-pagespeed.list

Now you should be left with one sources list that you imported further up. If you accidentally delete the correct one, copy the command further up again to restore it.

How to Enable/Disable Mod_Pagespeed module

Now that you have installed the PageSpeed module, enabling or disabling the module can be quickly done using the following commands.

First, to enable the service.

sudo a2enmod pagespeed

Secondly, disable the service.

sudo a2dismod pagespeed

Example output:

How to Install Mod_PageSpeed with Apache on Debian 11 Bullseye

Alternatively, you can edit module configuration directly by opening the file /etc/apache2/mods-available/pagespeed.conf.

sudo nano /etc/apache2/mods-available/pagespeed.conf

Next, change the following options.

 ModPagespeed on      #Turn on mod_pagespeed

 ModPagespeed off     #Turn off mod_pagespeed

Remember, each time you disable or enable, and you need to restart the Apache2 service.

sudo systemctl restart apache2

PageSpeed Filers Set Up

There are a few ways you can set up your pagespeed filters, and none are right or wrong and needs to be tested. Unlike Nginx, Apache has the configuration files to edit and requires uncommenting and configuring.

First, open the PageSpeed filter configuration file located /etc/apache2/mods-available/pagespeed.conf.

sudo nano /etc/apache2/mods-available/pagespeed.conf

The set filter option is “CoreFilters” as the re-write level by default.

Example:

How to Install Mod_PageSpeed with Apache on Debian 11 Bullseye

Overall, PageSpeed offers three re-write levels: “CoreFilters, PassThrough, and OptimizeForBandwidth”. As you may have noticed by the original comments, “CoreFilters” is safe for most websites and is used as the default for testing.

Corefilters consists of:

  •   add_head – Adds a <head> element to the document if not already present.
  •   combine_css – Combines multiple CSS elements into one.
  •   combine_javascript – Combines multiple script elements into one.
  •   convert_meta_tags – Adds a response header for each meta tag with an http-equiv attribute.
  •   extend_cache – Extends cache lifetime of CSS, JS, and image resources that have not been optimized by signing URLs with a content hash.
  •   fallback_rewrite_css_urls – Rewrites resources referenced in any CSS file that cannot otherwise be parsed and minified.
  •   flatten_css_imports – Inline CSS by flattening all @import rules.
  •   inline_css – Inlines small CSS files into the HTML document.
  •   inline_import_to_link – Inlines <style> tags comprising only CSS @imports by converting them to equivalent <link> tags.
  •   inline_javascript – Inlines small JS files into the HTML document.
  •   rewrite_css – Rewrites CSS files to remove excess whitespace and comments, and, if enabled, rewrite or cache-extend images referenced in CSS files. In OptimizeForBandwidth mode, the minification occurs in-place without changing URLs.
  •   rewrite_images – Optimizes images, re-encoding them, removing excess pixels, and inlining small images. In OptimizeForBandwidth mode, the minification occurs in-place without changing URLs. 
  •   rewrite_javascript – Rewrites JavaScript files to remove excess whitespace and comments. In OptimizeForBandwidth mode, the minification occurs in-place without changing URLs. 
  •   rewrite_style_attributes_with_url – Rewrite the CSS in style attributes if it contains the text ‘url(‘ by applying the configured rewrite_css filter to it.

Now, you can increase CoreFilters further by adding even more filters. However, I would advise doing some research as more filters equal more risk of your website breaking. It’s a fine art to use google pagespeed. For example, below of some additional options.

Remember, this is just an example!! This will takes countless hours of testing for your website, especially if you are new to using the Pagespeed module! My advice, use the core. That does not work; add the settings one by one. Monitor and watch out for website breakages, especially around minifying and combines. The end of the tutorial also has some links that may help.

WebP Conversion Support

Apache will require PHP support to convert webp images and Debian ships with PHP 8.0 by default. However, you may want to use different versions of PHP, so first, import the PHP repository PPA.

sudo add-apt-repository ppa:ondrej/php -y

Next, run an APT update to sync the newly added PPA.

sudo apt update

Now install one of the following packages.

PHP 7.4 WebP Support:

sudo apt install imagemagick php7.4-imagick ffmpeg

PHP 8.0 WebP Support:

sudo apt install imagemagick php8.0-imagick ffmpeg

PHP 8.1 WebP Support:

sudo apt install imagemagick php8.1-imagick ffmpeg

Verify Apache PageSpeed module is Operational

The hard work has paid off. Now you want to see if PageSpeed is working; use the following command below.

curl -I -p https://www.example.com

You should see in the output this part as follows:

x-page-speed: 1.13.35.2-0

Secure PageSpeed Admin Area

Most users would prefer using an admin web UI, and I know I do, but Apache Mod_PageSpeed admin should be restricted and configured by default. In your Apache virtual host configuration file, you can create a location to allow the local host and your IP.

Example:

<Location /pagespeed_admin>
    Order allow,deny
    Allow from localhost
    Allow from 127.0.0.1
    Allow from <Your IP>
    SetHandler pagespeed_admin
</Location>

<Location /pagespeed_global_admin>
    Order allow,deny
    Allow from localhost
    Allow from 127.0.0.1
    Allow from <Your IP>
    SetHandler pagespeed_global_admin
</Location>

Example of admin GUI:

How to Install Mod_PageSpeed with Apache on Debian 11 Bullseye

Preload Apache PageSpeed module

The ngx_pagespeed module does not have a preload function, which can be frustrating since it can take several visits before items are adequately cached and optimized. This can be made worse if you had to purge the cache a few times a day, unacceptably leaving your website optimization levels.

However, you enter the following command to pre-fetch all your web pages if you have ramdisk set up. Also, you can set this on a cronjob for the future to do hourly, daily, or weekly.

Visit the Crontab.guru if you need help creating cron timings.

Preload pagespeed:

sudo wget -m -p -E -k -P /tmp/preload/ https://www.example.com/

Cronjob job one hourly:

00 */1 * * * wget -m -p -E -k -P /tmp/preload/ https://www.example.com/

For those new to Linux and Debian, use the following command to open Cron.

sudo crontab -e

Community Resources for PageSpeed

  • ModPageSpeed.com – Official Documentation and probably the first site you should visit for help.
  • PageSpeedGuide.com – Great source of information on PageSpeed and filters.

Comments and Conclusion

PageSpeed is an excellent way of optimizing your website in these times where SEO optimization with Google Search counts on speed more these days than in the previous years.

The software is also fantastic as it works on the backend and is not an additional plugin that works through a web application like WordPress. However, PageSpeed can be very complicated; it will most likely take you a few days to even a few weeks to get the optimal setup for your website.

Before you start messing around with filters, visit the Google PageSpeed documentation. It will give you in-depth answers on the risk involved and what each filter will potentially do to your website. Most people give up using PageSpeed since they cannot put in the effort to get it right, as it’s very time-consuming and frustrating. But if you do, then it can potentially transform your website two to 10 times quicker.



Follow LinuxCapable.com!

Like to get automatic updates? Follow us on one of our social media accounts!