Spruce interactive

Noise

Setting Up A Proper LAMP Stack On AWS EC2 & EBS

Posted: April 22nd, 2013

Author: Nuri Hodges

Tags:

, , , , ,

 Details

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.

Additional Resources:

  • 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.
  • Ricky

    Thanks for putting this up.

    • Ricky

      I am having some issues with the last part. Any advice? See below for output:

      ubuntu@ip-172-31-39-90:~$ sudo mkdir /ebs1/etc/ /ebs1/lib /ebs1/log
      mkdir: cannot create directory `/ebs1/etc/’: File exists
      mkdir: cannot create directory `/ebs1/lib’: File exists
      ubuntu@ip-172-31-39-90:~$ sudo mv /etc/mysql /ebs1/etc
      mv: `/etc/mysql’ and `/ebs1/etc/mysql’ are the same file
      ubuntu@ip-172-31-39-90:~$ sudo mv /var/lib/mysql/ /ebs1/lib
      ubuntu@ip-172-31-39-90:~$ sudo mv /var/log/mysql/ /ebs1/log
      ubuntu@ip-172-31-39-90:~$ echo “/ebs1/etc/mysql /etc/mysql none bind” | sudo tee -a /etc/fstab
      /ebs1/etc/mysql /etc/mysql none bind
      ubuntu@ip-172-31-39-90:~$ sudo mount /etc/mysql
      ubuntu@ip-172-31-39-90:~$ echo “/ebs1/lib/mysql /var/lib/mysql none bind” | sudo tee -a /etc/fstab
      /ebs1/lib/mysql /var/lib/mysql none bind
      ubuntu@ip-172-31-39-90:~$ sudo mount /var/lib/mysql
      mount: mount point /var/lib/mysql does not exist
      ubuntu@ip-172-31-39-90:~$ sudo service mysql start

  • Vignolo

    i think is you are missing the step to mount /ebs1 at boot. Before the binding of directories. Really helpfull. Thanks!

  • coop

    Great article – thanks! So taking this one step farther … what about all the website content that is sitting in /var/www/.. no doubt a lot of dynamic content. Can the LAMP stack just be “simply” installed to the mounted EBS volume ?

Talk to Us

Wise men have said, "All good things must come to an end", and so it was with Spruce. We began our first project in 2006, and completed our last in Fall 2013. It's been a wonderful experience, but now it's time to begin the next adventure.

Though...we wouldn't want to leave you hanging, now would we? Here are our top recommendations for world-class branding, design, and web development:

  • Kristina (KJ) Parish and Beam Collective:
    KJ provided the artistic vision that allowed Spruce to succeed, and we still think she's the greatest designer in the entire world. She's started a new collective with some talented developers, and is covering much the same territory as Spruce--design and development all under one roof, with a caliber of service and professionalism seldom seen in this business or any other. If you like what you see on the Spruce site, then definitely get in touch with KJ at kj@beamcollective.com
  • Permanent Art & Design
    Permanent is a nationally recognized design & branding firm out of Minneapolis, MN. They've been a long term collaborator and strategic partner over the past couple years, and we'd recommend them for medium to large sized businesses/organizations who are serious about putting their best foot forward. In addition to design, Permanent offers strategy and marketing services, plus in-house and networked developers. For more information contact Joseph Belk joseph@permanentadg.com
  • Nate Thompson
    A freelancer, Nate reminds us of ourselves back in the day. Smart, communicative, and skilled, Nate is our recommendation for small to medium sized businesses/organizations who have sites built on Wordpress or other open source content management solutions. With a background in both design and development, he's a one-stop shop for most web-related tasks. You can get in touch at nate.a.thompson@gmail.com
  • Kevin DeBernardi
    Designer turned developer, Kevin is embarking on a freelance web career after working as the in-house designer at the Museum of the City of New York. He's adept at translating his design ideas into custom PHP code, and is constantly expanding his technical palette. Kevin's a good choice for projects that don't quite fit into the Wordpress mold, and that would benefit from a consistency of presence and vision from design through execution. Kevin can be reached at kevin@analoglifestyle.com