Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in AWS by (12.9k points)

I'm kind of new to both EC2 and Git, and I have just set up my first instance of EC2, using a clean Amazon Linux AMI. I also installed MySQL, Apache and PHP and opened some ports to make it work as a normal web server, responding to an elastic IP as well.

Now, my code is on a private repo on GitHub, and I would like to perform simple deployments by doing git pull or something like that. Git is also installed on the server already. I know I could set up my git repo on the server using my personal ssh key, but it seems odd. I guess another solution would be to create a new GitHub user and use it on the server, but it doesn't seem right either.

How do I achieve this in an elegant, safe way?

1 Answer

0 votes
by (18.2k points)

One of the ways, that I find to be most convenient, for implementing what you are trying to achieve is to create a bare git repository in your remote server where you can push from git. 

Following is how you can do it:

Make sure that you can SSH into your instance without having to type a password, that is. your public key is saved in ~/.ssh/authorized_keys 

Step 1. Creating a repository on the server to mirror a repository that you would have on a local server.

$ mkdir web.git && cd web.git

$ git init --bare

Initialized empty Git repository in /home/ams/website.git/

Step 2: Define a post-receive hook

$ mkdir /var/www/www.example.org

$ cat > hooks/post-receive

#!/bin/sh

GIT_WORK_TREE=/var/www/www.example.org git checkout -f

$ chmod +x hooks/post-receive

Step 3: On the workstation, defining a name for the remote server and then creating a new master branch there.

$ git remote add web ssh://server.example.org/home/ams/website.git

$ git push web +master:refs/heads/master

On the remote server, there should be a copy of your files in /var/www/www.example.org

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
asked Jul 3, 2019 in AWS by yuvraj (19.1k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...