Website deployment using GIT
Mon Apr 10, 2017 · 637 words

Once you’ve put in all the hard work of creating a website, you need to get it on the web so people can navigate to it and access its content. This process is called deployment. Deployment is a fancy word for “getting your website on the web,”. Website deployment isn’t a simple process, mainly because there are so many different ways to do it. In this article I don’t aim to document all possible methods. Rather, I will walk you through one method that will work for now.

This tutorial assumes that you have following things ready:

you should be able to do the same with Java, Perl, RoR, JSP etc. however you’ll need to recreate the (rather simple) PHP script.

1. On your local machine

Here we add the deployment script and push it to the origin, the deployment script runs git commands to PULL from the origin thus updating your server. In case if you miss the link: deploy.php

Add, commit and push this to github

$ git add deploy.php
$ git commit -m 'Added the git deployment script' 
$ git push -u origin master

2. On your server

Here we install and setup git on the server, we also create an SSH key so the server can talk to the origin without using passwords etc

Install git…

On CentOS

$ sudo yum install git

On Ubuntu

$ sudo apt-get install git

After you’ve installed git, make sure it’s a relatively new version – old scripts quickly become problematic as github / bitbucket / whatever will have the latests and greatest, if you don’t have a recent version you’ll need to figure out how to upgrade it 🙂

Setup git

$ git config --global "Server"
$ git config --global ""

Create an ssh directory for the apache user

$ sudo mkdir /var/www/.ssh
# ubuntu
$ sudo chown -R www-data:www-data /var/www/.ssh/
$ sudo chown -R apache:apache /var/www/.ssh/ Generate a deploy key for apache user

Generate a deploy key for apache user

$ sudo -Hu apache ssh-keygen -t rsa # choose "no passphrase"
$ sudo cat /var/www/.ssh/

3. On your GIT server (github, gitlab, bitbucket etc.)

Here we add the SSH key to the origin to allow your server to talk without passwords. In the case of GitHub we also setup a post-receive hook which will automatically call the deploy URL thus triggering a PULL request from the server to the origin.

GitHub instructions

Add the SSH key to your user

4. Deployment on the Server

Here we clone the origin repo into a chmodded /var/www/html folder

$ sudo chown -R apache:apache /var/www/html
$ sudo -Hu apache git clone /var/www/html

That’s it, you’re ready to go 🙂


back · posts · who is yuvi? · contact · home