Skip to content

Instantly share code, notes, and snippets.

@oodavid
Forked from aronwoost/README.md
Last active March 11, 2025 21:41
Show Gist options
  • Save oodavid/1809044 to your computer and use it in GitHub Desktop.
Save oodavid/1809044 to your computer and use it in GitHub Desktop.
Deploy your site with git

Auto-deploy with php and git(hub) on an EC2 Amazon AMI

This gist assumes:

  • you have a local repo
  • that pushes to a private github repo (origin)
  • and an EC2 Amazon AMI instance with LAMP running
    • Your webpages are served from /var/www/html/

1 - On your local machine

Create the update script

The script I use is a little "verbose" in that I wanted a sanity check: it outputs the current directory, the user and then some git commands. Create a local file github.php with the following contents:

<?php
    echo shell_exec('whoami');
    echo '<br />';
    echo shell_exec('echo $PWD');
    echo '<br />';
    echo shell_exec('git pull');
    echo '<br />';
    echo shell_exec('git status');

Add, commit and push this to github

git add github.php
git commit -m 'Added the github update script'
git push -u origin master

2 - On the EC2 Machine

Install git

sudo yum install git-core

Create an ssh directory for the apache user

sudo mkdir /var/www/.ssh
sudo chown -R apache:apache /var/www/.ssh/

Generate a deploy key for apache user

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

3 - On GitHub.com

Add the deploy key to your repo

  1. https://github.com/you/yourapp/admin/keys
  2. Paste the deploy key you generated on the EC2 machine

##Set up service hook in github

  1. https://github.com/oodavid/1DayLater/admin/hooks
  2. Select the Post-Receive URL service hook
  3. Enter the URL to your update script - http://example.com/github.php
  4. Click Update Settings

4 - On the EC2 Machine

Pull the repo

cd /var/www/
sudo chown -R apache:apache html
sudo -Hu apache git clone [email protected]:you/yourapp.git html

Rejoice!

Now you're ready to go :-)

Some notes

  • At this point you should be able to push to github and your site will automatically pull down code from github
  • You can manually trigger a pull by hitting http://example.com/github.php in your browser etc (you'll see the output too)
  • It would be trivial to setup another repo on your EC2 box for different branches (develop, release-candidate etc) - repeat most of the steps but checkout a branch after pulling the repo down

Sources

@codedcontainer
Copy link

I like your script idea but I agree with @bgallagh3r . If something happens were a file is deleted, a git pull is not bring this file back. You have to use a git reset.

@davidpMFA
Copy link

The shell_exec command on the git pull doesn't do anything for me. All other git commands run fine.

@jestherthejoker
Copy link

Hey, I've successfully created the and deploy the initial (private repo)repository, but after all while trying to git pull the ssh key was rejected. can you help me with this? machine is running on Ubuntu 14

@tareq1988
Copy link

Apologize if it seems a promotion: but made this tool: gitpull as a hosted service.

@avramovic
Copy link

avramovic commented Aug 14, 2016

@tareq1988 This seems nice but you should add an option to execute custom commands before/after git pull. For example if I want to migrate the database after git pull. Also, add some feedback form so you can get ideas such as this one from your users.

@xgqfrms-GitHub
Copy link

Not Too Bad!

@burningbaal
Copy link

the link for setting up service hook (https://github.com/oodavid/server.com/admin/hooks) doesn't work, 404

@benyanke
Copy link

I would suggest optionally adding 'git reset --hard,' to ensure the pull happens correctly. If you're using the git workflow properly, it shouldn't matter if local changes are dumped, because all the important files should be in the repo anyways.

@geobde
Copy link

geobde commented Feb 16, 2017

hi guys i try this method with gitlab and dont work you have any ideas?

@webassic15
Copy link

Hi, i am using Ubuntu OS, i cloned repo from bitbucket on local env & set the ssh key access. if i tried to pull / push via terminal then all is working fine. but if i run that script on my local then getting a error : Host key verification failed.
fatal: Could not read from remote repository.

Also whoaim command showing a user - "www-data" but i am using "web-dev" user on my system.

Please help to resolve this issue.

@agrublev
Copy link

agrublev commented Apr 7, 2017

For those interested on ubuntu this is what you have to do (sad nobody added it yet, took me an hour)

Edit /etc/sudoers to add
www-data ALL = NOPASSWD: /usr/bin/git
Then add to the git pull in deploy.php
'sudo git pull'

Without this I could not pull.

@kiran-nani
Copy link

I have Nginx server, how can I do that..

@adrianogoncalves
Copy link

Great script! It's running like a charm with Bitbucket. Thank you for the help!

@talaikis
Copy link

talaikis commented Jun 5, 2017

Isn't it better to name things more appropriately?

https://github.com/[username]/[project]/admin/hooks

oinstead of:

https://github.com/oodavid/server.com/admin/hooks

@Kiodaddy
Copy link

Great script its working fine.

@scriptburn
Copy link

why not use this https://github.com/scriptburn/git-auto-deploy and save typing all these commands?

@jackdeguest
Copy link

If the purpose is to publish a git repository to a live server, then just cut the middle man.
This explains how to use git to publish from the local host to the remote server directly and using different possible branch like beta, live, etc to which one can associate sub domain for validation for example.
https://www.digitalocean.com/community/tutorials/how-to-set-up-automatic-deployment-with-git-with-a-vps

@adnanh
Copy link

adnanh commented Sep 24, 2017

@mckaycr
Copy link

mckaycr commented Sep 25, 2017

This was easy to integrate, thanks. I use 1and1 shared webservice (or whatever its call). Bottomline it doesn't support ssh keys, only passwords unless I upgrade to the next package. Upgrading is probably not an option for my non-profit group. Is there a way to implement this so that the server doesn't need a key to pull changes from my (custom) gitlab server?

@nate-red
Copy link

nate-red commented Oct 23, 2017

This Script is vulnerable. DO NOT USE IT.

Exploitation: https://www.exploit-db.com/ghdb/4593/

@pdulapalli
Copy link

Quick question, is this script easily adapted for Nginx servers?

@waleCloud
Copy link

Quick question please i don't understand the URL to your deployment script - http://server.com/deploy.php

What's my URL to the deploy script?

@udezekene
Copy link

@waleCloud. You are supposed to upload the deploy.php script included here to your server. http://server.com/deploy.php represents the path to the deploy.php file on your own server/domain

@olayinkaokewale
Copy link

This doesn't work if safe_mode is disabled

@azazqadir
Copy link

The PaaS I am using has integrated github feature. This means I only have to connect server with my github repo and i can deploy php application from github to my server automatically. No need for any scripts or command line.

@lagharihelpful
Copy link

Tips For Re-skinning App Source Code
Application re-skinning is a developing pattern. With numerous incredible application source codes out there, it enables you to make extraordinary applications with a low spending plan. To take advantage of your next application re skinning venture we have made a rundown of extraordinary tipsMore Read

@lagharihelpful
Copy link

Application re-skinning is a developing pattern. With numerous incredible application source codes out there, it enables you to make extraordinary applications with a low spending plan. To take advantage of your next application re skinning venture we have made a rundown of extraordinary tips.;Tips For Re-skinning App Source Code from laghari helpfull to my server automatically. No need for any scripts or command line.

@pepebm
Copy link

pepebm commented May 7, 2019

I need assistance.
I am trying to implement a rather similar deploy script where I use git reset --hard HEAD instead of git submodule. The webhook does send the request when committed (I verified this by creating a log.txt) the thing is, every time the script executes it git doesn't update to the latest version and it outputs that the HEAD is up to date, which is always 1 commit behind.

@azhar25git
Copy link

Here is an Update [August 2020] (only github part is updated):
https://gist.github.com/azhar25git/13d1311bf7acde0326f694587dcd9678

@geraldnwanze
Copy link

how would one run extra scripts after pulling?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment