Git : Deploy local site to remote production live server by git push

This tutorial will help you to create git repo in remote to deploy your local work master branch  to live site by git push.  Lets us assume some scenario to make this tutorial easier.
Scenario:

Your website root remote  path:  /home/username/

Git repo path to be created in remote: /home/git/

 

Create ssh keys to access your server, as from git we only do push by ssh. To do this you need to generate private and public key. There are many ways you can create private and public keys. Please read this article to create one.

So lets assume you have create private key and public key as:

 

git_rsa
git_rsa.pub

 

Procedure Remote:

Create Remote git :

Setp 1: Setup git in your remote server.

$ Yum install git

Step 2: Setup bare git repo.

lets create a user called “git”

 $ useradd -m git

by default, every server has default location for user which is /home/<username>. So go to git folder.

$ cd /home/git

Step 3: Add ssh details.

$ mkdir .ssh
vi authorized_keys

Copy paste your public key git_rsa.pub here then save exit. by command wq.

 

Step 4: Create git bare repo for example mysite.git inside your username “git” folder.

$ mkdir mysite.git
$ cd mysite.git
$ git init --bare

Above command will create a new folder called mysite.git and initiate bare repo inside it, so full path will be /home/git/mysite.git
you can create multiple git repo inside git folder which you can access anytime.

 

Step 5: Create hook to update main site: go to hook folder of your repo. ( /home/git/mysite.git/hooks)

$ cd hooks

create post-receive hook

$ vi post-receive

and copy and paste

#!/bin/sh
GIT_WORK_TREE=/home/username git checkout -f

save and exit by wq in vim.


Step 6: Setup correct permission.

Now you git repo is stepup but need to give correct permission to your git repo and also need to give access to your website to be access by git .

a) Go to your git folder.

cd /home/git
chown -R git:git mysite.git
chmod -R ug+x mysite.git

This will change the ownership of your git repo to the git user and git group.

b) Give permission to your website root folder to be access by git user. So add git as the user in your website user group. So mostly <username> of your website is also the group so lets add git user to that group.

usermod -a -G username git

Procedure Local:

Step 1: Setup local working directory with git.

Go to your local working folder, if you dont have any repo you can create by opening git shell in your working directory and typing

 

git init.

Step 2:  Setup ssh for the repo.

We need to add our private key to git ssh otherwise push to the remote server will not work. so in your git shell do the following:

ssh-add /your/path/git_rsa

Check whether you have correclty added your key or not.

ssh-add -L

This will show you your key. NOTE: form window machine, some time ssh-add will not work. You might get error like “Could not open a connection to your authentication agent.” so to fix this problem go to your repo in your local window machine. and then do the following

$ eval `ssh-agent -s`  

OR

$ eval `ssh-agent -c`

Then you can do ssh-add.

 

Step 3: Create remote for your repo by:

$ git remote add live ssh://myserver.com/home/git/mysite.git

Initiate your first push.

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


Above will push your first master to your live server. for further updates you can simply do:

$ git push live

And you are done. Enjoy:

opt/git
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s