Upgrade your browser Download Chrome Download Chrome Download Firefox Download Firefox Download Opera Download Opera Download Safari Download Safari Download Explorer Download Explorer

Our children's book authoring tool Storybuilder is finally unveiled! Check it out at LittleRobot.com. Close

Robot Media

Blog

How to create an Amazon EC2 instance with Apache, PHP and MySQL (LAMP)

At Robot Media we want to spend as little time as possible worrying about anything that isn’t mobile or tablet related. That is one of the reasons why we use Amazon EC2 -and Amazon AWS in general- for the backend components of our apps.

 

In this tutorial we will show you how to set up an Ubuntu Amazon EC2 instance with Apache, PHP and MySQL (also known as LAMP), which should be enough to host a simple dynamic web site.

Some familiarity with Amazon AWS and LAMP is assumed; you can adquire the former from the book Host Your Web Site On The Cloud: Amazon Web Services Made Easy.


If you don’t have an AWS account already, sign up and open the EC2 tab of the AWS Management Console. To register you will need to provide payment details, as running EC2 instances costs marginal money.

1) A Security Group for web access

 

The first step will be to create a Security Group that allows HTTP connections and limited SSH to configure the instance. Click on Security Groups > Create Security Group and complete the following form.

Choose an appropriate name and description for the Security Group. We will use the name web_access thorough this tutorial.

Once the Security Group is created we need to add the Inbound Rules that allow HTTP and SSH access. Click on the newly created Security Group, the select the Inbound tab.

Select HTTP from the Create a new rule drop-down and leave the Source as 0.0.0.0/0 to allow HTTP connections from anywhere. Click on Add Rule.

Select SSH from the Create a new rule drop-down and change the Source to your IP address to restrict SSH connections to your computer or network; you don’t want anyone to be able to log in to the instance. Again click on Add Rule.

Now click on Apply Rule Changes; your changes won’t be saved unless you press this button.

2) A LAMP-friendly instance

The second step will be to create an EC2 instance that provides a very clean and quick LAMP installation. Click on Instances > Launch Instance to initiate the Request Instance Wizard.

An instance is created from an AMI, an acronym for Amazon Machine Image. Surprisingly, Amazon’s AMIs do not provide the most straightforward LAMP installation, so we wil use a Community AMI instead. Click on the Community AMIs tab and wait until the list loads (might take a while).

We will use a the 32-bit Ubuntu 10.10 Maverick EBS boot provided by alestic. You can enter its identifier on the text field to find it quicker: ami-ccf405a5. Click on Select to move to the next step.

From now on we will mostly use default settings. If you want to save some cents, select Micro from the Instance Type drop-down to choose the lowest-end hardware. Click on Continue.

Leave all the default settings in Advanced Instance Options and click Continue.

You can safely ignore the tags and click Continue.

You will now need to generate a new Key Pair (or use an existing one if you already have one). The Key Pair will allow you to log in to the instance via SSH. Enter a suitable name (we will use mykeypair in this tutorial) and click on Create & Download your Key Pair.

mykeypair.pem file will be downloaded. Save it somewhere easily accesible as you will need it in a few minutes. If you plan on using this instance for production, make sure the file is properly secured. Click on Continue.

In Configure Firewall select Choose one or more of your existing Security Groups. Choose the Security Group that we created on step 1 and click on Continue.

Review all the settings and click on Launch. The instance has to boot and thus will take a few seconds to be ready. In the meantime, take note of the instance identifier which will be needed to create an Elastic IP for the instance.

3) The Elastic IP

Amazon EC2 instances have a public a DNS name which is rather long and cumbersome to use. It is much more practical to use an IP address to reference an instance. Luckily, Amazon provides Elastic IPs which are free as long as they are associated to a running instance.

Click on Elastic IPs > Allocate New Address > Yes, Allocate. This will create a new Elastic IP but will not associate it to the instance yet. Write down the IP number as you will need it in a few minutes.

 

Select the newly created IP address and click on Associate Address. Choose the instance created on Step 2 from the drop-down and click on Yes, Associate.

By now the instance should be up and running (which you can confirm by returning to the Instances section and checking if the status indicator is green). It’s time to connect via SSH to it.

4) Installing LAMP via SSH

Open the console and navigate to the Key Pair file. In order to be able to use it for SSH we need to restrict its permissions:

chmod 600 mykeypair.pem

Now we can connect via SSH. Type…

ssh -i mykeypair.pem ubuntu@192.168.0.1

…where mykeypair should be replaced by the name of the Key Pair file and 192.168.0.1 should be the IP address that we created in step 3.

You will be asked: “Are you sure you want to continue connecting (yes/no)?” Type yes.

If everything is correct the prompt will change to reflect that you’re running commands from inside the Amazon EC2 instance.

Before installing LAMP we need to update the package database of the Ubuntu installation. To do this:

sudo apt-get update

Now we can install LAMP using tasksel, which is already present in this installation.

sudo tasksel

Scroll down to LAMP server and press space, then Enter.

The LAMP installation should run smoothly. You will be only asked to provide and confirm the password of the root user of the MySQL database.

Once the installation has finished the Apache server should be up and running. To test it, open a browser and type the IP address of the instance. A webpage stating “It works!” should load.

Finally, we will install phpMyAdmin in order to test the PHP and MySQL installation and also to conveniently configure the MySQL database. From the SSH session:

sudo apt-get install phpmyadmin

Select apache2 as the web server…

…and leave the database configuration for later (choose No on the second prompt).

phpMyAdmin should now be running under http://<instance IP>/phpmyadmin. You can log in using the root username and the database password that you provided earlier.

Congratulations! You have successfully created an Amazon EC2 instance with LAMP!

This tutorial just scratches the surface of what can be done with Amazon AWS; we recommend learning (and breaking) a bit more about EC2 before using instances like these for production.

43 thoughts on “How to create an Amazon EC2 instance with Apache, PHP and MySQL (LAMP)

  1. Doug Harrold says:

    Thank you for this! Just one question — in step 4. you say “Open the console…” – what console — i.e., where do I type “chmod 600 mykeypair.pem” and “ssh -i mykeypair.pem ubuntu@192.168.0.1″ Is that only if I have linux on my local machine? I’m using windows and use PuTTY to SSH into my instances. Do I just connect using PuTTY and pickup step 4 at “sudo apt-get update?” Thanks again.

  2. admin says:

    You’re welcome, Doug!

    Yes, that step might not be necessary with PuTTY.

  3. Ryan says:

    I wish I started here when I first began with trying out EC2 – I wasted a whole day trying to get the Bitnami AMI’s to work and being stonewalled each time. With your instructions above it got me working first time! Thank you so much!!

  4. naresh says:

    what is the need to create the instance in amazone?
    hw we connect to instance throw my project

  5. admin says:

    @naresh That is beyond the scope of this article. Sorry.

  6. Angel LAcret says:

    How can i start putting files in to the www/ folder, what permission sould i gice it?

  7. Sean says:

    Very helpful, thanks!

  8. Dale Larsen says:

    I have followed you instructions but I am on a windows and am trying to ssh to the ec2 instance via Putty. I know I have my key-pairs right and pretty sure they are converted to ppk correctly (followed an tutorial by aws on how to do that), but I always get the error message:

    Server refused our key.

    Then I get a popup that says “Disconnected: No supported authentication methods available”

    Sure could use some help. Thanks!

  9. Andy Everett says:

    Doug – If on Windows, ake sure that the PPK has been loaded into an instance of Pageant running on that machine.

  10. Mike says:

    Thanks for the article. Do you ever encrypt your EBS volumes at all?

  11. Adam Bellas says:

    Hmmm. When I first get into tasksel and choose LAMP, it winks out and brings me back to the prompt. No error messages. Any ideas on that one? Tasksel looks like it’s about to make my life a lot easier, but it just dies with no messaging after pressing enter.

  12. Rob Dana says:

    Hi.

    Thanks for the great tutorial. I’m accessing this instance from windows with Putty and thought I would mention that you need to log in as user ‘ubuntu’. I didn’t see that mentioned anywhere and it took me a little to find it, hoping I might save someone else looking.

    Thanks Again

  13. Rob Dana says:

    @Adam
    I had the same issue. Press space to select – https://help.ubuntu.com/community/Tasksel

  14. Neeraj Bhatnagar says:

    These instructions were extremely useful and intelligently written. Everything worked as described and I have an instance running in a very short time.

  15. George Nadirashvili says:

    Thank you very much, this instruction is perfect.

  16. Brian says:

    Awesome! A step-by-step guide to getting started with EC2!
    Thank you very much for posting this!

    For those of you that wish to create an instance in US West Region, simply search for:

    ubuntu-maverick-10.10-i386-server-20101225

    And the same AMI (Amazon Machine Image) referenced in this tutorial will pop up in the list.

  17. Bob Greenberg says:

    Thanks a bunch! So much better than the gobbledygook AWS documentation. Worked perfectly. I used “natty” instead of “maverick” because it seems to be the current ubuntu build.

  18. Nicolas Dujovne says:

    Thanks! Great post! It took me just a few minutes to set up my server.

    Do you have any recommendations for further reading about setting up a production environment?

    Thanks!

  19. Eric Tarasoff says:

    Great article and a huge help for someone like myself who works in the Windows world usually…thanks!

  20. Alex says:

    The issue I am having is access the www/ folder via an ftp program. I am able to see it but it won’t let me add or edit files as the ‘ubuntu’ user. If i try and log in with the ‘root’ I get an authentication error. How can fix this so that I can start adding files via ftp?

  21. Alex says:

    Any help with the FTP issue would be awesome!

  22. Thanks for a well explained article.

    @adam you can choose to use the alternative way to tasksel by entering:

    sudo apt-get install lamp-server^

    (dont forget the carat – ^. That’s the alternate way to use tasksel).

  23. Pingback: WordPress on Amazon EC2 | Blogging with Bonzo

  24. Gourab says:

    Thanks for the article. Thank a lot experts.

  25. Srinivasan says:

    This was really very helpful. After exploring a lot of unhelpful links. I was finally able to install LAMP seamlessly here.

    Quick question- /how good in terms of performance is an Amazon microinstance that comes free for a year? Please ignore if this is beyond the scope

    Thanks again!

  26. Alexander says:

    Great article, thanks! Very much requesting help in the (S)FTP department, too!

  27. Kenson Goo says:

    This is the most neat and clear instructions ever on this subject. That helps us a lot in setting up our test servers.

  28. Tanmay says:

    I could install the piece after figuring out the tasksel space bar key…
    Got through to the It Works page.
    but
    On the install for phpmyadmin – I go through the install and when I go to the url and type
    amazon instance name/phpmyadmin it gives me a 404
    any ideas?

  29. Pingback: Welcome to my Web Developer’s Blog! | George Cambell's WebDev Tech Notes

  30. atul says:

    Thanks, This article is really helpful.

  31. Pingback: How to stream live mixes (for free!) using Traktor+Amazon EC2 | Prajna Concepts

  32. Pingback: Como criar uma instância Amazon EC2, com Apache, Php e MySQL (Lamp) | 3Birds Blog

  33. Pingback: Como criar uma instância Amazon EC2, com Apache, Php e MySQL (Lamp) | 3Birds – Blog

  34. chad says:

    DUDE! YOU ARE CHAMP!!! I only wish this was the first place I started. Thanks!

  35. Max Min says:

    This is an EXCELLENT tutorial. Really, this helped me so much with stepping into this whole Amazon EC2 thing – thanks so much!

  36. brian says:

    tanmay, the address is case sensitive, I got the same error and just had to make sure everything was lower case

  37. Purush says:

    Excellent job… very useful for beginers..

    Thanks,

  38. Steve Tinter says:

    OK – so I setup my LAMP installation on AWS and now I want to actually host my website i.e. be able to type http://www.femteresting.com and get to the AWS hosted stuff – how can I do this? I am familiar with pointing name servers etc. and have access to the DNS manager for the domain.

    I suppose it is easy to install Joomla instance on the LAMP server?

  39. very very thanks for your hard work very much helpful information to learn the basic step with AWS. and i can say my search ends here. thanks.

  40. Pingback: linnth's articles » How I setup LAMP git gitolite on AWS EC2 Ubuntu instance

  41. Lam says:

    Thank you very much. I spent 4 days trying to set up a zend server on AWS and keep getting stonewall by bad instructions.

  42. Pingback: Amazon EC2 | Eindwerk Browser Compatibility Tool

  43. Harish says:

    I was able to install phpmyadmin but when i launch on browser – instancename/phpmyadmin , get 404 error.

    i have checked its all lower case, any idea?

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=""> <strike> <strong>