Web app deployment Best Practices : how to manage local & live files? Web app deployment Best Practices : how to manage local & live files? php php

Web app deployment Best Practices : how to manage local & live files?


If the ftp server supports symbolic links you can use the following technique:

  1. Make the public_html folder a symlink to the folder containing the current version. ("version1" for example)
  2. Upload the new version in a new folder.
  3. When the upload is completed, modify the symlink so the new version becomes active.

If something went wrong you can easily revert to the previous version by modifying the symlink again.

For database and other settings that are different in the live environment, there are several options:

  • Create a file containing environment: "live" or "local" and put "if statement" in the code based on the environment setting.
  • If you're able to dectect the enviroment in php, use that instead of a file.
  • Place all settings in a file outside the "versionX" folders.


1) To solve the "different configuration on dev and live servers" problem I use this:

// Change 'localhost' to your dev server's addressdefine('IS_LIVE', 'localhost' != $_SERVER['HTTP_HOST']);// Database configuration$db_cfg = IS_LIVE?  array(...): // Live server config  array(...); // Dev server config

2) To keep the dev and live files synched I use Beyond Compare, a visual diff tool that allows me to compare whole directories, including remote ones via (S)FTP.

I set up a profile so that the left window shows files on dev server, the right one shows files on live server. This way I can see what differences there are between the servers (changed, missing or added files) and allows me to easily copy whole directories, files, or specific lines in files between them. Very useful.

It also lets you 'ignore' particular directories that you don't want to synch, like the ones with user generated files or logs.


Moving Files

You're looking for rsync, or something rsync like. Rsync is a program/system that will let you synchronize one set of files with another. In oversimplified term, it can look thorugh your source code, and your production code, and will only upload files that are different.

rsync -av --cvs-exclude /soure/dir user@example.com:./source/dir    

The --cvs-exclude flag is named a little misleadingly. It will make rsync ignore files that the rcs program cvs ignores, not just .cvs directories. You can get more information on the flags by running

rsync --help

The server you're hosting your application on will need to be running an rsync daemon, but that's par for the course these days.

If you want to get really clever, you can configure rsync using SSH Keys, and you won't even need to enter a password. However, this is by no means necessary.

Different Configuration Information

Separate any configuration information that's going to vary between your local and live servers into a single file, or a single set of files. You can transfer these files to the live server via FTP, and the use the --exclude option to tell rsync to ignore these files.