Friday, June 17, 2011

Apache mod_cache (Ubuntu 10+)

You need to edit the /etc/apache2/mods-available/disk_cache.conf the command will be ---

gedit /etc/apache2/mods-available/disk_cache.conf


Make sure you uncomment the CacheEnable disk / line, so that the minimal configuration looks as follows:
 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>

Now we can enable mod_cache and mod_disk_cache:
sudo a2enmod cache
sudo a2enmod disk_cache

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:
sudo 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...
gedit /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.

1 comment:

Max said...

Not sure about Ubuntu 10+, but for Debian Squeeze htcacheclean daemon is launched when mod_disk_cache enabled: no need to configure it manually (see README.Debian.gz)