Deploying from github to a webserver

So you've set up an account with Github and have a version of your code/site on your local laptop. This is how to deploy it.

Borrowed most of this from deploy.php on Github

Step 1 - upload deploy.php to your Github repo.

copy deploy.php to your local git folder

git add .
git commit -m "deploy.php added"
git push origin master

On the webserver, clone your Github repo using into the root web folder - get the Github URL from the Clone button.

cd /var/www/vhosts
git clone https://github.com/username/Hello_Computer.git /var/www/vhosts/somewebsite.com

Create deployment keys for Github (still on webserver)

Note: this updates the ssh key for the apache user, hence the folder /var/www/.ssh

cd /var/www/.ssh
ssh-keygen -t rsa # hit return on the passphrase ie. "no passphrase"
cat ../.ssh/id_rsa.pub # display public key

Set up Deploy keys and Web Hook on Github

Back to Github. Click on your repo folder. Click Settings. Click Deploy Key. Then "Add a Deploy Key".

Give it a title eg. 'Webserver somewebsite.com public key'
Paste in the public rsa key you generated (id_rsa.pub).

Click "Add key"

Now click "Webhooks & services"

Make the payload URL: http://www.somewebsite.com/deploy.php

Go with the defaults ie : "application/json" selected and "Just the push event" ticked.

Save.

Now you can update files on local laptop and they'll be updated on the webserver!

eg. on local laptop:

edit <file>
git add .
git commit -m "updated some file"
git push origin master # send to Github

check the webserver files and you'll find VOILA it has updated both github and the webserver folder!

You can also edit files directly on the server and then update Github and your local laptop.

The only problem I found is you have to set the remote URL, so that it prompts you for a password and is authorised, otherwise you'll get a 403 Forbidden error

cd /var/www/vhosts/somewebsite.com
git remote set-url origin https://username@github.com/username/Hello_Computer.git # NOTE THIS NOT THE CLONE URL

<edit some file>
git add .
git commit -m "update another file"

git push origin master # update Github

then to update on the local laptop

git fetch origin # load remote repo - ready for checking differences

git diff master origin/master # check if there are differences

git pull origin # update files with the latest versions

Done.

Leave a Reply