Install and Configure mod_pagespeed on cPanel
Posted on Updated onA few years ago, Google released a module for Apache. This module rewrote web pages according to standard best practices. We all know the importance of caching and combining files to reduce requests. But a lot of other tweaks can be done to improve load times. Inlining above-the-fold content CSS, progressive display of images, browser expiry headers etc. All of these play a role in your website’s loading speed. You can implement all or most of these improvements by modifying your website. But it’s easier and faster if it’s done at the web server level.
ModPageSpeed is Google’s answer to these problems. Most shared web hosting plans exclude the mod_pagespeed module from their setup. Bluehost, for example, offers it in their VPS packages and upwards. Resellers can make this feature available to their clients out of the box. This improves their page loading times and lowers the strain on your own servers.
Steps to deploy ModPageSpeed on a cPanel / WHM server:
- Installing the mod_pagespeed module for Apache 2.4;
- Global ModPageSpeed configuration;
- Configuring ModPageSpeed for virtual hosts;
- Setting up a CDN with ModPageSpeed ;
- Enabling the PageSpeed Console;
- Purging the PageSpeed cache.
Without further ado, let’s proceed to the installation.
1. Installing the mod_pagespeed Module
To get started, open an SSH session on your cPanel web server. Install the ea-apache24-mod_pagespeed package using Yum:
sudo yum install yum install ea4-experimental sudo yum install ea-apache24-mod_pagespeed
Access the WHM control panel on your server. Click EasyApache 4 from the menu bar.
Click Customize to edit your current EasyApache profile. In the left column, select Apache Modules. Use the search filter to locate the mod_pagespeed module and activate it:
Note that in my screenshot, the module is activated. Select Review from the left column. Click on Provision at the bottom of the page. Restart the Apache service afterward.
2. Global mod_pagespeed Configuration
Make a backup copy of the global PageSpeed configuration file:
cp /etc/apache2/conf.modules.d/456_pagespeed.conf /etc/apache2/conf.modules.d/456_pagespeed.conf.backup
Edit the configuration file:
vi /etc/apache2/conf.modules.d/456_pagespeed.conf
Disable ModPagespeed globally. We will activate it on one website at a time instead. Add the following parameters identified in red:
[...]
<IfModule pagespeed_module>
ModPagespeed off
ModPagespeedFileCachePath "/var/mod_pagespeed/cache/default/"
ModpagespeedLogDir /var/log/pagespeed/default/
ModPagespeedMessageBufferSize 100000
ModPagespeedEnableCachePurge on
[...]
Save the configuration file and exit the editor. Restart the Apache service:
systemctl restart httpd
Make sure that all the websites are functioning.
To see the complete list of configuration parameters, follow this link.
3. Configuring ModPageSpeed for Virtual Hosts
Enable ModPagespeed one virtual host at a time to avoid issues. This makes it possible to use a custom configuration for each website.
Keep the Apache configuration files generated by cPanel intact in “/etc/apache2/conf” and “/etc/apache2/conf.d”. Otherwise, cPanel will overwrite them when you rebuild the configuration.
Prior to enabling ModPagespeed, determine which configuration file you need to create:
Description | Virtual Hosts | Directory and Configuration File |
---|---|---|
ModPagespeed on a single virtual host | With SSL | /etc/apache2/conf.d/userdata/ssl/2_4/user/domain/pagespeed.conf |
Without SSL | /etc/apache2/conf.d/userdata/std/2_4/user/domain/pagespeed.conf | |
ModPagespeed on all virtual hosts of a user | With SSL | /etc/apache2/conf.d/userdata/ssl/2_4/user/pagespeed.conf |
Without SSL | /etc/apache2/conf.d/userdata/std/2_4/user/pagespeed.conf |
Create the configuration file. Making sure to replace “user” and “domain” with their respective values. Let’s enable Pagespeed on a single virtual host with SSL:
mkdir -p /etc/apache2/conf.d/userdata/ssl/2_4/user/domain/ vi /etc/apache2/conf.d/userdata/ssl/2_4/user/domain/pagespeed.conf
Suggested configuration:
ModPagespeed on
ModPagespeedFileCachePath "/var/mod_pagespeed/cache/domain/"
ModPagespeedEnableFilters combine_css,prioritize_critical_css,rewrite_javascript,combine_javascript,defer_javascript,extend_cache,resize_rendered_image_dimensions
Save the configuration file and exit the editor. Rebuild the Apache configuration files:
/usr/local/cpanel/scripts/rebuildhttpdconf
Restart Apache:
systemctl restart httpd
Make sure your website is functioning as expected. If you are having trouble with Javascript, disable the filters rewrite_javascript, combine_javascript, and defer_javascript. With each change, make sure to rebuild the configuration files.
To verify that ModPagespeed is working, inspect the source code of a web page of your site. Look instances of the “pagespeed” keyword. For example:
<script type="text/javascript" src="/pagespeed_static/js_defer.I4cHjq6EEP.js"></script>
Note that the optimization of your website will be gradual. Image optimization, for example, is not done in real time. Optimized images are cached. Eventually, the name of the images will be replaced in the HTML code of your pages by the optimized images.
4. Configuring ModPageSpeed Filters
There are a multitude of filters as well as parameters to configure. These allow you to fine-tune ModPagespeed. Some filters will convert and/or optimize images on your website. There is even a filter for lazy-loading images.
To see the full list of filters and their parameters, click here.
5. Setting up a CDN with ModPagespeed
If you are using a Content Delivery Network (CDN), you can integrate it with PageSpeed.
Add these parameters to the virtual host file (see step # 3):
ModPagespeedAllow all ModPagespeedEnableFilters rewrite_domains ModPagespeedDomain domain.com ModPagespeedDomain domain-xyz.cdn.com
Replace “domain.com” with the domain of your website and “domain-xyz.cdn.com” with the domain of your CDN.
Regenerate the Apache configuration files:
/usr/local/cpanel/scripts/rebuildhttpdconf systemctl restart httpd
The files generated by PageSpeed will then be loaded through your Content Distribution Network (CDN).
6. Enabling the PageSpeed Admin Console
The PageSpeed Console allows you to view statistics but also to clear the cache. To enable the console, insert the following content into the configuration file created in step # 3:
ModPagespeedStatistics on ModPagespeedStatisticsLogging on ModpagespeedLogDir /var/log/pagespeed/domain/ <Location /pagespeed_admin> AuthType Basic AuthName "Restricted Area" AuthUserFile /home/user/pagespeed/.htpasswd Require valid-user SetHandler pagespeed_admin </Location>
In order to protect the console, it is necessary to create a file containing the list of users and passwords. The location of this file is defined by the “AuthUserFile” parameter. Here’s how to create a user named “admin”:
mkdir /home/user/pagespeed htpasswd -c /home/user/pagespeed/.htpasswd admin
Then enter the password when requested. To add other users to the same file, use the same command “htpasswd” omitting the “-c” parameter. The one is used to create the file.
Once the changes are complete, regenerate the configuration and restart the Apache service:
/usr/local/cpanel/scripts/rebuildhttpdconf systemctl restart httpd
You can now access the PageSpeed console at https://www.domain.com/pagespeed_admin/ with the username “admin” and the password you have chosen:
Note that enabling statistics can slow down the server. It is advisable to disable this one if it is not necessary. You will still be able to access the console:
ModPagespeedStatistics off ModPagespeedStatisticsLogging off
6. Purging the ModPageSpeed Cache
If you make changes to your website and these do not seem to apply, you will need to clear the cache.
Method # 1: Remove the Complete Cache by Command Line
This method requires “root” access which limits its use. Just create an empty file:
touch /var/mod_pagespeed/cache/domaine/cache.flush systemctl restart httpd
With this method, there may be a short delay before the cache is flushed.
Method # 2: Using a “PURGE” Command
In order for this method to be possible, you must first edit the global configuration file:
vi /etc/apache2/conf.modules.d/456_pagespeed.conf
Then add this line:
ModPagespeedPurgeMethod PURGE (optional)
Restart Apache:
systemctl restart httpd
It is now possible to send an “HTTP PURGE” command to the address that you want to remove from the cache. For example:
curl --request PURGE 'https://www.domaine.com/page'
It is also possible to empty the cache for an entire directory:
curl --request PURGE 'https://www.domaine.com/page/*'
For other examples, click here.
Method # 3: Using the PageSpeed Console
To make it possible to use the PageSpeed console to clear the cache, the ModPagespeedPurgeMethod parameter must be added to the global configuration file as described in the previous method.
From the PageSpeed console, click on “Caches > Purge Cache”. Enter the URL to purge or purge the entire cache:
Conclusion
ModPageSpeed can greatly improve the performance of your website and improve its search rankings in the process. Be careful when enabling filters. Enable them one at a time and make sure that they don’t break your site. Experiment with different filters and test your website’s performance using Google’s PageSpeed Insights.