How to install Apache mod_pagespeed on CentOS with Plesk

Apache-LogoI bumped into Kristian Markroft from Simplyroot in New Orleans last week, and he told me about an interesting speed-up module for the Apache Webserver.

mod_pagespeed is an open source project which speeds up page loads without having to change the code of the actual page. mod_pagespeed does this by adding filters before pages are served. For example it will resize images and minify CSS/JS files, which can speed up page load considerably. The project is hosted on Google Code:

  • https://code.google.com/p/modpagespeed/

Let’s see how we can install it on CentOS, test to see if it works and how to manage it in Plesk.

Installing mod_pagespeed

The easiest way to install the module is by pulling the pre-built packages via yum from Google. For this we need to add a new source for yum to access. This source only contains the mod_pagespeed module, none of your other packages will be affected (even when you casually yum update your server).

Let’s add it by creating the following file:

Add the following code block to it:

In case you’re not familiar with vi: hit a to enter edit mode, paste your code, hit escape followed by shift-Z-Z to save and exit.

Now we can install the module:

Nice. Now restart Apache for the changes to take effect:

Thanks to Rahul Kumar for explaining this procedure here: http://tecadmin.net/installing-mod_pagespeed-module-apache/

Verify that mod_pagewrite is working

The easiest way to do this is not from a web browser but from the command line. We’ll load a website and inspect the header information:

The last line tells us that mod_pagespeed was indeed involved and is working fine. Needless to say, please replace example.com with the website you’d like to test.

Managing mod_pagespeed

If yo’d like to turn the module off or on again, and you’re using Plesk, it’s as simple as (un-)ticking a box: head over to Tools and Settings (or Server if you’re using Power User View), click Apache Modules and find two new modules in the large list:

  • pagespeed (ticked after installation)
  • pagespeed_ap24

Screen Shot 2014-03-04 at 09.37.58

The latter is for Apache 2.4 which will only start if you actually have Apache 2.4 installed. If you’re running Apache 2.2 then the first module is for you and you’ll receive an error message should you try to start the other one.

If you are not using Plesk, or you prefer a more “bare bones command line” approach you can tweak the mod_pagespeed configuration file which lives in /etc/httpd/conf.d/pagespeed.conf:

Simply change that line to ModPagespeed off, save the file and restart Apache.

Removing mod_pagespeed

To remove the module completely, erase the package and remove the extra yum source:

It’s like it never happened πŸ˜‰

A small caveat to using mod_pagespeed is that changes to your website’s CSS do not show up immediately due to internal caching. Therefore I don’t recommend it on development sites as it gets in the way of actual results.

Further Reading

  • https://code.google.com/p/modpagespeed/
  • https://developers.google.com/speed/pagespeed/module/faq
  • http://www.modpagespeed.com
  • http://tecadmin.net/installing-mod_pagespeed-module-apache/

Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.

23 thoughts on “How to install Apache mod_pagespeed on CentOS with Plesk

  1. Hi Jay, thanks for the write up.

    Do you know how well this works in Plesk 11 when you have nginx handling the static file requests? My Plesk server also runs on CentOS and I’ve been looking at installing Apache mod_pagespeed, but wasn’t sure how well this would work with Plesk’s nginx / Apache setup.

    1. Hi Andy,

      glad you liked it! I’ve had mod pagespeed running on a test server for over a month and it works really well with Plesk 11.5. All my sites are WordPress installs, where NGINX only serves static files (graphics and plain HTML, including Super Cached files). Works a treat!

      I’ve just added it to my production servers and results are great – no adverse effects at all, but several sites with complex themes show a speed improvement (including this one).

      Let me know your experience – would be nice to get another perspective.

      All the best,

      JAY

      1. Hi Jay,

        Thanks for taking the time to reply. I finally got round to installing this and it seems to be working well.

        I don’t know too much about what mod_pagespeed does behind the scenes, but from what I’ve read it makes optimisations to images and other resources such as CSS and JS. Like you, I’m currently using Nginx (in Plesk 11.5) to serve the static resources such as images, so I’m guessing for these file types mod_pagespeed wouldn’t have any effect as the request would never reach Apache. But if you’ve noticed a speed improvement then it must be optimising other things which is good!

        I’ll try some more speed tests and see how I get on. Thanks again,

        Andy

        1. From what I understand mod_pagespeed parses all JavaScript and CSS files, and then serves each as one file rather than separate single files. So all CSS and JS is served together and is minified in the process. But the caveat is indeed that those files need to be served by Apache – so all static HTML files that rely on being served by mod_rewrite rules (for example, WP Super Cache’d files) will benefit from a speed increase – but anything served by NGINX won’t.

          I guess what would be really beneficial is a speed compare between these variations:

          • NGINX on / mod_pagespeed on
          • NGINX off / mod_pagespeed on
          • NGINX on / mod_pagespeed off

          all with and without WP Super Cache active. What’s a good website benchmark tool? Would be good to have some proper figures.

    1. Sure, in fact it’s perfect timing – Plesk 12 has just been released yesterday. Is there any particular aspect you’re interested in? Administering Plesk is a complex topic and probably deserves it’s own full length course to cover all aspects.

      1. I wil be interested in backing up my websites and emails correctly and how to upgrade my Plesk panel safely. Plus any other performance tips you might have including setting up Google’s page speed service. As I’m new to this it all helps πŸ™‚

    1. Pass I’m afraid – I bet it has to do with the multi PHP setup which I’ve never explored on my servers. Im not even sure how Plesk applies those.

      I’m running PHP 5.4 and mod_pagespeed works fine – but all subscriptions are running the same version of PHP.

  2. I tried It, I have ran into an error

    Error: File contains no section headers.
    file: file:///etc/yum.repos.d/google-mod-pagespeed.repo, line: 2
    ‘ogle-mod-pagespeed]\n’

    And I have tried to remove the file but failed. How can i solve this? Thanks

    1. Looks like there was a problem with how you copied and pasted the text into your file. The error is rather clear and suggests your file reads ‘ogle-mod-pagespeed’ instead of ‘google-mod-pagespeed’.

      Try comparing the text visually line by line.

      1. I did notice that, I even tried correcting it but got no where with it, it did not accept edits, I tried deleting the file and got the bellow results.

        E325: ATTENTION
        Found a swap file by the name “/etc/yum.repos.d/.google-mod-pagespeed.repo.swp”
        owned by: root dated: Thu Jan 1 12:38:35 2015
        file name: /etc/yum.repos.d/google-mod-pagespeed.repo
        modified: YES
        user name: root host name: mydomain.com
        process ID: 3678
        While opening file “/etc/yum.repos.d/google-mod-pagespeed.repo”
        dated: Thu Jan 1 08:39:36 2015

        (1) Another program may be editing the same file.
        If this is the case, be careful not to end up with two
        different instances of the same file when making changes.
        Quit, or continue with caution.

        (2) An edit session for this file crashed.
        If this is the case, use “:recover” or “vim -r /etc/yum.repos.d/google-mod-p
        agespeed.repo”
        to recover the changes (see “:help recovery”).
        If you did this already, delete the swap file “/etc/yum.repos.d/.google-mod-
        pagespeed.repo.swp”
        to avoid this message.

        Thanks for the reply πŸ˜‰

        1. Remove the vi swap file like this:

          Then edit the file again. Using vi isn’t exactly intuitive πŸ˜‰

Add your voice!