Separate volumes

Published on Author adventurerLeave a comment

This post assumes you’re using EC2 instances with Amazon Linux – some of the specific commands and file locations may vary if you’re using other variants of Linux

By default AWS starts all instances with a single root volume. Although this can be an EBS (Elastic Block Store, i.e. persistent) or ephemeral, it is deleted as and when the instance is terminated. Should you be using a spot instance to host your website this is a significant risk (it may sound crazy to host a public facing website on a spot instance but when you’re starting out it can be significantly cheaper unless your eligible for the AWS Free Tier!). Even if you ask for an additional volume at startup this will not be mounted by default and all transient and state data will be stored on the root volume, under /var.

If you want to ensure that the code you deploy and more importantly the state data that is collected is retained even if the instance dies it is better to put these on a separate volume. Using a standard EBS volume is ok for this for a very small site with low traffic, however this architecture will not scale out. AWS have just announced a new service, Elastic File System, which will overcome this by allowing you to create a volume which can be mounted by multiple instances using NFS, although at the time of writing it is still an invitation-only preview service.

To move the /var directory onto a different EBS volume, first create an EBS volume and attach it to the relevant EC2 instance – the steps below assumed this is attached to /dev/sdf (also referred to as /dev/xvdf ).

The stop the httpd (Apache) service if it is running:

sudo service httpd stop

Temporarily rename the existing /var/www/html directory:

cd /var/www
sudo mv html html.old

Make a new, blank /var/www/html directory and set the permissions appropriately – this will be the mount point for the new EBS volume:

sudo mkdir html
sudo chgrp www html
sudo chmod 2775 html

Format the additional volume you attached in the above step as ext4 file system:

sudo mkfs -t ext4 /dev/sdf

Mount the file system at the new /var/www/html directory:

mount -t ext4 /dev/sdf /var/www/html

Restart the httpd (Apache) service:

sudo service httpd start

Make a new, separate folder as the wwwroot and set the permissions accordingly:

mkdir /var/www/html/wwwroot
sudo chown root /var/www/html/wwwroot

In order to use this new folder as the Apache document root, the Apache configuration needs to be amended, by editing the httpd.conf file:

sudo vi /etc/httpd/httpd.conf

Find the DocumentRoot directive, and edit it to read as follows:

DocumentRoot /var/www/html/wwwroot

Apache is now up-and-running with the new volume mounted, however we also need to ensure that this volume will be mounted at start up, by amending the fstab file:

sudo vi /etc/fstab

Add the following line:

/dev/xvdf /var/www/html ext4 defaults 0 2

There you go – your Apache Linux instance now has two volumes, with everything under /var/www/html on a second volume. Should the instance fail or be terminating the second volume with the data will remain available, and can be attached a new instance to restore the data or to an existing running instance in order to recover the data.

Leave a Reply

Your e-mail address will not be published. Required fields are marked *