EC2 Guide: Attach existing EBS volumes (5b / 7)

This step assumes that you have already created all the EBS volumes following the exact steps in this guide, they just need attaching to the correct points and a few folders need to be removed / re-factored.

Attach the EBS Volumes

We are going to use the same configuration as during initial creation. So, in Elasticfox:

Then back on the server:

sudo mkdir -m 000 /webserver 
sudo mkdir -m 000 /database 
sudo mkdir -m 000 /email 

echo "/dev/sdc /webserver xfs noatime 0 0" | sudo tee -a /etc/fstab 
echo "/dev/sdd /database xfs noatime 0 0" | sudo tee -a /etc/fstab 
echo "/dev/sde /email xfs noatime 0 0" | sudo tee -a /etc/fstab 

sudo mount -a 

Make MySQL use the EBS volume data

This assumes we have an EBS volume with a mount point called "database" attached that contains all necessary data / permissions.

sudo service mysql stop 

Remove some existing MySQL parts so they can be mounted with those already available on the EBS volume.

sudo mv /etc/mysql /etc/mysql.ORIG 
sudo mv /var/lib/mysql /var/lib/mysql.ORIG 
sudo mv /var/log/mysql /var/log/mysql.ORIG 

sudo mkdir /etc/mysql /var/lib/mysql /var/log/mysql 
sudo chown mysql:mysql /var/lib/mysql 
sudo chmod 0700 /var/lib/mysql
sudo chown mysql:adm /var/log/mysql 
sudo chmod 0750 /var/log/mysql 
sudo chmod g+s /var/log/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 

Make Apache / PHP use the EBS volume data

This assumes we have an EBS volume with a mount point called "webserver" attached that contains all necessary data / permissions.

sudo service apache2 stop 

Remove some existing Apache parts so they can be mounted with those already available on the EBS volume.

sudo mv /etc/apache2 /etc/apache2.ORIG 
sudo mv /var/log/apache2 /var/log/apache2.ORIG  
sudo mv /var/www /var/www.ORIG 
sudo mv /etc/php5 /etc/php5.ORIG 

sudo mkdir /etc/apache2 /etc/apache2/vhosts /var/log/apache2 /var/www /etc/php5 
sudo chown root:adm /var/log/apache2 

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 

Make Postfix / Dovecot use the EBS volume data

This assumes we have an EBS volume with a mount point called "email" attached that contains all necessary data / permissions.

sudo service postfix stop 
sudo service dovecot stop 

Remove some existing Postfix / Dovecot parts so they can be mounted with those already available on the EBS volume. Because we are going to use a MySQL controlled, virtual mail setup we also need to create a new user and set some permissions.

sudo mkdir /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 mv /etc/postfix /etc/postfix.ORIG 
sudo mv /etc/dovecot /etc/dovecot.ORIG 
sudo mv /var/spool/postfix /var/spool/postfix.ORIG 

sudo mkdir /etc/postfix /etc/dovecot /var/spool/postfix 

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 

Make Subversion use the EBS volume data (optional)

sudo mkdir /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