Caching With Apache's mod_cache On Debian Etch

Version 1.0
Author: Falko Timme
Last edited 12/23/2008

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

You can find explanations for these configuration options and further configuration options on

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.

Share this page:

4 Comment(s)

Add comment


From: Simon Holywell at: 2009-05-14 10:05:20

If you set the Pragma header to no-cache it will also stop mod_cache from caching your webpage.

From: bl0m5t3r at: 2009-05-09 18:23:41

yes it is possible, i use the following statement in my .htaccess

<IfModule mod_headers.c>
# Turn on Expires and set default to 0
ExpiresActive On
ExpiresDefault A0
# Set up caching on media files for 1 year (forever?)
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
ExpiresDefault A29030400
Header append Cache-Control "public"

# Set up caching on media files for 6 month
<FilesMatch "\.(gif|jpg|jpeg|png|swf)$">
ExpiresDefault A14515200
Header append Cache-Control "public"
# Set up 1 month caching on commonly updated files
<FilesMatch "\.(xml|txt|html|css|js)$">
ExpiresDefault A2419200
Header append Cache-Control "proxy-revalidate"
# Force no caching for dynamic files
#<FilesMatch "\.(php|cgi|pl|htm)$">
#ExpiresActive Off
#Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform"
#Header set Pragma "no-cache"

I guess with the above example you can adjust it to every one of your sites.

From: Norio at: 2009-01-27 13:56:27

I have multiple sites on my server.  Some can be cached for 10 minutes, others for 1 minute, others for days at a time.  Is it possible to have a separate configuration for each using .htaccess files or something?

From: raghu at: 2011-05-25 05:11:04

The cachetest.php behaves in the same way though i enable or disable mod_cache