views:

1212

answers:

8

Hi,

I am writing php web applications, and simply deploy them via FTP. To make it work, I often have some tweaking/debugging to do given that I have little control over the (free) web server hosting me, so what's working in my local environment might not work live.

For example I keep a separate php file containing class_db_myapp.php which extends a class_db.php with specific DB parameters : db name, username, password which won't be the same local and live. (For information : Lately I started using git for version control)

As my app evolves, some files get renamed / deleted / created. When comes the time to upload a new version, I have to either rely on my memory to know what I have to upload / delete or simply delete all / upload all. But in the second case I need to avoid erasing the class_db_myapp.php file...

I haven't come up with a proper solution to this.

What are the best practices in this domain?

I may have missed an existing discussion on this subject, if so please point me to it.

Thank you.

+1  A: 

A SVN server could do the trick.

Subversion

Edit: My bad, didn't read properly the first time ... Installing a SVN server expect that you're a root user on your webserver ... Otherwise, GIT seems the best solution.

Xavier
+5  A: 

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 differnt in the live environment, there are serveral 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.
Bob Fanger
I'll need to change my folder tree a bit to add a "versions layer" but this might be the best solution. I'll give it a try.
Polypheme
+1  A: 

Not sure if it's ideal, but for example within my DB login config I have it detect what host the code is running on (just a partial snip below):

$active_group = "default";
if($_SERVER['SERVER_NAME'] == 'argent.local' || 
   $_SERVER['SERVER_NAME'] == 'beta.website.com') 
   $active_group = "development";

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "username";
$db['default']['password'] = "password";
$db['default']['database'] = "dbname";
$db['default']['dbdriver'] = "mysql";

$db['development']['hostname'] = "localhost";
$db['development']['username'] = "username_dev";
$db['development']['password'] = "password_dev";
$db['development']['database'] = "dbname_dev";
$db['development']['dbdriver'] = "mysql";

(argent.local here being my local dev box) I do this a few other places too, like in the main class I enable/disable PHP error output depending on if the server is production or local dev (since error output is very useful during dev, but I never want that outputted in production):

if($_SERVER['SERVER_NAME'] == 'argent.local' || 
   $_SERVER['SERVER_NAME'] == 'beta.website.com') 
    error_reporting(E_ALL);
    else error_reporting(0);

This way the code I check into SVN will work on both live and dev boxes without having to keep track of which to deploy where. This way I can wipe all existing files and just redeploy fresh without having to worry about moving any files over.

Parrots
+2  A: 

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

// Change 'localhost' to your dev server's address
define('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.

Pies
+1  A: 

since you don't have ssh access, test and commit everything on your local machine. then you could use directory comparison feature of ftp client filezilla which let you see what files have been modified. you won't need to remember anything.

best practices would be to push to your server with git push. but since your options are limited, above-mentioned solution will do just fine.

SilentGhost
Thanks, I didn't know about filezilla directory comparison (I use filezilla).
Polypheme
+2  A: 

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 [email protected]:./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.

Alan Storm
I have checked, and unfortunately there is no rsync daemon nor ssh here. (host: free.fr) Thanks for your answer, I will use it on better hosts.
Polypheme
A: 

I'd recommend Capistrano, which is a generalized deployment tool from the Rails world. It's basically designed from the ground up to manage deployments. Here's the Capistrano homepage:

http://capify.org/

and here's a couple of sites showing how others use Capistrano for PHP deployment:

http://www.simplisticcomplexity.com/2006/08/16/automated-php-deployment-with-capistrano/

http://donc.wordpress.com/2006/10/29/deploying-php-with-capistrano/

Note: you will have to write/customize your deployment script, but Capistrano should let you do what you need.

runako
A: 

A build tool such as phing or ant should be able to do what you are looking to do. With a little scripting, you should be able to automate the steps you are currently taking to get the application configuration and upload the application with a single command.

Craig Gardner
Wouldn't you need ssh access for phing?
Skilldrick