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.
A 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 email@example.com
…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.
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.