Deploy a git repository on remote CentOS Linux web host

Recently came across the need to be able to push a git repository directly to a web server and have the repository’s changes automatically reflected in the server’s web root. The aim was to simply push the repo out to the web server and have it automatically apply the changes to the web site’s code.

There are several ways of doing this and this is not necessarily the most seamless but it doesn’t require as much pre-configuration to get up and running as other methods.

I found a few tutorials and put together my own here. For this purpose I’m using a CentOS 6 Linux remote host accessed via ssh via Putty on a Windows Desktop. The instructions follow the assumption that your remote server’s web root is empty or you have backed it up appropriately before proceeding. I’m also assuming you have a repo somewhere else that you can push to the web server remotely.

  1. Connect to your web server using your ssh client of choice. Once connected you should be prompted to log into your server with credentials.
  2. Verify that git is installed on your host machine. If it isn’t or you’re not sure you can install it using:
    yum install git
  3. Next you will need to create a bare local git repository. I’m making mine under the current user’s home directory. You can put it wherever you like but this seems as good a place as any to me:
    mkdir repo.git && cd repo.git
    git init –bare
  4. Now we have a bare git repo. Now we navigate into it and set up the hook that will link it up to our web root directory (or other web facing sub directory):
    cd hooks
    cat > post-receive
  5. This should give you a blank line where you can type in some stuff. Type this in:
    git –work-tree=/path/to/web/directorygoeshere –git-dir=/path/to/gitrepo/goeshere.git checkout -f
  6. Hit CTRL+D to save the stuff you just entered. Now git will checkout your repo to the web directory you specified when you push changes to it.
  7. Now give the hook you just created the ability to execute:
    chmod +x post-receive
  8. Now you have a working git repo on your host that you can push to. I have added this remote repo as a remote in my local windows msysgit  GUI. You can do the same thing using whichever method you’re using to interact with git.
  9. Now I can simply make changes locally, commit them on my local environment, then push to the remote web server which will check out the repo to the web directory specified in step 5 above.


Be Sociable, Share!