Speed-up your pages with mod_pagespeed

Last week Google released two new tools to help people make their websites faster.

The first one is a Firefox extension called “Page Speed” which gives you a performance analysis of the website you’re viewing as well as lots of tips on how you can make the website faster if you’re the website owner.

The second one is an Apache module called “mod_pagespeed”. Enabling it in Apache causes Apache to analyze the resources it’s serving and optimize them on the fly before serving them. We have installed mod_pagespeed on all of our servers (except web1-web20 which run rhel4). It is disabled by default for all websites but if you are using a php/cgi/php app or a PHP-based app like WordPress, Drupal, Joomla etc. all you have to do to enable mod_pagespeed for your website is to add this line to your .htaccess file (or create a new .htaccess file if you don’t have one yet):

SetOutputFilter MOD_PAGESPEED_OUTPUT_FILTER

If you’re using a Django app or any other app that comes with your own Apache instance you can also easily enable mod_pagespeed like this (assuming your app is called “django” and your username is “username“):

mkdir -p $HOME/mod_pagespeed/cache/ $HOME/mod_pagespeed/files
cd $HOME/webapps/django/apache2/modules
cp /usr/lib/httpd/modules/mod_pagespeed.so .
vi ../conf/httpd.conf # Or another editor if you think "vi" is too simple...
    # add the following:
    LoadModule pagespeed_module modules/mod_pagespeed.so
    ModPagespeed on
    SetOutputFilter MOD_PAGESPEED_OUTPUT_FILTER
    ModPagespeedFileCachePath            "/home/username/mod_pagespeed/cache/"
    ModPagespeedGeneratedFilePrefix      "/home/username/mod_pagespeed/files/"
    # save and exit
cd ../bin; ./restart

If you’re using your own Apache instance you can also configure which filters you want to enable or disable. If you’re using a static/php/cgi or a php-based app like WordPress, Drupal or Joomla then it is not currently possible to configure which filters you want because the current version of mod_pagespeed doesn’t seem to support filter configuration per virtual hosts. For these apps you will be using the default “core filters” chosen by Google. These are:

  • add_head: adds a head to the document if it encounters a <body> tag before finding a <head> tag.
  • combine_css: replaces multiple distinct CSS files with a single CSS file, containing the contents of all of them.
  • rewrite_css: parses referenced and inline CSS and minifies them.
  • rewrite_javascript: parses referenced and inline javascript code and minifies it.
  • inline_css: reduces the number of requests made by a web page by inserting the contents of small external CSS resources directly into the HTML document.
  • inline_javascript: reduces the number of requests made by a web page by inserting the contents of small external JavaScript resources directly into the HTML document.
  • rewrite_images: rescales, and compresses images; inlines small ones.
  • insert_img_dimensions: inserts width= and height= attributes into <img> tags that lack them and sets them to the image width and height.
  • extend_cache: rewrites the URL references in the HTML page to include a hash of the resource content. Thus if the site owner changes the resource content, then the URL for the rewritten resource will also change.

For the official docs on mod_pagespeed and its filters see http://code.google.com/speed/page-speed/docs/using_mod.html

Enjoy the new mod_pagespeed and let us know if you notice any difference in the loading time for your pages.

Update: Be aware that mod_pagespeed is still in beta and therefore might have some bugs. In particular we’ve had reports saying that it can saturate the number of Apache processes available for your site and cause some requests to hang. We’ll roll-out future versions as Google release them but in the mean time if mod_pagespeed is causing problems for your particular site the best thing to do is to just not enable it for your site for now.

Update 1 August 2011: Removed deprecated ModPagespeedUrlPrefix directive.

This entry was posted in General. Bookmark the permalink.

6 Responses to Speed-up your pages with mod_pagespeed

  1. Pingback: Tweets that mention Speed-up your pages with mod_pagespeed | WebFaction Blog -- Topsy.com

  2. I’m very excited about these changes, thanks for keeping up with the latest technologies!

  3. Robert S. says:

    Holy cr*p, could be my imagination, but mod_pagespeed seems to have produced near instantaneous page loads!

  4. Pingback: wp-popular.com » Blog Archive » Speed-up your pages with mod_pagespeed | WebFaction Blog

  5. Brian S. says:

    Just installed mod_pagespeed on both of my hosting servers at BsnTech Networks. I have noticed a little bit of improvement but not a lot.

    Note that when using the “AddOutputFilterByType” directive – there must be TWO arguments passed in. When I removed the last argument, Apache wouldn’t reload. Therefore, mine looks like:

    AddOutputFilterByType MOD_PAGESPEED_OUTPUT_FILTER text/html

  6. mynet okey says:

    how can i use it in htaccess only?

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>