Speed up apache webserver with mod_pagespeed and memcached on Ubuntu

Google has developed the apache modulemod_pagespeed” to optimize and streamline the content delivery of the apache web server which reduces the load times of pages, especially when they use many assets like CSS files, javascript includes and images. The pagespeed module uses a filesystem based cache by default.

Configure pagespeed to use Memcached to store the cached items in memory which is faster than the default cache method. This Tutorial can be used on any apache Installation

Installation of mod_pagespeed
Google provides Debian packages for mod_pagespeed for 32 and 64-bit installations.

Debian 64 bit
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_amd64.deb
sudo dpkg -i mod-pagespeed-stable_current_amd64.deb

Debian 32 bit
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_i386.deb
sudo dpkg -i mod-pagespeed-stable_current_i386.deb

Restart apache to activate the pagespeed module
sudo service apache2 restart

Installation of Memcached
The Memcached package is available in the Ubuntu repositories

sudo apt-get install memcached

Check if memcached is running

netstat -tap | grep memcached

The result shall look similar to this:

tcp 0 0 localhost.localdo:11211 *:* LISTEN 27420/memcached

In the output, we see the port were Memcached is listening on: 11211, (need that port for the next step)

Configure mod_pagespeed to use memcached

sudo nano /etc/apache2/mods-available/pagespeed.conf

find the line
[….]
# ModPagespeedMemcachedServers localhost:11211
[….]

and remove the #, so that it looks like this:
[….]
ModPagespeedMemcachedServers localhost:11211
[….]

If there is plenty of memory in the server, then get a further speedup by removing the # in front of this line:
[….]
ModPagespeedCreateSharedMemoryMetadataCache “/var/cache/mod_pagespeed/” 51200
[….]

Restart apache to apply the changes:

sudo service apache2 restart

Now mod_pagespeed will use memcached as storage backend.

Get usage statistics from memcached with the following command:

echo stats | nc 127.0.0.1 11211