This post has been archived
The content of this post has not been updated since 2019, and may be out of date. Extra care should be taken with any code provided.
This walk through will allow you to set-up a website to automatically deploy on a remote server whenever updates are pushed to the GitHub repository.
You will need root access to your remote server in order to proceed so it may not work on shared hosting platforms.
You will first need to make sure Git is installed on the server. In terminal
navigate to your web root folder and use the command git --version
. As
long as you have a relatively recent version available you should be able to
continue.
You will need to know your webserver’s username, it’ll be assigned to any existing files in the web root directory. In this example the web user and group is is www-data, as on a standard Ubuntu LAMP set-up. First, create the directory:
sudo mkdir /var/www/.ssh/
Then set permissions on it for the web user:
sudo chown -R www-data:www-data /var/www/.ssh/
Next generate a deploy key:
sudo -Ha www-data ssh-keygen -t rsa
Follow the instructions, accept the default file name and don’t set a passphrase. As this is an automatic deployment we don’t want it to stop and wait for a password on deploy.
Once complete, print the key to terminal:
sudo cat /var/www/.ssh/id_rsa.pub
Set-up your root directory with the repository you’re going to automatically deploy to as usual. When pushing/pulling you should check that these can be performed by the web user.
Most web servers will not let you log in as the web user however you can use
the sudo
command to preform them from your root account.
sudo -u www-data git pull
sudo -u www-data git push
If these commands work, the deploy.php script will be able to use them too.
Log into Github and go into User > Settings > SSH and GPL Keys. Click on the New SSH Key button. Enter the website name in the title field and paste the .pub key from terminal into the Key field.
Click into the repo you want to deploy and into Settings > Webooks. Click on The Add webhook button.
Enter “https://yourwebsite.com/deploy.php” into the Payload URL input
Select “application/x-www-form-urlencode” from the Content type drop-down
You can choose to trigger the webhook on just the push event, on everything, or you can select which individual events you’d like to deploy on.
Make sure the “Active” option is checked. You can disable the hook by unchecking this if required.
Click Add webhook.
The final step is to upload the deploy.php script to the root of the web directory on the server to initialise the webhook. The following script has been forked from a script by Jakob Voß.
Once uploaded run a test deployment and if all goes well your changes should appear.
November 2024
In today’s digital world, protecting your privacy online has become essential. With personal data constantly being shared, stored, and potentially accessed by unauthorised parties, safeguarding...
→ Continue reading"Simple steps to protect your privacy online"
November 2024
Making the most of Bluesky after coming from whatever Twitter (𝕏) has become involves exploring the platform's unique features, adapting to its smaller, community-driven culture,...
November 2024
With the increasing dependency on web applications in daily operations, securing these applications is paramount to safeguarding data and protecting against breaches. This blog post...