Speed up apache webserver with mod_pagespeed and memcached on Debian 7 (Wheezy)


The page load time gets more and more important for websites to provide a better user experience and it is important for the search engine ranking as well. Google has developed the apache module “mod_pagespeed” to optimize and streamline the content delivery of the apache webserver 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, in this tutorial I will configure pagespeed to use memcached to store the cached items in memory which is faster then the default cache method. This Tutorial can be used on any apache Installation. It is compatible with the ISPConfig 3 Perfect Server tutorials.

Installation of mod_pagespeed

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

Debian 64 bit (this should be the right for most current servers)

cd /tmp
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_amd64.deb
dpkg -i mod-pagespeed-stable_current_amd64.deb

Debian 32 bit (this should be the right for most current servers)

cd /tmp
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_i386.deb
dpkg -i mod-pagespeed-stable_current_i386.deb

finally restart apache to activate the pagespeed module

service apache2 restart

Installation of memcached

The memcached package is available in the Debian repositories, so we can install it with apt directly

apt-get install memcached

Now check with the command “netstat -tap | grep memcached” if memcached is running

netstat -tap | grep memcached

The result shall look similar to this:

[email protected]:/tmp# netstat -tap | grep memcached
tcp 0 0 localhost.localdo:11211 *:* LISTEN 27420/memcached

In the output we see the port were memcached is listening on: 11211, we need that port for the next step.

Configure mod_pagespeed to use memcached

vi /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 you have plenty of memory in your server, then you can get a further speedup by removing the # in front of this line:

ModPagespeedCreateSharedMemoryMetadataCache “/var/cache/mod_pagespeed/” 51200

Finally restart apache to apply the changes:

service apache2 restart

Now mod_pagespeed will use memcached as storage backend. You can get usage statistics from memcached with the following command:

echo stats | nc 11211

[email protected]:/tmp# echo stats | nc 11211
STAT pid 27420
STAT uptime 422
STAT time 1397644927
STAT version 1.4.13
STAT libevent 2.0.19-stable
STAT pointer_size 64
STAT rusage_user 0.012000
STAT rusage_system 0.000000
STAT curr_connections 5
STAT total_connections 6
STAT connection_structures 6
STAT reserved_fds 20
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 0
STAT get_misses 0
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 6
STAT bytes_written 0
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT evictions 0
STAT reclaimed 0

Press [ctrl] + c to get back to the command prompt.