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

Check out our newest children's book for iPad & iPhone: Who's in the Loo? Download 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.

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

  1. kavita says:

    very helpful…… thanks a lot

  2. elad sc says:

    hey, great tutorial! thanx
    all works great apart from the very last step… i go to my dynamicIP adress/phpmyadmin and i get a 404 from the apache(ubuntu) server…

  3. jeremy says:

    Thanks! this worked like a charm for me – despite being written over a year ago!

  4. Andrea says:

    Really helpful, thanks a lot!

  5. Genc says:

    Probably the first article that has ‘worked’ exactly as it’s supposed to be

  6. Pingback: Amazon EC2 Instance with LAMP Stack | Psychoclerk Blog

  7. adhi says:

    Thanks :D

  8. Jaspal says:

    Excellent stuff …
    Just the last step gives a 404.
    Here is a fix from the older comments by chizuoka it worked for me :)

    chizuoka says:
    May 21, 2012 at 5:07 pm
    Run this two in command line…all solved

    sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf.d/phpmyadmin.conf

    sudo /etc/init.d/apache2 restart

  9. iedux says:

    great its works

  10. Thank you very much :) It works perfectly. How can I know upload files to the Apache 2 Server? I’ve installed already vsftpd. Thank you for your answer

  11. Thank you very much :) It works perfectly. How can I know upload files to the Apache 2 Server? I’ve installed already vsftpd. Thank you for your answer

  12. futuredge says:

    thankyou soomuch!!!!!!!!!

  13. Ralph Frost says:

    Great tutorial! Thank you.

    For other newbies working on Windows and doing the SSH via putty, like I am, once you’ve puttygen’d the private key and set the session and the ssh\auth, fields, when prompted for the login user name, enter ubuntu. Not, root and not ec2-user.

    It’s clearly written in the AWS putty documentation, BUT it still took me a while to find it…

    Thanks again!

  14. Me says:

    You need copy the dir:
    sudo cp -R /usr/share/phpmyadmin /var/www

  15. sharad says:

    Thank you. It was a great article.

  16. burnertoday says:

    18months old and still going.

    Works a treat!

  17. Dan says:

    Hi. Thanks for this article. I followed it through, I have got Apache installed and running, and MySQL installed. The IP address works, but my IP/phpmyadmin is not working – 404 error. On Ubuntu help page (https://help.ubuntu.com/community/ApacheMySQLPHP) it says this error means Apache and phpmyadmin are not working together. I tried following the instructions there but I am a complete noob to the console and it didn’t work, specifically I couldn’t install the universe repository.
    Any chance of a bit of help with this?
    Cheers

  18. Dan says:

    No worries, I seem to have it all working now.

  19. Chris says:

    Thanks much. Very helpful.

  20. Rohit Batra says:

    thanks a lot for this tutorial really needed it. I am a newbie to Amazon ec2. Can you also refer me a simple guide on how to use ec2 for php applications?

  21. Michael says:

    Thanks for the post. I tried to mimic what you said but I got stuck at the part where you needed to select an image “Ubuntu 10.10 Maverick EBS boot ”

    I skipped this step and instead grabbed one of the Amazon supplied Ubuntu images and also had to skip the step about the IP selection.

    Other than that, it worked a dream. I can’t believe how quick it was to get an instance up and running & it’s a lot faster than my current hosts too.

    I’ll be doing a migration shortly of all my data for sure.

    Cheers!

  22. Yudi says:

    Thanks, help me a lot to setup a portal in Amazon.

  23. Hello! Thanks for posting this tutorial – unfortunately I get blocked at the sudo apt-get update command, which fails with multiple 404 Not Found errors related to maverick-security. I’m unfortunately an Ubuntu noob so something easy might be able to fix this, but a few searches on Google, while promising, didn’t yield any results. Any ideas?

  24. Great post. step by step guide to kick-start. must try out. Thanks.

  25. Jonay Pelluz says:

    Thanks! I have been trying for an hour to do this, great tutorial, keep them coming!

  26. Miles says:

    Nice one !

    I got a problem in the last section, I can’t do ssh, it cannot reach it, so I tried to ping my new instance and they are all timeout.

    Why ?

    Thanks

  27. dan says:

    I cannot ssh it says time out what could be the error or problem. Tried the provided ubuntu@54.213.149.9 / dan@54.213.149.9 / but all is in vain.. My internt connection is fine.

  28. Starwave says:

    Thank you so much!!!

  29. Heather Evereth says:

    This did not work for me, I had to find a newer version of Ubuntu…. which resolved the issues I was experiencing. After that, all worked fine.

  30. Pingback: Creating an instance on AWS | f@*! it

  31. kazi says:

    Works fines here!!!. I would like to know now how to place / put files there

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>