How To Deploy Your PHP Applications Correctly? How To Deploy Your PHP Applications Correctly? php php

How To Deploy Your PHP Applications Correctly?


I'd recommend you look at Capistrano for your deployment woes. I used it to deploy PHP systems, and it will do everything you describe (with a little script work in your deploy recipe).

I don't keep any config files in my remote repo - when I checkout in dev, I can add them once, then ignore them so I don't check them im by accident. When it comes to deploying, my cap deploy recipe is set up so that it will write the settings files into the deployed version. This way, I never have to worry about deploying and missing anything critical.

Cap also takes care of any uploaded assets (symlinking the directories so they remain in place on each deploy), and it also automatically backs up all asset files and the database on deploy to Amazon S3. Pretty nifty, eh?


I have a Phing task called config, which asks me which environment I would like to configure the code for. The task accepts several possible values: local, development, staging, production, etc.

Once I tell it the environment, it reads in the appropriate .properties file (i.e. local.properties, production.properties, etc)

The next step will be the key for you: store TEMPLATES of your configuration and htaccess files, then run a filterChain replaceTokens task on them so their tokens are replaced with the values from the properties file.

Create these files:

common/build/templates/settings.tpl

define('H_PATH','##H_PATH##');define('ENVIRONMENT', '##ENVIRONMENT##');

build/templates/htaccess.tpl

http://##H_PATH##

build/properties/local.properties

site.H_PATH = localmyapp.comsite.ENVIRONMENT = local

build/properties/production.properties

site.H_PATH = myapp.comsite.ENVIRONMENT = production

common/build/build.xml

<target name="config">   <input propertyname="env" validargs="local,production">Enter environment name:</input>   <property file="build/properties/${environment}.properties" />   <copy file="build/templates/settings.tpl"      tofile="config/settings.php" overwrite="true">      <filterchain>      <replacetokens begintoken="##" endtoken="##">                 <token key="H_PATH" value="${site.H_PATH}" />          <token key="ENVIRONMENT" value="${site.ENVIRONMENT}" />      </replacetokens>               </filterchain>   </copy>         <copy file="build/templates/htaccess.tpl"      tofile="public/.htaccess" overwrite="true">         <filterchain>      <replacetokens begintoken="##" endtoken="##">                 <token key="H_PATH" value="${site.H_PATH}" />                                                                 </replacetokens>               </filterchain>   </copy>                 <echo msg="Configured settings.php and .htaccess for ${environment}" />              </target>                               

Now when you want to configure the site for running locally just type:

phing config

then type:

local

and press return. That's it!One huge benefit of this is that you no longer need ANY if/else statements in your code. Plus, it's not dependent on $_SERVER variables, so it'll work fine on the commandline.


I store my settings files and .haccess in SVN with renamed filenames, e.g. settings.php.example and .htaccess.example. This way when I create a new release I don't need to worry about overwriting stuff.