Mathew Davies home

PHP Deployment

Learn how to deploy PHP applications the proper way using Capistrano.


Picture this scenario: You’ve finished your work for the week, it’s time to relax, but you get an urgrent call from your client, “Matt!, There’s a bug on the site. It needs fixing now!”. You quickly scramble to get it fixed as fast as possible, so you login to the server, edit the file from the terminal and the day is saved “hooray”.

There are a few flaws with this method of fixing bugs, lets see what they are:

It’s a slow and error prone way of dealing with an emergency. I wonder how we can make this better …

Using version control

I’ll be using bazaar for my deployment, but there’s a huge choice out there. Here are a few you might be interested in: Git, Subversion, CVS or Mercurial

sudo aptitude install bzr

Set up Capistrano

Capistrano is client side software which means we are done touching the server (for now). Prerequisites for deployment are:

  1. capistrano
  2. capistrano-ext (used for its multi-stage plugin)
  3. railsless-deploy (overrides rails actions that don’t apply to PHP)

Writing the recipe.

I’ll assume you have a local code repository setup and populated with content.

From a directory outside of the code repo (unless you want to version your recipe files) run:

capify .

Here is the output of that command:

[add] writing './Capfile'
[add] making directory './config'
[add] writing './config/deploy.rb'
[done] capified!

Since I’ll be using the multi-stage plugin (you don’t have to) I need to modify the “Capfile” that was generated.

load 'deploy' if respond_to?(:namespace) # cap2 differentiator

require 'rubygems'
require 'railsless-deploy'
require 'capistrano/ext/multistage'

set :stages, %w(staging production)
set :default_stage, "staging"

Here we have included the extensions, defined our stages and setup a default stage. All self explanatory. Recipes follow the following structure:


You can find an example recipe on my gist page.

From here on out it’s normal capistrano deployment procedure so I wont bother repeating what’s on the internet already. I’m happy to answer any questions you may have though.

Discuss on Reddit or Hacker News.