Speed up Apache with mod_pagespeed and Memcached on Debian 8

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 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, in this tutorial I will 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, it is compatible with the ISPConfig 3 Perfect Server tutorials and the Debian LAMP tutorial.

1 Prerequisites

  • Debian 8 with Apache web server installed.
  • Root access to the server

I will use the Debian LAMP setup as the basis for my installation.


2 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


3 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:

root@server1:/tmp# netstat -tap | grep memcached
tcp 0 0 localhost:11211 *:* LISTEN 35396/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

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 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
root@server1:/tmp# echo stats | nc 11211
STAT pid 35396
STAT uptime 72
STAT time 1458293309
STAT version 1.4.21
STAT libevent 2.0.21-stable
STAT pointer_size 64
STAT rusage_user 0.008000
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 malloc_fails 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT evictions 0
STAT reclaimed 0
STAT crawler_reclaimed 0
STAT lrutail_reflocked 0

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

Virtual Machine image

This tutorial is available as ready to use virtual machine in OVA / OVF format for Howtoforge subscribers. The VM format is compatible with VMWare and Virtualbox and other tools that can import this format. You can find the download link in the right menu on the top. Click on the file name to start the download.

The login details of the VM are:

SSH Login

Username: root
Password: howtoforge

MariaDB Login

Username: root
Password: howtoforge

Please change the passwords after the first boot.

The VM is configured for the static IP, the IP can be changed in the file /etc/network/interfaces.

Share this page:

2 Comment(s)