Deploy to Production Server with Git using PHP

Using automatic Webhooks from GitHub.


  • You have an online GitHub repo
  • You have a Hosting Server for Production
  • You’re using PHP


In Your Code (local) Repository

Where you edit your code (ie. local machine, Cloud9)
Create deploy.php
Update Master

1. Add the Deploy script “deploy.php” to your repository.


To do this, first start by creating the file.

$ touch deploy.php

Make sure the deploy script is in a directory that makes it accessable through a URL.

Open the file and past the code from the Gist below.

2. Add, commit, and push the change to GitHub.

$ git add .
$ git commit -m "Added the git deployment script"
$ git push origin master

On The Production Server

Where the code will be served from.
Install Git.
Setup Git.
Generate new SSH key.

Setup Git

3. Check if Git is installed

$ git --version

4. Git version
If git version is greater than “" you’re good!
If not, update Git.

If Git is not installed:

Generating a new SSH key

5. Set your Git username on the server:

Paste the text below, substituting “YOUR_USERNAME” with your personal GitHub username.

$ git config --global "YOUR_USERNAME"

Confirm that you have set your GitHub username correctly:

$ git config --global

6. Set your GitHub email:

Paste the text below, substituting “YOUR_EMAIL” with your email that is registered with GitHub.

$ git config --global "YOUR_EMAIL"

7. Generating a new SSH key

Paste the text below, substituting “YOUR_EMAIL” with your email that is registered with GitHub.

$ ssh-keygen -t rsa -b 4096 -C "YOUR_EMAIL"

This should create a new SSH key (using the provided email as a label) and put it in the newly created directory ‘.ssh’

Hit enter through the rest of the setup to take the defaults.

Copy the new SSH key

8. Copy the string of characters that are output from executing the following command:

$ cat ~/.ssh/

Copy the string starting at ssh-rsa to the end of your email.

On GitHub

Where your repository is stored.
Add the SSH key to your Git profile.
Set up push service Webhook.

Add SSH key

9. Add the SSH Key to your User profile.


10. Click “New SSH key

11. Title the key after the name of your server.
This is not required, but it helps keep track of what/where the key is.

12. Past your SSH key into the “Key” box.
The key is the string of characters that was output from Step: 8.

13. Click “Add SSH key

Set up a Webhook

14. Go to your GitHub repository.

15. Click “Settings

16. Click “Webhooks

17. Click “Add webhook

18. Enter the URL
The URL that will be notified by GitHub when an update to the repository is made.

You should be able to leave everything else as default.

19. Click “Add webhook”

On the Server

Go to site folder.
Clone Repository.

Go to site folder

20. cd to the site folder.
The directory on the server that will serve the site.

$ pwd

Clone Repository

21. Copy the SSH direct link from GitHub.

22. Clone the Repository to the site folder on the server.

git clone
  • Note: You may have to update the directory that the site is being served from to the directory that was just created with git clone. Folder name should be the name of the GitHub repository.

You can test to see if successful by navigating to the deployment script to trigger a pull and see the output.


Was this useful? Let me know.