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