Search

Save the Internet: Click here

Entries in LAMP on EC2 (10)

Wednesday
Mar042009

LAMP on EC2 - Part 10: Generating a Custom AMI

So in our last post, we created scripts that would take snapshots of our persistent volume. So that covers our persistent datastore. But what about the changes we have made to our Linux image. In the event that the instance goes down, we would loose any configuration changes made to date. So we will create our own private AMI of our current Linux instance state. In the event of a failure or even to spawn multiple images, we can use this custom AMI to quickly restore our state.


This post is part of a series of posts detailing the steps required to host a LAMP installation on Amazon's Elastic Computing Cloud. Steps in this post may depend on actions taken in previous posts. Amazon's Web Cloud Services are a pay-as-you-go service so please realize anything you do may result in charges to your Amazon account.


To do this, first shut down MySQL and Apache and unmount your persistent store using the following commands:

 
/etc/rc.d/init.c/mysql stop
/etc/rc.d/init.c/httpd stop
umount /persistent

Go to your AWS Management Console and retrieve your Owner ID from the running instance. Copy that and paste it within the following command, which creates the new AMI:

 
ec2-bundle-vol --fstab /etc/fstab \
     -c /home/ec2/certs/[certificate] \
     -k /home/ec2/certs/[private key] \
     -u [Owner ID]

This will create the image in the /tmp directory, but that image still needs to be uploaded. Upload it using the following command:

 
ec2-upload-bundle -b [bucket name] \
     -m /tmp/image.manifest.xml \
     -a [Access Key ID] \
     -s [Secret Access Key]

where the bucket name is a globally unique identifier. It can be the name of an bucket you already use or a new one, in which case the bucket will be created (if the name is available).

You will need to register this new AMI with AWS Management Console by going to the AMIs and clicking the Register New AMI. The AMI manifest path that it will ask for is your bucket's name followed by /image.manifest.xml. The AWS Management Console should add your AMI to the list of public ones (it will be marked "private"). If you don't see it right away, you can do a search for a substring within the name of your bucket.

Wednesday
Mar042009

LAMP on EC2 - Part 9: Setting up Snapshots

One of the benefits stated in Post 1 is "Fast and Easy Backups". Amazon provides the ability to perform immediate, incremental backups of EBS volumes. With the help of some scripts to place our MySQL database in a state suitable for backup, we can make nearly-immediate backups of our persistent storage.


This post is part of a series of posts detailing the steps required to host a LAMP installation on Amazon's Elastic Computing Cloud. Steps in this post may depend on actions taken in previous posts. Amazon's Web Cloud Services are a pay-as-you-go service so please realize anything you do may result in charges to your Amazon account.


We'll need some tools from Amazon so login to your instance via SSH and switch to the root user. Since the Rightscale AMI already has an older version of the EC2 AMI tools installed, we will download the newer tools and use rpm to update them to the current version:

 
cd /usr/src 
wget "http://s3.amazonaws.com/ec2-downloads/ec2-ami-tools.noarch.rpm" 
rpm -Uvh ec2-ami-tools.noarch.rpm 

You will most likely have to use a web browser and visit http://developer.amazonwebservices.com/connect/entry.jspa?externalID=351&categoryID=88 to identify the url for the EC2 API tools. The Rightscale AMI we used also has a version of the tools installed. They are installed in /home/ec2. I did the following:

 
cd /usr/src 
wget "http://s3.amazonaws.com/ec2-downloads/ec2-api-tools.zip" 
unzip ec2-api-tools.zip 
cd /home/ec2 
rm -rf bin 
rm -rf lib 
cd /usr/src/ec2-api-tools* 
mv bin /home/ec2 
mv lib /home/ec2 

We will need to the X.509 certificate and private key (we downloaded these when we setup our Amazon Web Services account in Part 1). Use whatever method you feel comfortable with to upload the pk-*.pem and cert-*.pem to the /home/ec2/certs directory. In the /root/.bashrc file, add the following lines to make sure that the EC2 tools know where to find the certificate and key:

 
export EC2_CERT=/root/.pem 
export EC2_PRIVATE_KEY=/root/.pem 

The backup script that will run every hour will need to lock the MySQL database during the snapshot process, so create a /root/.my.cnf file that has the following format:

 
[client] 
    user=root 
    password=

Next we will install two scripts. The first is called takesnapshot and should be downloaded and placed in /etc/cron.hourly or /etc/cron.daily depending on your needs. Edit this file to insert the volume ID of your persistent store. This ID can be found using the Amazon Web Services Account under the Volumes tab. Make this script executable using chmod +x.

The second script is called ec2-snapshot-xfs-mysql.pl and is a modified version of Amazon's script. Move this script to /usr/bin, edit it to point to the proper file names of your X.509 certificate and private key, and make it executable.

Once everything is in place, you can manually try running the takesnapshot script. Once it finishes, checkout your AWS Management Console for the Snapshot tab for your backup.

Wednesday
Mar042009

LAMP on EC2 - Part 8: Setting up an Elastic IP Address

So now we have a Linux instance out on the Internet and an Apache web server running on it. We now need a static IP address to associate with our server so that the Internet can access our (web) services. Amazon offers what they call Elastic IP addresses. Elastic IP addresses are associated with your Amazon Web Services account, not specific instances. Any elastic IP addresses that you associate with your account remain associated with your account until you explicitly release them. Unlike traditional static IP addresses, however, elastic IP addresses allow you to mask instance or availability zone failures by rapidly remapping your public IP addresses to any instance in your account.


This post is part of a series of posts detailing the steps required to host a LAMP installation on Amazon's Elastic Computing Cloud. Steps in this post may depend on actions taken in previous posts. Amazon's Web Cloud Services are a pay-as-you-go service so please realize anything you do may result in charges to your Amazon account.


Some good news: Amazon imposes a small hourly charge when these IP addresses are not mapped to an instance. When these IP addresses are mapped to an instance, they are free of charge.

So let's login to your AWS Management Console. Select Elastic IPs from the left-hand Navigation area. Click on the Allocate New Address button near the top. A pop-up window will be displayed to confirm your request for a new address. Click on "Yes Allocate". Write down your new IP address and select it with a click. Next, click on the Associate button. A pop-up windows will be displayed, asking you to associate the IP address with your Instance ID. Select your Linux instance and click on Associate.

Your instance now has an IP address. You will need to add this new IP address to your registered DNS settings.

Wednesday
Mar042009

LAMP on EC2 - Part 7: Configuring PHP

As I stated in my previous posts, optimizing software for the web and especially for Amazon's EC2 service can be a mystical art. I will not pretend to be an expert and I invite any comments on further optimzations you may have.


This post is part of a series of posts detailing the steps required to host a LAMP installation on Amazon's Elastic Computing Cloud. Steps in this post may depend on actions taken in previous posts. Amazon's Web Cloud Services are a pay-as-you-go service so please realize anything you do may result in charges to your Amazon account.


The only optimations I really have for PHP involve the following changes to your /etc/php.ini file:
 
max_execution_time = 90
max_input_time = 240 
memory_limit = 128M

You will want to restart your Apache web server to make use of the changes:

 
service httpd restart

Happy coding!

Wednesday
Mar042009

LAMP on EC2 - Part 6: Configuring Apache

As is with most software, there seems to be a some mysticism involving optimizing software -- especially for the web -- and even more so on a hosted web services such as Amazon's EC2. I will not pretend to be an expert but will try to bring together both my own experiences as well as the fine posts by others on the web.


This post is part of a series of posts detailing the steps required to host a LAMP installation on Amazon's Elastic Computing Cloud. Steps in this post may depend on actions taken in previous posts. Amazon's Web Cloud Services are a pay-as-you-go service so please realize anything you do may result in charges to your Amazon account.


Our apache web server should already be running on our instance from our earlier instructions in Part 3. But just in case, we can check its status by connecting to our instance via SSH, switching to the root user, and then entering:

 
service httpd status

If it is not running, you will probably have some invetigating to do -- check your logs.

Now it is time to make the following change to our Apache config file located at "/etc/httpd/conf/httpd.conf":

 
KeepAlive On
NameVirtualHost *:80

Note that we plan to use the Virtual Hosting (by Name) capabilities of Apache here. Depending on your plans, your configuration may vary. We would not only uncomment the NameVirtualHost directive, but we would also need to setup the VirtualHost section located at the bootom of this file:

 
<VirtualHost *:80>
        DocumentRoot /mnt/persistent/www
        ServerName www.your_domain.com
        <Directory /mnt/persistent/www>
                AllowOverride All
                allow from all
                Options +Indexes
        </Directory>
        ServerAlias your_domain.com
</VirtualHost>

Depending on the location and other properties of your web site, your configuration will most defintely be different. Let's restart our web server based on our changes:

 
service httpd restart

Our web server should be good to go! I invite any recommendations you may have as far as further optimizations.