EC2 Guide: Create and attach new EBS volumes (5a / 7)

The EBS volumes that we are about to create will function as external hard drives to our instance. They can only be attached to one instance at a time. If you don't want to use these please make the few changes that are needed to keep this guide compatible here. If you already have these volumes with data on them as described below you need to go here.

Create the EBS volumes

Using Elasticfox you will need to create 3 EBS volumes (Database, Web-server and Mail-server):

Attaching the EBS volumes

Using Elasticfox:

Formatting the EBS volumes

Back to the server, where your new volumes are now attached we need to create a file-system on them. XFS is generally used for this:

sudo mkfs.xfs /dev/sdc 
sudo mkfs.xfs /dev/sdd 
sudo mkfs.xfs /dev/sde 

Mounting the EBS volumes in useful locations

Choose descriptive path names for each!

echo "/dev/sdc /webserver xfs noatime 0 0" | sudo tee -a /etc/fstab 
sudo mkdir -m 000 /webserver 
sudo mount /webserver 

echo "/dev/sdd /database xfs noatime 0 0" | sudo tee -a /etc/fstab 
sudo mkdir -m 000 /database 
sudo mount /database 

echo "/dev/sde /email xfs noatime 0 0" | sudo tee -a /etc/fstab 
sudo mkdir -m 000 /email 
sudo mount /email 

Move MySQL to an EBS volume

This assumes we have an EBS volume with a mount point called "database" attached.

sudo service mysql stop 

Move the existing MySQL parts to run on the EBS volume, but keep their 'place-holders' populated through mount points.

sudo mkdir /database/etc /database/log /database/data 

sudo chown mysql:mysql /database/data 
sudo chmod 0700 /database/data 
sudo chown mysql:adm /database/log
sudo chmod 0750 /database/log 
sudo chmod g+s /database/log

sudo cp -R /etc/mysql /etc/mysql.ORIG 
sudo cp -R /var/lib/mysql /var/lib/mysql.ORIG 
sudo cp -R /var/log/mysql /var/log/mysql.ORIG 

sudo mv /etc/mysql /database/etc/ 
sudo mv /var/lib/mysql/* /database/data/ 
sudo mv /var/log/mysql/* /database/log/ 

sudo mkdir /etc/mysql

echo "/database/etc/mysql /etc/mysql none bind" | sudo tee -a /etc/fstab 
echo "/database/data /var/lib/mysql none bind" | sudo tee -a /etc/fstab 
echo "/database/log /var/log/mysql none bind" | sudo tee -a /etc/fstab 

sudo mount -a 
sudo service mysql start 

Move Apache / PHP / sites to an EBS volume

This assumes we have an EBS volume with a mount point called "webserver" attached.

sudo service apache2 stop 

Move some Apache parts to run on the EBS volume, but keep the 'place-holders' populated through mount points.

sudo cp -R /etc/apache2 /etc/apache2.ORIG 
sudo cp -R /var/log/apache2 /var/log/apache2.ORIG 
sudo cp -R /var/www /var/www.ORIG 
sudo cp -R /etc/php5 /etc/php5.ORIG 

sudo mv /etc/apache2 /webserver/ 
sudo mv /var/log/apache2/ /webserver/log/ 
sudo mv /var/www/ /webserver/sites/ 
sudo mv /etc/php5 /webserver/ 

sudo mkdir /webserver/vhosts /webserver/apache2/vhosts /etc/apache2 /etc/apache2/vhosts /var/log/apache2 /var/www /etc/php5 

echo "/webserver/apache2 /etc/apache2 none bind" | sudo tee -a /etc/fstab 
echo "/webserver/vhosts /etc/apache2/vhosts none bind" | sudo tee -a /etc/fstab 
echo "/webserver/log /var/log/apache2 none bind" | sudo tee -a /etc/fstab 
echo "/webserver/sites /var/www none bind" | sudo tee -a /etc/fstab 
echo "/webserver/php5 /etc/php5 none bind" | sudo tee -a /etc/fstab 

sudo mount -a 
sudo service apache2 start 

Move E-mail / Postfix / Dovecot to an EBS volume

This assumes we have an EBS volume with a mount point called "email" attached.

sudo service postfix stop 
sudo service dovecot stop 

Move some E-mail related parts to run on the EBS volume, but keep the 'place-holders' populated through mount points. Because we are going to use a MySQL controlled, virtual mail set-up we also need to create a new user and set some permissions.

sudo mkdir /email/vmail /email/log /email/log/postfix /email/log/dovecot /email/spool /email/spool/postfix/ /email/spool/vmail/ /email/smtp-certificates /home/ubuntu/smtp-certificates /var/log/dovecot /var/log/postfix /var/spool/vmail /home/vmail

sudo groupadd -g 5000 vmail 
sudo useradd -g vmail -u 5000 vmail -d /home/vmail 
sudo chown vmail:vmail /home/vmail 

sudo chown vmail:adm /var/log/dovecot 
sudo chown syslog:adm /var/log/postfix 
sudo chown vmail:mail /var/spool/vmail
sudo chmod 0775 /var/spool/vmail 

sudo chown vmail:adm /email/log/dovecot 
sudo chown syslog:adm /email/log/postfix 
sudo chown vmail:vmail /email/vmail 
sudo chown vmail:vmail /email/log 
sudo chown vmail:vmail /email/spool/postfix 
sudo chown vmail:mail /email/spool/vmail 
sudo chmod 0775 /email/spool/vmail 

sudo cp -R /etc/postfix /etc/postfix.ORIG 
sudo cp -R /etc/dovecot /etc/dovecot.ORIG 
sudo cp -R /var/spool/postfix /var/spool/postfix.ORIG 

sudo mv /etc/postfix/ /email/postfix 
sudo mv /etc/dovecot/ /email/dovecot 
sudo mv /var/spool/postfix/* /email/spool/postfix 

sudo mkdir /etc/postfix /etc/dovecot /etc/postfix/virtual

echo "/email/postfix /etc/postfix none bind" | sudo tee -a /etc/fstab 
echo "/email/dovecot /etc/dovecot none bind" | sudo tee -a /etc/fstab 
echo "/email/spool/postfix /var/spool/postfix none bind" | sudo tee -a /etc/fstab 
echo "/email/spool/vmail /var/spool/vmail none bind" | sudo tee -a /etc/fstab 
echo "/email/vmail /home/vmail none bind" | sudo tee -a /etc/fstab 
echo "/email/smtp-certificates /home/ubuntu/smtp-certificates none bind" | sudo tee -a /etc/fstab 
echo "/email/log/dovecot /var/log/dovecot none bind" | sudo tee -a /etc/fstab 
echo "/email/log/postfix /var/log/postfix none bind" | sudo tee -a /etc/fstab 

sudo mount -a 
sudo service postfix start 
sudo service dovecot start 

Move Subversion to an EBS Volume (optional)

This assumes we have an EBS volume with a mount point called "webserver" attached.

We have no subversion repository location yet, so we can just create one:

sudo mkdir /webserver/svn /svn-repositories 

echo "/webserver/svn /svn-repositories none bind" | sudo tee -a /etc/fstab 

sudo mount -a 

Download this guide

To make this guide more useful I've added a feature to allow you to save it offline in a simple HTML format. If you have not customised this guide to your own values you may wish to do so here before you download it. There are a few options here:

Guide contents

  1. Hosting a website on Amazon EC2 - The goals and assumptions of this guide
  2. Preparing required tools - Create an AWS account, configure Elastic Fox and add an SSH tool
  3. Customise this guide - Allow all commands to be tailored to you (optional)
  4. Core software installation - Install some common software to the server image
  5. Depending upon your chosen configuration there is a choice here:
    1. Create and attach new EBS volumes - New server that you may want to split in future
    2. Attach existing EBS volumes - If you have used this guide before and have EBS volumes
    3. No attached EBS volumes - If you are not using the cloud or don't want to use them
  6. Depending upon your chosen configuration there is another choice here:
    1. Software Configuration - Set up the system to work as a multi-function server (from 5a or 5c)
    2. Software Configuration from existing EBS volumes - Use settings from EBS volumes (from 5b)
  7. Backing up and clean up - Configure Crons, log rotation etc