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 …
sudo aptitude install bzr
Capistrano is client side software which means we are done touching the server (for now). Prerequisites for deployment are:
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:
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.