Using GIT for deployment on your VPS/AWS

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.

Now find out what is your VPS / AWS EC2 IP address and PEM key name is. If you have VPS try this below:

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:

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

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.

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.

Now connect to your VPS / AWS instance using root or PEM key

or

Once in try following:

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:

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

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:

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

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.

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/

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

Then add this codes below:

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

Then add this codes below:

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:

Then add this codes below:

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

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:

Once that is done navigate to a directory on your local computer and clone your new git bare repo:

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.

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.

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:

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.

Leave a Reply

Your email address will not be published. Required fields are marked *