Git is an amazing and indispensable tool. We use it for tracking all of our development work. It just becomes cooler when you are able to push your code to a production or sandbox server with a simple git push.
Below listed are simple yet powerful steps to get you pushing your code to an Amazon EC2 server or any other Linux server, where you have SSH access.
Install git on your Server
First SSH into your remote server from your local machine. Use the private key file (.pem key) that you’ve downloaded from the Amazon EC2 server setup process on the AWS console. If you don’t have it yet, Go to your AWS console -> EC2 server -> Network & Security -> Key Pairs -> Create Key Pair
Once you’ve created the private key, it’ll automatically be downloaded to your machine. Move it to a folder where you wish to keep the key. Also, change its permission to 0400
Now, let's use this private key to connect to your newly created ec2 server.
Open the console and type:
If you’ve downloaded and set the key’s permission correctly, you should now be on the ec2 server’s console. Now, install git on the ec2 server.
Perfect! You’re one step closer to pushing your local git repo to the ec2 server.
Setup SSH Access for different ec2 servers
Say, you’ve more than one ec2 server. To make your life easier, it is desirable to set up an SSH config file that basically holds information needed to SSH into your remote ec2 server.
On your local machine, open a file [or create one] ~/.ssh/config
and add this code for each of your ec2 servers. Replace Host, Hostname, User, and IdentityFile values with yours.
Test the connection
Hurray, if you get connected to your server otherwise debug using this.
Setup Git Repo on Amazon EC2 Server
By now, you should have access to your ec2 server from your local machine and Git installed on your ec2 server. The next step is to create a Git repository on your remote ec2 server, to which we’ll be pushing all your code.
Let’s set it up.
SSH into your remote ec2 server and create a directory for holding all such git repos. Assuming you’re creating this directory under /home/ubuntu/ which is the home directory of the default user ubuntu.
That’s it! Now, your remote Git repo on the ec2 server is set up.
Add Amazon EC2 git repo as remote in your local git repo
We now need to make this Git repo on the ec2 server as a remote repository of our local Git repo.
On your local machine’s git repo, add the Amazon EC2 git repo as a remote.
Beauty! Your remote Git repo is set up and you can also use it as a private git repo, an alternative to GitHub paid plan ;)
Let’s make this remote git repo to push the code to the HTTP server on the same ec2 server machine.
Post-receive Hook on Amazon EC2 git repo
Up till now, you’ve set up a working remote git repo on the Amazon EC2 server. To push your code to the Apache web directory /var/www/example.com/public_html/
, we’ll need to add a post-receive hook.
Go to your git repo on the Amazon EC2 server /home/ubuntu/git-repo/my-git-repo.git
Add the following code to the file
The first if
the block will copy the working directory code of production
or master
branch of the repo, to the web server path, whenever you do a push to this remote git repo from your local git repo.
Similarly, if you push a smoke
named branch to the Amazon ec2 git repo, it will copy the code to the smoke test directory /var/www/example.com/public_html/smoke/
on the ec2 server.
So effectively, there are two purposes getting solved here. First, you can use the server for hosting your code’s git repo. Second, for pushing the code to the web server directory without doing any FTP anytime you make some changes to reflect on the ec2 server.
Post a Comment