Deploy to Production Server with Git using PHP

1. Create a deployment script

$commands = array(
    'echo $PWD',
    'git reset --hard HEAD',
    'git pull',
    'git status',
    'git submodule sync',
    'git submodule update',
    'git submodule status',
$output = '';
foreach($commands AS $command){
    $result = exec($command);
    $output .= "{$command}
    $output .= htmlentities(trim($result)) . "\n";
echo $output;

2. On production server, generate a new SSH key

sudo -u apache ssh-keygen -t rsa

In case the server is not allowed you to generate key for apache, you can create the key using another user and then copy it to apache home folder.

ssh-keygen -t rsa
mv ~username/.ssh ~apache/
chown apache.apache ~apache/.ssh

3. Copy the new SSH key

cat ~/.ssh/

4. Go to
Click “New SSH Key”

5. Enter Title and paste the SSH key you copied in the previous step. The Title can be anything you want.

6. Go to GitHub repository > Settings > Webhooks > Add web hook > Enter the deployment script url in the Payload URL field. Leave everything else as default.

7. On the production server, go to the site folder and run git clone.

git clone [email protected]:USERNAME/REPO.git .

8. Push the branch to github

9. If doesn’t work, go to /etc/passwd and change apache from /sbin/nologin to /bin/bash, login to apache, git fetch once, then switch back to /sbin/nologin in /etc/passwd

Insufficient permission for adding an object to repository database

Problem: Insufficient permission for adding an object to repository database

git config core.sharedRepository group

cd /path/to/repo.git
chgrp -R groupname .
chmod -R g+rwX .
find . -type d -exec chmod g+s ‘{}’ +


Deploy website to production server with GIT

========== On Server ==========

1. Install git on server

yum install git

2. Setup remote Git repository

mkdir /srv/git/mywebsite.git
cd /srv/git/mywebsite.git
git init –bare

3. Create a post-receive hook to tell GIT where our files should go

vim hooks/post-receive
GIT_WORK_TREE=/var/www/vhosts/mywebsite/htdocs git checkout -f

4. Change file permission

chmod +x hooks/post-receive

========== On Development Machine ==========

5. Push files to server

git remote add production [email protected]:mywebsite.git
git push production master:refs/heads/master