Traveling, web programming, and scuba diving

Setting up an AWS EC2 Instance with LAMP and GIT

Setting up an AWS EC2 Instance with LAMP and GIT

If you’re reading this then you’re probably interested in hosting a web application with Amazon’s Web Services (AWS) and taking advantage of Amazons cloud computing capabilities for your web application in this step by step tutorial we will go over the process of accomplishing this plus a few extra tips and tricks on the way.




  • Successfully run a web application on a LAMP server on a AWS EC2 instance and deploy to it using GIT version control

Launch an Ubuntu AWS EC2 instance

After you have set up an AWS account go to your AWS console and sign in if need be. then follow the following images to set up and Ubuntu AWS EC2 Instance.

AWS Console

Click EC2 Virtual Servers in the Cloud

AWS Launch an Instance

Click ‘Launch an Instance’

AWS Launch an Instance - Step 1

Click ‘Continue’

Select Ubuntu Server 12.04.2 LTS

Select Ubuntu Server 12.04.2 LTS

Launch an AWS Instance - Step 3

Leave the defaults but ensure that ‘T1 Micro’ is the selected instance type if on a free Tier then Click ‘Continue’

Launch an AWS Instance - Step 4

Leave the defaults and click ‘Continue’

Launch an AWS Instance - Step 5

Leave the defaults and click ‘Continue’

Launch an AWS Instance - Step 6

Associate your instance with tags for categorization if need be and click ‘Continue’

Launch an AWS Instance - Step 7

Enter your name for your PEM key then click ‘Create & Download your Key Pair’. Note: you will need to keep this private and safe as it will be used to connect to your instance.

Launch an AWS Instance - Step 8

Choose your key pair you just made and click ‘Continue’

AWS Launch an Instance - Step 9

Click ‘Create a new Security Group’. In the Port range field enter 22 for SSH and Click ‘Add Rule’ then additionally enter 80 for HTTP. Then click ‘Continue’

Launch an AWS Instance - Step 10

At this stage you should see an instance review which should look similar to what’s shown, if so click ‘Launch’

AWS Instance Launched

At this stage your AWS instance should be launching. You may need to wait a few minutes for everything to be setup and propagated. ‘Click Close’

Listed AWS Instances

On the EC2 Virtual servers page you can click ‘Instances’ in teh sidebar to see your listed instances

Connect to you AWS Instance with SSH

Right click on your newly created listed instance and click ‘Connect’ then click ‘Connect with a standalone SSH Client’

Connect to server with your PEM Key using SSH

Open up Terminal in your applications folder if on OSX or download and open Putty if on Windows and connect using Amazons tutorial.

Execute the following commands in your terminal to connect to your AWS instance with your PEM Key.

Run the following command to modify the permissions of your pem key as it must not be publicly view able for SSH to work.

chmod 600 path/to/yourkeyname.pem

Reference the last image from the section on setting up your instance as a reference to find out what your IP and pem key name is.

ssh -i path/to/yourkeyname.pem [email protected]

Once in and have accept the servers finger print.

Install Linux Apache MySQL PHP (LAMP) Server

Execute the following command to update ubuntu.
sudo apt-get update

Once Ubuntu has been updated, run the following command to install a complete LAMP stack on ubuntu, enter yes or ‘y’ when prompted followed by your MySQL password for root user twice, take note of this password for connecting you your databases later.

sudo apt-get install lamp-server^

Install PHPMyAdmin

Execute the following command to install PHPMyAdmin.
sudo apt-get install phpmyadmin

Enter ‘y’ when prompted then select apache2 when prompted followed by no when asked to use default databases.

After the installation has finished you will need to execute a few commands to allow for you to access /phpmyadmin in your browser, you will need to add phpmyadmin to the apache configuration by executing the following command.

sudo nano /etc/apache2/apache2.conf

This will bring up the apache2 config file, use ‘CTRL’ + ‘V’ on mac to scroll to the bottom of the file. Once you have done that paste the following and hit ‘CTRL’ + ‘X’ to exit then enter ‘y’ to save then exit.

# Include web access to phpmyadmin
Include /etc/phpmyadmin/apache.conf

Restart apache for changes to take effect.

sudo service apache2 restart

If the following step went off without a hitch you should be able to see the following screens.

AWS LAMP Server Success

You should see this screen when you navigate to your elastic IP in your browser window

AWS PHPMyAdmin Success

You should see this screen when you navigate to your elastic IP in your browser window followed by /phpmyadmin

Connecting with SSH without a PEM key

In this section we will set up a key pair which will make deploying with git and connecting to your server a lot more simple.

First up you will need to navigate to your .ssh folder on your local machine

cd .ssh

if this folder doesn’t exist use mkdir to make it.

Once in your ssh folder on your local machine which should be in /Users/yourusername/.ssh generate your key by executing the following.

ssh-keygen -t rsa -b 1024

When prompted enter the file name to save the key enter id_rsa_aws, when prompted to enter a password leave blank.

In your .ssh directory execute the following command and copy the output to paste later.


Now connect to you AWS instance using you PEM key

ssh -i path/to/yourkeyname.pem [email protected]

Once in

echo 'the key you copied from' >> .ssh/authorized_keys
chmod 640 .ssh/authorized_keys
chmod 750 .ssh

You should now be able to connect to your webserver using ssh and the path to your id_rsa_aws in the same way as you were using your pem key.

However to make it even simpler to connect to your web server we will take an extra step. Go back to your local ssh folder and type the following.

vi config

Once in vi (a file editor) type the following. To start press ‘o’ then to write and exit the file type ‘wq’.

Host webserver
User ubuntu
IdentityFile ~/.ssh/id_rsa_aws

You should be able to to connect to your server using ssh webserver or another name you chose for your Host if it suits better.

If you get permissions errors set your .ssh folder to 750 and your to 600

Setup GIT for web deployment and version control

Once in your server install git with the following command.

sudo apt-get install git

This part of the tutorial has been adapted from if you want more in depth instructions check it out.

Firstly we will initialize the server with two git repositories the first will act as a centralized hub with a bare repository. The other will reside in the code base which will contain the live code.

We will store the live git repo in awsproject, you can change the name if that suits your needs better.

First off ssh to your server and follow the commands wehn you create index .html

ssh webserver
cd /var/www/
sudo mkdir awsproject
cd awsproject/
sudo vi index.html

In the index.html file start typing with ‘o’ and ‘wq’ to write and quit vi. Type a message to indicate the site is up and running “Site up and running powered by amazon web services and git!”

After you have save your index.html in awsproject initialize a git repository add the files within and commit it using the commands below.

sudo git init
sudo git add .
sudo git commit -m "initial live site commit"

Next we need to create a bare repository to act as a mediator between the live code and the local code. We will place this in /var/git/

sudo mkdir -p /var/git/awsproject.git
cd /var/git/awsproject.git
sudo git init --bare
cd /var/www/awsproject
sudo git push /var/git/awsproject.git master

Update the config of the live repo to be a remote of the bare repo by editing the file at /var/www/awsproject/.git/config

[remote "hub"]
url = /var/git/awsproject.git
fetch = +refs/heads/*:refs/remotes/hub/*

After that we need to set up some hooks to push updates from the bare repo to the live repo

Create/edit the file at /var/git/awsproject.git/hooks/post-update with the following

echo "**** Pulling changes into Live [Hub's post-update hook]"
cd /var/www/awsproject || exit
unset GIT_DIR
git pull hub master
exec git-update-server-info

Additionally if we decide to make changes on the live server for some reason we need to be able to push these changes to the bare repo. Create/edit the file at /var/www/awsproject/.git/hooks/post-commit with the following

echo "**** pushing changes to Hub [Live's post-commit hook]"
git push hub master

Both of these files need to be executable so execute the following commands

sudo chmod +x /var/git/awsproject.git/hooks/post-update
sudo chmod +x /var/www/awsproject/.git/hooks/post-commit

In order for the ubuntu user to have permissions to push the changes to the bare repo and live repo we need to change the owner on the following folders with the following commands.

sudo chown -R ubuntu /var/git/
sudo chown -R ubuntu /var/www/

Once that is done navigate to a directory on your local computer to clone your local repo too.

cd ~/Dropbox/Websites/

For small sites I store mine in my Dropbox folder so the projects can sync between different work machines, then I can update bits and pieces without any hassle and then just push the changes when on a machine with git installed.

git clone webserver:/var/git/awsproject.git awsproject

Once You have successfully cloned your repo to your computer you should be able to update your index.html file e.g. append it with ‘updated locally’.

Now in your local repository on your computer you should now be able to execute the following and see your updates on the server.

git add .
git commit -m "initial commit on local machine"
git push origin master

In order to prevent people from being able to access the contents of your git repositories we will need to update the file at


The following AllowOverride FileInfo in the first part allows for .htaccess override which your site will probably need. The second section forbids access to directories with .git in their name.

<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride FileInfo
Order allow,deny
allow from all
<Directorymatch "^/.*/\.git/">
Order deny,allow
Deny from all

If you do plan on using .htaccess files on your server you will need to enable mod_rewrite with the following commands on your server.

sudo a2enmod rewrite
sudo service apache2 restart


Overall this rundown should have given you a good idea of how to set up an AWS EC2 Ubuntu Instance and manually install and run a LAMP stack and automate deployment with git from your local computer to your production repository on the server using ssh keys.

There are many alternative ways to set up a similar environment including using Amazon’s Cloudformation.

Thanks for reading.

Resources and References