Caching With Apache's mod_cache On Debian Etch
Version 1.0
Author: Falko Timme
This article explains how you can cache your web site contents with Apache's mod_cache on Debian Etch. If you have a high-traffic dynamic web site that generates lots of database queries on each request, you can decrease the server load dramatically by caching your content for a few minutes or more (that depends on how often you update your content).
I do not issue any guarantee that this will work for you!
1 Preliminary Note
I'm assuming that you have a working Apache2 setup (Apache 2.2.x - prior to that version, mod_cache is considered experimental) from the Debian repositories - the Apache version in the Debian Etch repositories is 2.2.3 so you should be good to go.
I'm using the document root /var/www here for my test vhost - you must adjust this if your document root differs.
2 Enabling mod_cache
mod_cache has two submodules that manage the cache storage, mod_disk_cache (for storing contents on the hard drive) and mod_mem_cache (for storing contents in memory which is faster than disk caching). Decide which one you want to use and continue either with chapter 2.1 (mod_disk_cache) or 2.2 (mod_mem_cache).
2.1 mod_disk_cache
The mod_disk_cache configuration is stored in /etc/apache2/mods-available/disk_cache.conf, so let's edit that one:
vi /etc/apache2/mods-available/disk_cache.conf
Make sure you uncomment the CacheEnable disk / line, so that the minimal configuration looks as follows:
# a2enmod-note: needs-configuration <IfModule mod_disk_cache.c> CacheRoot /var/cache/apache2/mod_disk_cache # If you enable disk caching, you need to use htcacheclean from the # apache2-utils package to ensure that the cache does not grow indefinitely. # See the htcacheclean man page for details. # There is currently no mechanism in the Debian package to start htcacheclean # automatically, but it is planned to add such a mechanism in the future. CacheEnable disk / CacheDirLevels 5 CacheDirLength 3 </IfModule> |
You can find explanations for these configuration options and further configuration options on http://httpd.apache.org/docs/2.2/mod/mod_disk_cache.html.
Now we can enable mod_cache and mod_disk_cache:
a2enmod cache
a2enmod disk_cache
/etc/init.d/apache2 force-reload
To make sure that our cache directory /var/cache/apache2/mod_disk_cache doesn't fill up over time, we have to clean it with the htcacheclean command. That command is part of the apache2-utils package which we install as follows:
apt-get install apache2-utils
Afterwards, we can start htcacheclean as a daemon like this:
htcacheclean -d30 -n -t -p /var/cache/apache2/mod_disk_cache -l 100M -i
This will clean our cache directory every 30 minutes and make sure that it will not get bigger than 100MB. To learn more about htcacheclean, take a look at
man htcacheclean
Of course, you don't want to start htcacheclean manually each time you reboot the server - therefore we edit /etc/rc.local...
vi /etc/rc.local
... and add the following line to it, right before the exit 0 line:
[...] /usr/sbin/htcacheclean -d30 -n -t -p /var/cache/apache2/mod_disk_cache -l 100M -i [...] |
This will start htcacheclean automatically each time you start the server.