Skip to main content

Installing mod_pagespeed on a cpanel server

PageSpeed is a very popular module for Apache and Nginx developed by Google that can optimize a lot of aspects of your web server.



You can follow these steps to install on a server with cpanel:

$>/usr/local/cpanel/3rdparty/bin/git clone /tmp/pagespeed/
$> cd /tmp/pagespeed/Easy
$> tar -zcvf pagespeed
$> mkdir -p /var/cpanel/easy/apache/custom_opt_mods/Cpanel/Easy
$> mv -t /var/cpanel/easy/apache/custom_opt_mods/Cpanel/Easy/
$> cd && rm -rf /tmp/pagespeed

Now, you can go to EasyApache in your WHM and compile the module, you have to check Mod PageSpeed in the apache modules config page.

PageSpeed Configuration

The module configuration file is:


To activate/deactivate the module you can set this option on/off:

ModPagespeed on

Tmpfs for the pagespeed cache

Due to the hight IOPS load that the cache may involve, a good idea is to put it in memory if you have enough memory in your server. This ensures that the cache accesses will be very fast.

To configure a 1,5GB size tmpfs cache you have to add this line to your /etc/fstab file:

tmpfs			/var/mod_pagespeed/cache tmpfs  rw,uid=99,gid=99,size=1500m,mode=0777 0 0

After that, simply run mount /var/mod_pagespeed/cache command.

The uid and gid must be the user that the web server uses to run.

Some parameters to configure cache:

# Cache max size
ModPagespeedFileCacheSizeKb          768000 
# Cache Ionode limit
ModPagespeedFileCacheInodeLimit      500000 

The cache expires old entries every 10 minutes if the value in ModPagespeedFileCacheSizeKb or ModPagespeedFileCacheInodeLimit is reached:

  ModPagespeedFileCacheCleanIntervalMs 600000 

If you want to disable pagespeed in some accounts you can do it through .htaccess file

<IfModule pagespeed_module>
	ModPagespeed off

Side Effects after installing PageSpeed on Cpanel

I got an unwanted side effect after installing PageSpeed on cpanel shared servers. The pagespeed module does its own http requests through its internal proxy to get the resources if them can not be found in the cache. These requests are originated from the server and the user-agent is something like “Serf/1.1.0 mod_pagespeed/”.

These requests are logged in domains access logs in /usr/local/apache/domlogs and in the logs used to audit the bandwidth used by each domain, so inevitable leads in an increase in traffic accounted by each domain. - - [01/Sep/2015:14:52:26 +0200] "GET /style.css HTTP/1.1" 200 1861 "" "Serf/1.1.0 mod_pagespeed/"

Solution: Don’t log these internal requests made by mod_pagespeed in the apache logs!

Setting up SetEnvIF module to detect pagespeed requests

We are going to use the SetEnvIF apache module to detect the specific traffic made by pagespeed and then set an env variable. We will use this variable later to avoid logging these requests.

Edit pre_main_global.conf in cpanel and add the following configuration:

<IfModule mod_setenvif.c>
    SetEnvIFNoCase User-Agent "^Serf/0.* mod_pagespeed/.*$" dontlog
    #SetEnvIf Remote_Addr "IP_SERVER" dontlog

Now, if the User-Agent used by pagespeed is detected the variable dontlog is set. You can set the variable too if the request is made by the server itself (2nd line).

Set virtualhosts logging depending on the donlog variable

Because Cpanel rebuilds apache configuration file (httpd.conf) in every compilation you won’t want to make the changes directly in httpd.conf file. Instead of that, you should change the templates provided by cpanel to be sure the changes will kept.

Depending on the apache version you are running the templates are located in:


To make custom changes in the templates you have to make a local copy of the template you want to change and make the changes in it:

# cp /var/cpanel/templates/apache2_4/vhost.default /var/cpanel/templates/apache2_4/vhost.local

Edit vhost.local template and add env=!dontlog to the lines begining with CustomLog word:

[% IF logstyle == 'combined' -%]
    [%- IF !enable_piped_logs || !supported.mod_log_config -%]
    CustomLog [% paths.dir_domlogs %]/[% wildcard_safe(vhost.log_servername) %] combined env=!dontlog
    [%- END %]
[% ELSE %]
    TransferLog [% paths.dir_domlogs %]/[% wildcard_safe(vhost.log_servername) %]
[% END -%]
[% IF supported.mod_log_config && supported.mod_logio && !enable_piped_logs -%]
    CustomLog [% paths.dir_domlogs %]/[% wildcard_safe(vhost.log_servername) %]-bytes_log "%{%s}t %I .\n%{%s}t %O ." env=!dontlog
[% END -%]

This setting is telling apache not log anything if the dontlog variable is set.

The last step you need to do is to rebuild the apache configuration file:


Now, you can verify in your domains access logs that the pagespeed internal requests are not logged anymore.

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.