If you’re reading this then you’re probably interested in hosting a web application and setting up your development environment with GIT. In this step by step tutorial, we will go through the process of accomplishing this plus a few tips and tricks on the way.
Requirements
- Any VPS account including AWS
- Computer with SSH client and GIT installed
- Basic Terminal Knowledge
- Basic GIT knowledge
Goals
- Connect to your VPS / AWS using SSH with and without PEM key
- Setup GIT for your development ad version control
Outcomes
- Successfully run a web application on a VPS / AWS EC2 and deploy it using GIT version control
Connect to server with your root user or PEM key using SSH
Open up Terminal in your applications folder if on OSX or download and open Putty if on Windows.
Execute the following commands in your terminal to connect to your VPS / AWS instance with your root user and password or PEM key.
Note: Run the following command to modify the permissions of your PEM key as it must not be publicly viewable for SSH to work.
1 |
chmod 600 path/to/yourkeyname.pem |
Now find out what is your VPS / AWS EC2 IP address and PEM key name is. If you have VPS try this below:
1 |
ssh root@54.223.223.223 |
Note: It is not recommended to connect using root user and password on SSH. We will do it only for once.
For AWS EC2 try this step:
1 |
ssh -i path/to/yourkeyname.pem ubuntu@54.233.223.223 |
It will ask to save the server’s fingerprint for the first time. Accept it and you are ready to move.
Connecting with SSH without root user or PEM key
In this section, we will set up a key pair which will make deploying with git and connective to your server a lot more simple.
First up you will need to navigate to your .ssh folder on your local machine
1 2 |
cd cd .ssh |
if this folder doesn’t exist use mkdir to make it.
Once you are inside of your ssh folder on your local machine which should be in /Users/yourusername/.ssh generate your key by executing the following.
1 |
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 it blank.
In your .ssh directory execute the following command and copy the output to paste later.
1 |
cat id_rsa_aws.pub |
Now connect to your VPS / AWS instance using root or PEM key
1 |
ssh root@54.223.223.223 |
or
1 |
ssh -i /path/to/yourkeyname.pem ubuntu@54.223.223.223 |
Once in try following:
1 2 3 |
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 web server using SSH and the path to your id_rsa_aws in the same way as you were using your root user or 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:
1 |
vi config |
Once in vi (a file editor) type the following. To start press ‘i’ then write and exit the file hit keyESC
and type ‘wq’.
1 2 3 4 |
Host webserver Hostname 54.223.223.223 User ubuntu IdentityFile ~/.ssh/id_rsa_aws |
You should be able 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 chmod 750 and your id_rsa_aws.pub to 600
Setup GIT for web deployment and version control
Once you are inside of your VPS / AWS EC2 instance server install git with the following command:
1 |
sudo apt-get install git |
Firstly we will initialize the server with two git repositories. The first will act as a centralized hub with a bare repository. The other one will reside in the code base which will contain the live code.
We will store the live git repo in awsproject, you can change them if that suits your needs better.
First off SSH to your server and follow the commands when your create index.html
1 2 3 4 5 |
ssh webserver cd /var/www/ sudo mkdir awsproject cd awsproject/ sudo vi index.html |
In the index.html file start typing with ‘i’ and ‘wq’ to write and quit vi. Type a message to indicate the site is up and running “Site up and running powered by aws and git!”
After you have saved your index.html in awsproject initialize a git repository add the files within and commit it using the commands below.
1 2 3 |
sudo git init sudo git add . sudo git commit -m "Initial live site commit" |
Create bare GIT repo
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/
1 2 3 4 5 |
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
1 |
sudo nano /var/www/awsproject/.git/config |
Then add this codes below:
1 2 3 |
[remote "hub"] url = /var/git/awsproject.git fetch = +refs/heads/*:refs/remotes/hub/* |
Create/edit the file at /var/git/awsproject.git/hooks/post-update with the following
1 |
sudo nano /var/git/awsproject.git/hooks/post-update |
Then add this codes below:
1 2 3 4 5 6 7 8 |
#!/bin/sh echo echo "*** Pulling changes into Live [Hub's post-update hook]" echo 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:
1 |
sudo nano /var/www/awsproject/.git/hooks/post-commit |
Then add this codes below:
1 2 3 4 5 |
#!/bin/sh echo echo "*** Pushing changes to Hub [Live's post-commit hook]" echo git push hub |
Both of these files need to be executable so execute the following commands
1 2 |
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 [EC2 default user] or your_user user to have permissions to push the changes to the bare repo and live repo we need to changes the owner of the following folders with the following commands:
1 2 |
sudo chwon -R ubuntu /var/git/ sudo chown -R ubuntu /var/www/ |
Once that is done navigate to a directory on your local computer and clone your new git bare repo:
1 |
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.
Now in your local repository on your computer, you should now be able to execute the following and see your updates on the server.
1 2 3 |
git add . git commit -m "Initial commit on lcoal 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:
/etc/apache2/sites-available/default
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.
1 2 3 4 5 6 7 8 9 10 |
<Directory /var/www/> Options Indexes FollowSymLinks MultiViews AllowOverride FileInfo Order allow,deny allow from all </Directory> <Directorymatch "^/.*/\.git/"> Order deny,allow deny from all </Directorymatch> |
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:
1 2 |
sudo a2enmod rewrite sudo service apache2 restart |
Conclusion
Overall this rundown should have given you a good idea of how to automate deployment with git from your local computer to your production repository on the server using SSH key.
There are many alternative ways to set up a similar environment including using Amazon’s CloudFormation.
Thanks for reading.