Delving the depths of computing,
hoping not to get eaten by a wumpus

By Timm Murray

Running Apache files on a Raspberry Pi ramdisk with mod_perl

2014-11-06


When used as a web app server, the Raspberry Pi often hosts a small number of static files that rarely change. Although the Raspberry Pi Model B(+) only has 512 MB of RAM, using 10MB for a ramdisk is usually more than enough.

Files will be copied by Apache at startup. If you make changes to these files, you’ll either need to copy them manually, or restart Apache.

Start by making the directory mount point. I used /var/www-ramdisk:

# mkdir /var/www-ramdisk

Modern linux systems mount ramdisks through ‘tmpfs’, so add an entry to /etc/fstab:

tmpfs /var/www-ramdisk tmpfs nodev,nosuid,uid=[UID],gid=[GID],size=10M 0 0

Replace [UID] and [GID] with the respective uid and gid that Apache runs under for your system. On the default Raspbian install, this will be the www-data user and group.

Run mount -a and the ramdisk should appear (use df to confirm).

Next comes the Apache config. Somewhere in the config, you’ll need a PerlConfigRequire:

PerlPostConfigRequire /etc/apache2/mod_perl_post_config.pl

Create /etc/apache2/mod_perl_post_config.pl, an write in:

# Copy files to the RAM disk
(system( 'cp -R /var/www/* /var/www-ramdisk' ) == 0)
        or die "Could not copy files to ramdisk: $!\n";
1;

This shells out during startup to recursively copy everything from the default Apache docroot to the ramdisk.

Now grep through your Apache config, and change all paths to /var/www and replace it with /var/www-ramdisk. Lastly, restart Apache with:

# /etc/init.d/apache2 restart

Check the files with ls -l /var/www-ramdisk and you should see everything that’s in /var/www.

Edit: Forgot to credit domoticz.com, where I got much of the fstab setup.



Copyright © 2024 Timm Murray
CC BY-NC

Opinions expressed are solely my own and do not express the views or opinions of my employer.