Storing Web Cache on a RAM Disk: Part Two

In part one, you learned how to create a basic RAM disk and how to make it permanent by creating an entry in /etc/fstab. In this section, you will learn how to configure your Apache to send certain files to your RAM drive.

In this example, we will only send images to the RAM drive. Special thanks to NixCraft for the info.

First copy your images to the ram drive you created:

cp /home/user/www/public_html/images/*.jpg /webcache

Next, you will need to edit your Apache configuration to point any visitors to images.yourdomain.tld to the right place. This assumes you want to cache your images and that you have created a subdomain for those image files.


ServerAdmin admin@yourdomain.tld

ServerName images.yourdomain.tld

DocumentRoot /webcache

#ErrorLog /var/logs/httpd/images.yourdomain.tld_error.log

#CustomLog /var/logs/httpd/images.yourdomain.tld _access.log combined


Then, reboot Apache:

# service httpd reload

Finally, to ensure your images are not deleted upon reboot, you will need to create a script called and place it in /etc/rc.local. You can use code similar to this:


mkfs -t ext2 -q /dev/ram1 8192

[ ! -d /webcache ] && mkdir -p /webcache

mount /dev/ram1 /webcache

/bin/cp /home/user/public_html/images/*.jpg /webcache

Make sure you make the script executable:

# chmod +x /path/to/

You should now have a working cache for your images that loads them into RAM and gives your website viewers a much faster experience.