When doing the configuration of LAMP on AWS EC2, data persistence issues are unnecessary annoyance that we’d all love to avoid, as a disappearing database is no fun for anyone. In other words, using an EBS (Elastic Block Store) volume to house and serve your database would be a more preferable way. Plus, using an EBS volume for your MySQL database makes it easier to scale!

This guide begins with the assumption that you’ve already created and launched an EC2 instance, and also running on Ubuntu 12.04 LTS on the instance. If it is not, the corresponding commands would be similar as other linux OS instances.

Note: For Ubuntu 12.04 LTS, it comes bundled with XVD drivers, which means your EBS volume will attach as /dev/xvd[a-z]_instead of _/dev/sd[a-z].)_

For example, if the EBS you attach with is named “/dev/sdf“, than inside the Ubuntu, you need to call “/dev/svdf” to use.

Set up EBS

Format EBS Volume

sudo mkfs -t ext3 /dev/xvdf

Mount EBS Volume

sudo mkdir /ebs1
sudo mount /dev/xvdf /ebs1

Update Ubuntu

Update APT Repositories

sudo aptitude update && sudo aptitude dist-upgrade

Install & Configure Stack

Note: Save the MySQL root password that is set during this step in a secure place._

sudo aptitude install apache2 php5-mysql libapache2-mod-php5 mysql-server
sudo a2enmod rewrite

Configure Apache

Update httpd.conf to Allow .htaccess Overrides

In the VirtualHost block for Directory /var/www, replace AllowOverride None with AllowOverride All._

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

Restart Apache & Load New Configuration

sudo service apache2 restart

Move MySQL to EBS Volume

We create all the necessary directories for MySQL on our EBS volume (/ebs1/etc,/ebs1/lib, and /ebs1/log), and then use fstab to bind the new directories to the old paths._

sudo service mysql stop
sudo mkdir /ebs1/etc /ebs1/lib /ebs1/log
sudo mv /etc/mysql /ebs1/etc
sudo mv /var/lib/mysql /ebs1/lib
sudo mv /var/log/mysql /ebs1/log
sudo mkdir /etc/mysql /var/lib/mysql /var/log/mysql
echo "/ebs1/etc/mysql /etc/mysql none bind" | sudo tee -a /etc/fstab
sudo mount /etc/mysql
echo "/ebs1/lib/mysql /var/lib/mysql none bind" | sudo tee -a /etc/fstab
sudo mount /var/lib/mysql
echo "/ebs1/log/mysql /var/log/mysql none bind" | sudo tee -a /etc/fstab
sudo mount /var/log/mysql
sudo service mysql start

At this point, you have successfully migrated MySQL from its default directories (/etc/mysql, /var/lib/mysql, and /var/log/mysql) to its new residence on your EBS volume. Administering your databases is no different after the move, simply connect via the MySQL console, using the root password you set earlier.


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>