Install Google Page Speed module for Apache 2 on Debian
The Apache 2 Google Page Speed module is a tool automatically optimizing web sites hosted on the server.
This howto is tested on:
- Debian 6.0 Squeeze
Prerequisites
This howto needs a Apache2 HTTP server installed as described in Install and setup Apache 2 on Debian.
Installation
Detect the server architecture:
ARCH=$(command dpkg --print-architecture)
Download the last version of the module:
command wget "https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_${ARCH}.deb" \
--output-document="/tmp/mod-pagespeed-stable_current_${ARCH}.deb"
Install the dependencies:
command apt-get install apache2.2-common libc6 libgcc1 libstdc++6
Install the module:
command dpkg -i "/tmp/mod-pagespeed-stable_current_${ARCH}.deb"
Enable the module:
command a2enmod pagespeed
Adjust the module default settings ( les réglages par défaut du module (among other things, disable it by default):
echo "# Pagespeed local defaults.
<IfModule pagespeed_module>
ModPagespeed off
ModPagespeedFileCachePath /var/mod_pagespeed/cache/
</IfModule>" \
> "/etc/apache2/conf.d/pagespeed"
Reload the server configuration:
/etc/init.d/apache2 force-reload
Usage
Provide the domain name to optimize:
DOMAIN="www.example.com"
Compute the domain VirtualHost configuration file path. Adjust the value to your environment if you have not used a2tools to setup the domain:
DOMAIN_CONFIG="/etc/apache2/sites-available/http-${DOMAIN}"
Add the PageSpeed configuration to the domain virtual host configuration file:
if [ -e "${DOMAIN_CONFIG}" ]; then
command sed -i -e "/<\/VirtualHost>/i\\
<IfModule pagespeed_module>\\
ModPagespeed on\\
ModPagespeedFileCachePath /var/mod_pagespeed/cache/\\
#ModPagespeedGeneratedFilePrefix /var/mod_pagespeed/files/\\
\\
# Turn this on if /var/mod_pagespeed/cache/ is on a ext3 filesystem.\\
ModPagespeedSharedMemoryLocks off\\
\\
# Declare that ${DOMAIN} is served by the localhost HTTP server.\\
# Be sure to use the correct OriginDomain. A mis-configured value\\
# can be really troublesome.\\
ModPagespeedDomain http://${DOMAIN}\\
#ModPagespeedMapOriginDomain localhost ${DOMAIN}\\
\\
# Allow to disable the core filters.\\
ModPagespeedRewriteLevel PassThrough\\
\\
# Core filters\\
#ModPagespeedEnableFilters add_head\\
ModPagespeedEnableFilters combine_css\\
ModPagespeedEnableFilters convert_meta_tags\\
ModPagespeedEnableFilters extend_cache\\
ModPagespeedEnableFilters inline_css\\
ModPagespeedEnableFilters inline_import_to_link\\
ModPagespeedEnableFilters inline_javascript\\
ModPagespeedEnableFilters rewrite_css\\
#ModPagespeedEnableFilters rewrite_images\\
ModPagespeedEnableFilters rewrite_javascript\\
ModPagespeedEnableFilters rewrite_style_attributes_with_url\\
#ModPagespeedEnableFilters trim_urls\\
\\
# Additionnal filters\\
ModPagespeedEnableFilters collapse_whitespace\\
ModPagespeedEnableFilters remove_comments\\
\\
# Disabling filters that can be troublesome.\\
#\\
# Add <head/> tags if missing. Useless.\\
ModPagespeedDisableFilters add_head\\
# Heavy server load. Can be replaced by smart image management:\\
# Compress and resize your images before uploading to server.\\
ModPagespeedDisableFilters rewrite_images\\
# No real point. Allow the browser to render the correct\\
# image dimensions without loading the image.\\
ModPagespeedDisableFilters insert_img_dimensions\\
# Break XHTML by removing attribute quotation. No real point.\\
ModPagespeedDisableFilters remove_quotes\\
# Can break SEO and cause 404 errors.\\
ModPagespeedDisableFilters trim_urls\\
\\
# Warning turning this on can cause memory leaks.\\
ModPagespeedLowercaseHtmlNames off\\
\\
# Example static content configuration (here for a Symfony based site) \\
# Configure this according to your content. This improve performances.\\
# Prevent pagespeed to fetch these ressources with a HTTP request.\\
#ModPagespeedLoadFromFile \"http://${DOMAIN}/css/\" \"/opt/${DOMAIN}/web/css/\"\\
#ModPagespeedLoadFromFile \"http://${DOMAIN}/js/\" \"/opt/${DOMAIN}/web/js/\"\\
#ModPagespeedLoadFromFile \"http://${DOMAIN}/images/\" \"/opt/${DOMAIN}/web/images/\"\\
\\
# Example pagespeed exclusion configuration (mandatory for some JS editors and frameworks)\\
ModPagespeedDisallow */tiny_fck/*\\
ModPagespeedDisallow */modules/wysiwyg/*\\
ModPagespeedDisallow */ckeditor/*\\
</IfModule>\\
" "${DOMAIN_CONFIG}"
else
echo "Error: File '${DOMAIN_CONFIG}' does not exists."
fi
Check the Apache 2 configuration:
command apache2ctl -t
Reload the configuration by restarting the HTTP server:
/etc/init.d/apache2 restart
Check the logs for module PageSpeed errors:
command tail -f "/var/log/apache2/error.log"
Origin server
The "ModPagespeedMapOriginDomain" option is very important. The PageSpeed module use this information to est très importante. Le module PageSpeed utilise cette information pour speed up the fetch of data associated to the served pages.
If the domain name of the site optimised by PageSpeed does not point to the site origin server (for a reverse proxy, by example), make sure that the "ModPagespeedMapOriginDomain" option map the domain name to this server.
Static content
If the static content file system path is known, adjust accordingly the "ModPagespeedLoadFromFile" options.
The systematic use of this configuration directive for static content limit the PageSpeed module resources usage.
Cache management
The mod_pagespeed cache management can trigger these errors:
[Fri Dec 02 09:03:59 2011] [error] [mod_pagespeed 0.10.19.4-1209 @7059] /var/mod_pagespeed/cache/rname/ce/y3_Io7zAGUEgn8lqUAo8:0: creating dir (code=31 Too many links)
[Fri Dec 02 09:03:59 2011] [error] [mod_pagespeed 0.10.19.4-1209 @7059] Could not create directories for file /var/mod_pagespeed/cache/rname/ce/y3_Io7zAGUEgn8lqUAo8/hpGQPajpyeMfeWveYprL,
This error is due to a ext3 file system limitation: no more than 32000 (approximation) files by folder. There is two solutions:
- Enable the lock management in shared memory with:
ModPagespeedSharedMemoryLocks on
According to my experience,this option has low impact on the error frequency. - Make sure to use a ext4 file system for these folders (recommended !):
- /var/mod_pagespeedcache
- /var/mod_pagespeed/cache
Memory allocation error with APC
Use APC with Google mod_pagespeed can trigger this error:
[apc-warning] Unable to allocate memory for pool. in /opt/mysymfonyapp/lib/vendor/symfony/lib/addon/propel/database/sfPropelDatabase.class.php on line 68
To solve this problem, install from sources (using the PECL tool) the last version of APC. A installation method is described in the guide Install the PHP extension APC on Debian.
Selective disablement
To disable the pagespeed module optimizations for some files, use the "ModPagespeedDisallow" option:
ModPagespeedDisallow */jquery-ui-1.8.2.custom.min.js
ModPagespeedDisallow */js_tinyMCE.js
This option allow to fix problems with some Wysiwyg editors and Javascript libraries.
Bugged and useless filters
Some Pagespeed module filters are bugged:
- insert_img_dimesions: Insert the images sizes in the HTML code. It is a browser rendering optimization. This filter use a lot of resources. If you site use images from others domains, disable this filter. It can trigger a high memory usage by Apache 2.
- rewrite_images: This filter use a lot of resources and can overload the Pagespeed cache contents, causing on the long term a performance loss.
- add_head: Add the <head/> tags. Useless if your sites have a clean code.
- trim_urls: This filter can break links (404 errors) and disturb your SEO.
- remove_quotes: Remove the HTML attributes quotes. The collapse_whitespace filter is a better way to reduce page size.
Limit the memory usage
In order to limit the memory usage when PageSpeed is enabled, i've applied the following changes for a server with 2 GB RAM:
- Maximum 50 requests by connexion (MaxKeepAliveRequests) : allow a greater turn-over for HTTP clients.
- Maximum 100 simultaneous connexions (MaxClients) : limit the server load.
- Maximum 500 requests by Apache thread (MaxRequestsPerChild): raise the Apache threads memory freeing frequency.
These values are intuitive and should be adjusted to server resources.
Apply the changes:
echo "# mod_pagespeed memory usage limitation
MaxKeepAliveRequests 50
MaxClients 100
MaxRequestsPerChild 500" \
> '/etc/apache2/conf.d/pagespeed-optimization'
Reload the server configuration:
/etc/init.d/apache2 restart
References
These books can help you:
- The Art of SEO: Mastering Search Engine Optimization (Theory in Practice)
- SEO Warrior
- High Performance Web Sites: Essential Knowledge for Front-End Engineers
- Even Faster Web Sites: Performance Best Practices for Web Developers
- Building Scalable Web Sites: Building, Scaling, and Optimizing the Next Generation of Web Applications
- Website Optimization: Speed, Search Engine & Conversion Rate Secrets
Thanks
- Thanks to Google mod_pagespeed developers.
- Thanks to the writers of mod_pagespeed Installation and Configuration.
- Thanks to The Art of Web for System: Apache mod_pagespeed settings and System: Apache mod_pagespeed issues.
- Thanks to Linux Poison for Configure Apache HTTP server with mod-pagespeed for high performance.