Setting Up A Proper LAMP Stack On AWS EC2 & EBS
Posted: April 22nd, 2013
Author: Nuri Hodges
Setting up a LAMP stack may be something you’re already familiar with, but we’ve all heard the horror stories of misconfigured instances on Amazon Web Services. Data persistence issues are unnecessary annoyance that we’d all love to avoid, as a disappearing database is no fun for anyone. In this post, you’ll set up your own Linux, Apache, MySQL and PHP stack on AWS the proper way, using an EBS (Elastic Block Store) volume to house and serve your database. Sleep soundly knowing your records won’t perform a disappearing act overnight when your junior sysadmin restarts your EC2 instance. 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, so if you haven’t yet, go ahead: configure your instance and deploy it to your desired AWS availability zone. We’re using a micro instance, running on Ubuntu 12.04 LTS, serving from the US West zone. Once your EC2 instance is up, go ahead and create an EBS volume in your AWS control panel; attach that volume to your EC2 instance, which is identifiable by its unique name (usually something like i-4eefe17c) in the dropdown menu. (Note: If you’re running Ubuntu 12.04 LTS like we are, it comes bundled with XVD drivers, which means your EBS volume will attach as /dev/xvd[a-z] instead of /dev/sd[a-z].)
Now that you’ve attached your fresh EBS volume to your EC2 instance, we can get down to the nitty gritty. Go ahead and connect to your EC2 instance via SSH, and we’ll get started with the LAMP stack setup and configuration. Remember, we’re using Ubuntu 12.04, so we’ll be installing our stack using the Advanced Packaging Tool (APT) package manager, which is available by default on Debian based systems.
Format EBS Volume
sudo mkfs -t ext3 /dev/xvdh
Mount EBS Volume
sudo mkdir /ebs1 sudo mount /dev/xvdh /ebs1
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
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
In this step, 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.
Fun Fact: What is the difference between using a symlink and binding with fstab?
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.
Creating EBS Snapshots
EBS’ Snapshots feature can be used to facilitate routine database backups, which can be restored at any given moment with a few clicks.