Raspberry Pi boots up from the sd card that is usually limited in space. No wonder that one of the first tasks in hand is to connect some sort of external hard drive.
Connecting an external device is fairly simple (just plug it in and format into ext4 file system), but making this external storage used by a web server is not so obvious.
I’ve tried few options before finding the simplest one for me, which is mounting external storage into the web server folder.
To do it, start with finding the external storage device name
df -h
This will output a list of storage, one of which we will be using, it should have a name like “sda” or “sdb”. SDA – stands for a SCSI Hard-disk a.
Once identified, mount it to default web server folder
mount /dev/sda /var/www
This will mount device name “sda” to the folder “/var/www” until next reboot or until you pull the plug to the external device.
Once mounted we need to address files and folders ownership. Change owner of everything in the folder /var/www to user, group www-data
sudo chwon www-data:www-data -R /var/www
To mount it automatically at every reboot little more fiddling needs to be done by either modifying “/etc/fstab” which can be dangerous or creating cron job at reboot which is a little less dangerous.
The correct way is modifying “/etc/fstab”, I know that, but there is a but. I have tried modifying /etc/fstab, but made a mistake that resulted in a fatal crash at boot and I had to reinstall everything. Which taught me 2 things: First – I need backups, Second – don’t touch /etc/fstab unless absolutely necessary or you know what you doing.
To mount a device using cron job, find file system on the external drive by typing:
df -Th
Column Type. Cron job has to be done from root user, so use “sudo”
sudo crontab -e
Crontab needs an empty line at the end, so add one line before last.
@reboot sleep 30&&mount -t ext4 /dev/sda /var/www
Save the file. What we are trying to achieve is: Start this cron job only at boot, wait for 30 seconds, so everything finishes loading, then try to mount the device.
Testing: Reboot the Raspberry Pi and see if the external device is mounted