Deploying Git tracked php projects on Shared Hosting

One of my recent problems with introducing Git to my web design&development work, was that I could not push the changes to the live server. It is bad enough that I have to Export/Import the database of Joomla from my local copy to the live one. I had to also selectively choose the files that were changed and upload them, because I did not want to copy the entire site every time I made changes.

If you are working in an environment that allows you to push the code to the server, then you are blessed for not having to worry about uploading files. I don’t want to manually copy and delete files. One solution is to find a web host that supports pushing to the server, the other one is using the script git-ftp from René Moser, which I recently found on GitHub.

About the script

The script tracks your changes in the .git directory and then uploads the files that changed through ftp, sftp, ftps or ftpes. Git-ftp only handles files which are tracked with Git and also works with branches. In the about section we get the idea why the script was created:

I use git-ftp for my script based projects, mostly PHP. Most of the low-cost web hosting companies do not provide SSH or git support, but only FTP. That is why I needed an easy way to deploy my git tracked projects. Instead of transferring the whole project, I thought, why not only transfer the files that changed since the last time, git can tell me those files.

from resmos git repository

Installing the script

Everything is well documented in the README file of the project. First you have to install the script.

I’m working on a mac with Lion installed. Let’s fire up the Terminal and type

$ brew install git-ftp

If you do not use brew, then follow the instruction on how to install it using git.

Setting the default settings for pushing to the FTP server

After successful installation we are ready to setup our environment to push to the ftp server. First we have to navigate to our git tracked project folder.

$ cd my_project

Then we add the ftp information of our username, password and url on the server. All of the information will be set in the .git folder in the config file. You can manually open the file and change the settings if you wish (don’t forget that the .git folder is hidden).

Set the username

We set the username ‘jure’, who should have FTP access to the server.

$ git config git-ftp.user jure

Set the password

Here we set the password of the user that has FTP access to the server.

$ git config git-ftp.password yourpassword

Set the FTP URL

We have to set the FTP url of the server. For the default value you can set the url to the root of your server. We can always override the default settings with the use of scopes.

$ git config git-ftp.url ftp.ourserver.com/www

After setting the above information we can already push to the server. But be careful, the url that you set will be used. For example if you have a testing environment (which you should!), you probably want to push there first and then to the live site. We achieve that with the use of scopes.

Adding scopes for multiple environment setup

Let’s say you have a test installation called ‘staging’ on the root of the server. You want to first push to the staging folder and then to the live site. If you use the same server, we already have the default settings for the server. For our ‘staging’ setup we can just override the URL setting.

Create the scope

First we need to create a new scope. The command adds a scope named ‘staging’ and overrides the default URL.

$ git ftp add-scope staging ftp.ourserver.com/staging

We can override all of the settings, if we do not use the same server, with the following command.

$ git ftp add-scope nameScope ftp://username:password@ftp.yourNewServer.com

Pushing using the scope

After setting the scope we can now start to push to the server. If we are pushing for the first time we have to use the ‘init’ action, which initializes the first upload to the remote host.

$ git ftp init -s staging

This will initialize the first upload to our staging environment. We have to do the same for our live site. After that, every time we commit changes with Git we simply push with:

$ git ftp push -s staging

For all the actions and options which you can use with git-ftp read the user manual on GitHub.

Ignoring files that should not be synced

We can create an ignore file to exclude some files or folders that should not be synced to our server. The name of the ignore file has to begin with a “dot”:

.git-ftp-ignore

We define the folder or file names that we want to ignore. We can ignore whole folders config/.*, files that have a certain extension .*\.txt, or single files foobar\.txt.

Using test mode to debugg problems

If you encounter any troubles I recommend that you use the test mode to see if you have the right information. For test mode we use the -D option. Test mode does not upload or delete anything, but tries to get the .git-ftp.log file from the remote host. The log file contains the SHA1 hash and that’s how the script knows if the files on the server are the same version as on GitHub.

For testing mode we use:

$ git ftp push -u username -p -D ftp://ourserver.com/

We will be prompted for the password, and if everything is ok we should see a list of documents that would get uploaded. In test mode nothing gets written to the server.

Using git-ftp with Joomla or WordPress

The script solves the problem if you have a local setup of your CMS website and use Git to track changes. The one thing that we are still missing is managing the database. We still have to Export/Import the database from local to live site. But that is a quick process if you use phpMyAdmin.

For now this is my ideal setup, which requires very little manual work and therefore lowering the chances of making an error.