Tutorial

How To Get Started With mod_pagespeed with Apache on a CentOS and Fedora Cloud Server

Published on July 10, 2013
author

Shaun Lewis

How To Get Started With mod_pagespeed with Apache on a CentOS and Fedora Cloud Server

Introduction

One of the more recently popular modules for Apache is mod_pagespeed. It is an output filter for Apache 2.2+ that can be configured through a variety of options through configuration files or a .htaccess file. An “output filter” is a something that transforms the data before it’s sent to the client. In other words, it’s a layer between your website and what the user’s browser receives when they visit your URL.

Speed Up the Web

The goal of mod_pagespeed is to speed up your website. It does this by applying filters to a variety of files in order to reduce the number of trips the browser has to make to grab what it needs, to reduce the size of those files and to optimize the length those files are cached.

Installation

Installation is very simple. It’ll vary depending on the operating system you use. Ubuntu and Debian have packages you can download and install (or any Linux distribution that uses .DEB packages). Other Linux distributions can download the source and build from that.

If you’re on a 64-bit version (likely)...

wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_x86_64.rpm

If you’re on a 32-bit version (less likely)...

wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_i386.rpm

Follow up with:

yum install at
(If you do not already have 'at' installed)
rpm -U mod-pagespeed-*.rpm

Remove the downloaded package

rm mod-pagespeed-*.deb

Note: Installing from source is outside the scope of this article. You can find detailed instructions from Google here: Build Mod_Pagespeed from Source

The module enables itself automatically when installed. However, you must restart Apache for it to start working.

/etc/init.d/httpd restart

You should now have a working version of mod_pagespeed up and running on your VPS. You can check this by looking at your page’s response headers. There should be a value for “X-Mod-Pagespeed” with the version number you installed.

Setup

The installation package handles a lot of configuration out-of-the-box. In fact, there are conservative defaults that are automatically enabled on Apache. Depending on the Apache version you’re running, you’ll get a different version of the module installed and enabled. If you’re running Apache 2.2, mod_pagespeed.so will be installed; Apache 2.4 users will use mod_pagespeed_ap24.so.

Note: mod_pagespeed only works with Apache 2.2 and greater. There is also a bug with Apache 2.4.1 that prevents it from working with that version. Apache 2.4.2 or greater should be used.

Additionally, configuration files have been added to your Apache installation. The primary configuration file is pagespeed.conf. This file is located at:

/etc/httpd/conf.d/

Configuration

If you wanted to, you could stop now. The defaults for mod_pagespeed are good, but you’ll often find that you can get better performance with a few additional tweaks to your site. Every site will get different results with different settings and it’s best to play around and find the settings that work best for you and your site.

For the purposes of this tutorial, we’ll go over a few of the more common settings.

How to configure mod_pagespeed

There are a few different ways mod_pagespeed can be configured. You can use the pagespeed.conf file described above to configure it for the whole cloud server. Or, if you’d rather, you can put your configuration settings in the VirtualHost directive for an Apache virtual host/website. Finally, you have the option of specifying directives in a .htaccess file, such as what most sites do for mod_rewrite.

The least performant of these options is the .htaccess file because it has to be loaded with every request. The pagespeed.conf file is loaded when Apache starts, so it’s the ideal place to store your configuration settings. Inside the VirtualHost directive is also preferable to inside your site’s htaccess file for the same reason. That’s a good place to put site-specific settings too.

You can use whatever text editor you want to edit the configuration file. For this tutorial, we’ll be using nano.

To start editing the main configuration file, use the following command:

nano /etc/httpd/conf.d/pagespeed.conf

Basic Settings

In general, the settings in pagespeed.conf are pretty well documented inside the file. There is also a great list of filter examples available from http://www.modpagespeed.com. Here are a few common settings you might want to play with to optimize for your site’s performance.

Turn mod_pagespeed On/Off

First off, you can turn the module on or off with the ModPagespeed setting.

ModPagespeed on

or

ModPagespeed off

Rewrite Levels

You can specify different “levels” of settings to simplify any configuration. The default is “CoreFilters.” It contains a set of filters the Google team believes is safe for use. The filters are the individual actions that are applied to a file. In general, you won’t need to change this value. It’s easier to use this default and then enable or disable filters using the ModPagespeedEnableFilters and ModPagespeedDisableFilters directives.

The default setting:

ModPagespeedRewriteLevel CoreFilters

To disable CoreFilters use this setting:

ModPagespeedRewriteLevel PassThrough

Note: You’ll have to explicitly enable any filters you want to turn on using the “PassThrough” setting.

Using the default “CoreFilters” rewrite level includes a number of filters by default. As of the time of this writing, it includes:?

add_head
combine_css
convert_jpeg_to_progressive
convert_meta_tags
extend_cache
flatten_css_imports
inline_css
inline_import_to_link
inline_javascript
rewrite_css
rewrite_images
rewrite_javascript
rewrite_style_attributes_with_url

New filters will be added in the future. By using CoreFilters, you’ll automatically have these filters enabled if they become part of the default set whenever you update mod_pagespeed. Using PassThrough will require you to explicitly enable the new filters.

Enable Filters

If you’d like to enable additional filters, you can pass them as a comma-separated list to ModPagespeedEnableFilters. You can have multiple ModPagespeedEnableFilters directives throughout your configuration files. So, if you want to enable a filter per site, you could enable it in the virtual host configuration file or in the .htaccess file instead of in the main pagespeed.conf file.

Here’s an example that enables the Pedantic filter (which adds the type attribute to script and style tags) and the Remove Comment filter (which removes all HTML comments):

ModPagespeedEnableFilters pedantic,remove_comments

Disable Filters

You can also disable filters on a per-case basis if you’d like. Specify a list of filters you’d like to disable similar to

ModPagespeedEnableFilters
The following example disables the “Convert JPEG to Progressive” filter even though it’s part of the CoreFilters set:

ModPagespeedDisableFilters convert_jpeg_to_progressive

Specify Which URLs are Rewritten

By default, mod_pagespeed rewrites everything it can. You can disable certain files (for example Javascript libraries) from being rewritten with the following directive:

ModPagespeedDisallow "*/jquery-ui-*.min.js"

This would disable rewriting of any files that match the wildcard pattern specified (jquery UI in this case).

You can also turn off the rewriting of all files by default and only enable files you want to rewrite manually. You can do this with the following settings:

ModPagespeedDisallow "*" 
ModPagespeedAllow "http://*digitalocean.com/*/styles/*.css" 
ModPagespeedAllow "http://*digitalocean.com/*.html" 
ModPagespeedDisallow "*/notrewritten.html"

The order of execution means that all files at digitalocean.com ending in .html would be rewritten. That last Disallow directive means any URLs matching that pattern would not be rewritten because it overrides the previous setting.

Restart Apache

Don’t forget if you’re using the pagespeed.conf or VirtualHost files to alter the settings, you’ll have to restart Apache for the settings to take effect. You can do this with the following commands:

/etc/init.d/httpd restart

Conclusion

This guide will help you get started using mod_pagespeed. There are a number of other settings and directives that can be applied server-wide or per-site. In addition, mod_pagespeed is under active development so it’s changing every day. For more detailed information, visit the Google-run http://www.modpagespeed.com.

In addition, you can check out the official mod_pagespeed site at https://developers.google.com/speed/pagespeed/mod.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Shaun Lewis

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Anyway to enable pagespeed only for a specific directory?

example: skip everything in /var/www/html/ Enable for, and only for the following directory: /var/www/html/images/project/something/images/large_images/

Good Job

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
July 30, 2014

@askaraidarkhan: Since nginx will be handling static content, it would be better to install mod_pagespeed on nginx.

I would like to install mod_pagespeed . I have no idea if is it reasonable to use with nginx set as front-end proxy. To install mod_speedtest both nginx and apache to make it work, then what to prefer? User <— NGINX <— PageSpeed <— Apache User <— PageSpeed <— NGINX <— Apache

Hi , I have installed page-speed in httpd and able to see the header for page speed enable. but while accessing my sample application its making external function are all embedded in the html or jsp page.

But the cache folder we created and there is no files available. how to link my application with page-speed.

i deployed my sample application in tomcat, browser will request through Apache server, the Apache server will redirect through proxy pass to tomcat then tomcat will response to Apache, then Apache will response to browser. in the time i have created cache folder but its not dropped any static pages or scripts.

Please help us to solve the issues or give steps how to create a connection .

To cleanup change the step: rm ‘mod-pagespeed-*.deb’

to ‘rm mod-pagespeed-*.rpm’

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
September 3, 2013

@Tyssen: I believe it’s working properly as it’s modifying the pages.

Are you experiencing any problems or does it seem fine to you?

In my original comment I asked if I was better off starting from scratch and just installing the stable module as mentioned in this article. Then I asked HOW I go about removing the beta module.

But I’ve since been looking at the code of the sites on my droplet and can see stuff like pagespeed.criticalImagesBeaconInit(‘/mod_pagespeed_beacon’,‘http://www.domain.com/','SkBrK9JthN’); at the bottom of the page so I guess it is working. But I still don’t see anything in the response headers so I’m not sure. :?

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
August 27, 2013

@Tyssen: Why would you install both versions? Remove one of them and install only the other. Does that work?

I have done and after some emailing the author he’s requested root access which I’m not comfortable handing out. So I thought I’d start over with the instructions in this article, but as mentioned in my first comment, when I try to install the stable version alongside the beta, I get dependency conflicts and I want to know what the way around those is.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.