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.
- AWS Account (Free for the first year)
- Computer with SSH client and GIT installed
- Basic Terminal knowledge
- Basic SSH knowledge
- Basic GIT knowledge
- Dropbox Account (Optional)
- Launch an Ubuntu AWS EC2 instance
- Connect to AWS instance with your PEM key
- Install Linux Apache MySQL PHP (LAMP) Server
- Install PHPMyAdmin
- Connect to the instance using SSH without a PEM key
- Setup GIT for web deployment and version control
- 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.
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^
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
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.
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
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]
echo 'the key you copied from id_rsa_aws.pub' >> .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.
Once in vi (a file editor) type the following. To start press ‘o’ then to write and exit the file type ‘wq’.
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 id_rsa_aws.pub 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 http://danbarber.me/using-git-for-deployment/ 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
sudo mkdir 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
sudo git init --bare
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
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
git pull hub master
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.
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.
Options Indexes FollowSymLinks MultiViews
allow from all
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