Mounting A USB Memory Stick As An Apache Folder

If you have run out of space on your Pi's SD card for large files to be served by Apache, you can add a USB stick and configure Apache to use that instead. This is really a temporary fix due to possible performance limitations of the USB stick, failure to mount after a reboot, etc.

In this example we will mount the USB device at /media/usb. The existing folder that is to be superseded is presumed to be old-folder in the directory identified as DocumentRoot in the configuration file.

At the Pi command line, create a directory to act as the mount point:

sudo mkdir /media/usb

Now add a line in /etc/fstab so that the filesystem is mounted at boot time. This example presumes the USB device is called sda1:

/dev/sda1 /media/usb vfat defaults 0 0

To enable Apache to access this folder and serve files from it and because it lies outside Apache's view of the world at DocumentRoot, you need to create an alias for it in your sebsite's configuration file. Configuration files are stored in /etc/apache2/sites-enabled. If you only have a single website the file is called 000-default by default. If you have other websites configured, then you'll know what their configuration files are called. However, before making any changes, reload Apache to confirm that what you already have is correct:

sudo service apache2 reload

You should see some messages and a green ok. If not, you should correct your configuration before making any changes. If all is correct, edit the configuration file:

sudo nano /etc/apache2/sites-enabled/000-default

Add the following line within the directives section:

Alias "/old-folder" "/media/usb"

Save the changes and reload the Apache configuration:

sudo service apache2 reload

If you see an error message then there's something wrong with what you typed. We know this because you successfully reloaded the unchanged configuration previously.

Finally, rename your old folder to something different and then test that a request for a file from /old-folder works. Note that your web clients still refer to /old-folder but it's Apache that uses /media/usb instead.