Monday, May 26, 2008

Configure LAMP server on Amazon EC2 (Linux Apache mySQL PHP)

In this guide, we're going to set up a Ubuntu based LAMP server on Amazon Elastic Compute Cloud (EC2). If you are a starter with Amazon EC2, do have a look in my get started guide first before going through with this. You need to first setup an Amazon Web Services (AWS) account and learn how to run and save an instance using ElasticFox and puTTY.

I'm going to use one of the base install Ubunty Hardy AMI created by alestic as the Linux foundation. Start up ElasticFox and in the available Machine Images (AMIs) list, select the base Ubuntu Hardy AMI with ID
ami-6a57b203 (alestic/ubuntu-8.04-hardy-base-20080517.manifest.xml), and select Launch Instance(s) of this AMI.
Make sure that you've created a keypair for SSH authentication. If not, have look at the get start guide here. Select the appropriate keypair in the next window, and launch the instance. It may take a few minutes for the instance to run (keep refreshing the Your Instances section).

Once the instance state changed to running, SSH to the instance's public DNS as shown below using puTTY.

You should see something like the window below.

On your instance's command shell, enter the following command to begin installation of the LAMP server, and follow through the on screen instructions.

sudo tasksel install lamp-server

Once that is completed, let's enable mod rewrite to support pretty URL.

a2enmod rewrite

Let's first update the apt-get application

apt-get update

Install phpmyadmin with the following command.

apt-get install phpmyadmin

You need at least one text editor application. My favorite one in Linux is emacs. Use the following command to install emacs.

apt-get install emacs22

To setup phpmyadmin under apache all you need to do is include the following line in /etc/apache2/apache2.conf

Include /etc/phpmyadmin/apache.conf

If you use emacs to edit the file, use the command below to open the file in the text editor

emacs /etc/apache2/apache2.conf

Make the necessary changed in emacs, the use Ctrl X-S to save the file, and then Ctrl X-C to close.

This is optional, but I use php curl library in some of my work. So I would like to install the library using the following command

apt-get install php5-curl

Now, we can restart apache.

/etc/init.d/apache2 restart


Let's try out our newly installed webserver and phpmyadmin. Get your instance public DNS, and append phpmyadmin at the end. Open the address in your web browser. For example

http://ec2-67-202-12-202.compute-1.amazonaws.com/phpmyadmin

Congratulations!!! Your LAMP installations is working if you can see the welcome page of phpmyamin. You can use phpmyadmin to manage your mySQL database (add users, create databases etc). Just remeber your mySQL root password that you specified during the installation.



Lastly, you need to save your LAMP instance and create your own machine image (AMI). Refer to the getting started guide if you don't know how to do this.

If you would like to go further by configuring a LAMP cluster with load balancer, have a look at my cluster with load balancer guide here.

8 comments:

saltymule said...

Huge, I mean, HUGE, it would have taken me a few weeks to figure all that out. Would you say that an ftp server would be the most efficient way to get a set of application files to the server? Or is there something I'm missing?

Hidayat S said...

Well, there's a few method you can transfer files to the instance server. I use SCP mainly to do that from my PC. And for transfering from Amazon S3, I'm using s3sync. To install application, use apt-get or wget. Cheers.

Marcos said...

Seriously - Thanks!

Keith said...

First I read your EC2 post, and then this. You have no idea how much time and pain you have saved me with these excellent postings. You should keep doing this as you are better at it than 99% of the other ones out there. Thank you!!

Anonymous said...

Please continue your good articles on scaling php/mysql web applications using amazon web services framework.

mike said...

This is +perfect I'd like to get more of this information I want to know something else Can I get something related to cialis online in Amazon.

Unknown said...

Nike free sale store Australia,
Nike Lunar haze sale Nike Lunar shoes store,
Nike hyper women shoes for sale,cheap,
Cheap Nike free run shoes for sale
Nike run free shoes cheap on sale,
Nike free run sale online store,
Nike free shoes online sale store,
nike dynamo free shoes sale.
nike air presto sale, cheap!

Kyle Grando said...

Awesome site! Browsing through your site gives me a lot of knowledge in so many ways. Thanks!

westin hotel cincinnati oh