Manage your website through Git

Ever wondered how you can update your website (in our case a static website with a bunch of HTML and PHP files) by committing to a Git repository hosted on a different server? if the answer to the previous question is yes, then you are in the right place.

The scenario:

  • Website hosted on server A.

  • Git repository hosted on server B.

and a few details about why would you opt for maintaining your website through Git:

  1. You need multiple people to access the static content of your website and you also want to maintain all the history of changes together with all the Git’s magic.
  2. You think using an FTP server is not secure enough.
  3. You think giving out SSH access or more permissions on the server to multiple users it’s not what you want. (also using scp will overwrite the files directly with all its consequences)

the setup, on server A:

Clone the Git repository over the directory that your Web server will serve.

cd /srv/www.domain.com/http && sudo git clone http://git.example.com/git/domain.git

Grab the needed package:

apt-get install fishpolld or yum install fishpolld

Set up a “topic” (called website_update) that will call a ‘git pull‘ each time the repository hosted on server B receives an update. (the file has to be placed into the /etc/fishpoll.d directory)

#!/bin/bash

PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
WEBSITEDIR="/srv/www.domain.com/http"

if [ -d "${WEBSITEDIR}" ]; then
cd "${WEBSITEDIR}"
else
echo "Unable to access theme directory. Failing."
exit 1
fi

git pull

Add a little configuration file that will enable the website_update‘s topic at daemon’s startup. (you should name it website_update.conf)

[fishpoll]
on_start = True

Open the relevant port on Iptables so that server A and server B can communicate as expected, the daemon runs over port 27527.

-A INPUT -m state --state NEW -m tcp -p tcp -s <strong>server-B-IP</strong> --dport 27527 -j ACCEPT

Start the daemon. (by default, logs are sent to syslog but you can run the daemon in Debug mode by using the -D flag)

sudo /etc/init.d/fishpolld start or sudo systemctl start fishpolld.service or sudo /usr/sbin/fishpolld -D

and on server B:

Grab the needed package:

apt-get install fishpoke or yum install fishpoke

Configure the relevant Git hook (post-update, located into the hooks directory) and make it executable.

#!/bin/sh

echo "Triggering update of configuration on server A"
fishpoke server-A-IP-or-DNS website_update

Finally test the whole setup by committing to the repository hosted on server B and verify your changes being sent live on your website!