Skip to main content

Optimization Made Easy with mod_pagespeed

By Louis Simoneau

Big announcement last week from the folks at Google: they’ve released a new module for the Apache web server: mod_pagespeed. mod_pagespeed bundles together a bunch of server-side page speed optimizations into one easy-to-use module. With the appropriate configuration options, it will compress and combine your CSS and JavaScript files, optimize caching settings, remove comments and whitespace from your files, optimize images, and more.Let’s take it for a quick spin, shall we?


The mod_pagespeed module is available as a simple installable package. That’s good news: you won’t need to recompile Apache from source to use it. At the moment it’s available for CentOS/Fedora and Ubuntu/Debian systems, whether they’re 32 or 64-bit. For the purposes of this walkthrough, I’ll be using Ubuntu, but you can find installation instructions for CentOS/Fedora on the downloads page.Download the .deb file, then run (assuming, of course, you already have Apache 2.2 installed):

sudo dpkg -i mod_pagespeed_*.debsudo apt-get -f install

Now you just need to restart Apache to enable the new module:

sudo /etc/init.d/apache2 restart


By default, all of mod_pagespeed’s filters are turned off, so you won’t actually see it doing anything. To remedy this, we need to edit its configuration file. By default (on Ubuntu or Debian-based systems), this will be located in /etc/apache2/mods-available/pagespeed.conf.At the very top of that file, you’ll see something like:

<IfModule pagespeed_module>  SetOutputFilter MOD_PAGESPEED_OUTPUT_FILTER  ModPagespeed on

That’s good! If you see ModPagespeed off, change it to on before going any further.A little farther down the file (line 29 in the version I have), you’ll see a commented out line like this:

# ModPagespeedRewriteLevel CoreFilters

As the comment block above that line explains, the default rewrite level of CoreFilters will give you a basic set of optimizations that are safe for most web pages. Uncomment that, and restart your Apache server.Now, if you load any pages from your server, you should notice a few things happening. For example, multiple small CSS files will be combined into one tag inline in your page. mod_pagespeed is smart about the way it handles this: it weighs the potential value of caching that could be derived from serving the CSS files separately against the cost of those extra HTTP requests. So, only files of a certain size or larger will be inlined like this (of course, you can configure this size threshold yourself).Those filters are pretty conservative, so let’s enable some more! Further still down the conf file (line 46 for me), there’s a line like this:

# ModPagespeedEnableFilters collapse_whitespace,elide_attributes

The ModPagespeedEnableFilters declaration simply takes a comma-separated list of filters you’d like to enable. By default, it’s written out with collapse_whitespace (which, as the name implies, will collapse superfluous whitespace characters in your HTML files) and elide_attributes. That latter is a tricky one (and an indicator of how serious Google is about byte-counting): it will shorten any HTML attributes which make no difference to the browser. So, for example, disabled="disabled" works exactly the same as just disabled, so mod_pagespeed will change the former to the latter. type="text" is the default for input elements, so mod_pagespeed will drop it entirely.Once you’ve uncommented that line, you can experiment with all the other available filters by stringing them to the end of it. There’s remove_comments, which strips comments from your HTML (but is intelligent enough not to remove your IE conditional comments). There’s optimize_images, which re-scales, re-compresses, and strips metadata from images loaded via img tags. For the totally obsessive among you, there’s even remove_quotes, which strips unnecessary quotation marks from around HTML attributes (so class="description" will become class=description).The full list of available filters and descriptions of how they work and how to use them is available on the project’s Google Code page

What else?

mod_pagespeed also comes with some simple ways to track statistics with regards to page performance. To do this, it injects tiny JavaScript snippets into the top and very bottom of your pages, and uses those to report back to the server on load times. This way, you can measure the effects of various different filters and decide which ones you’d like to keep using.So, will you be rushing to set up mod_pagespeed on your servers?

note:Want more?

If you want to read more from Louis, subscribe to our weekly tech geek newsletter, Tech Times.

Louis joined SitePoint in 2009 as a technical editor, and has since moved over into a web developer role at Flippa. He enjoys hip-hop, spicy food, and all things geeky.

Integromat Tower Ad