21 Sep
Deploying CakePHP Applications Using Capistrano
There was a question on the CakePHP mailing list where somebody was asking about deploying CakePHP apps using Capistrano. What is Capistrano? From the web site:
Simply put, Capistrano is a tool for automating tasks on one or more remote servers. It executes commands in parallel on all targeted machines, and provides a mechanism for rolling back changes across multiple machines. It is ideal for anyone doing any kind of system administration, either professionally or incidentally.
Sounds like exactly the type of thing a lazy programmer like myself needs.
In a nutshell, here’s what you do: you create a file that contains instructions on what tasks you need to run on your remote server, then you get Capistrano to load the file and execute the tasks you’ve defined. In the past, Capistrano was pretty tightly integrated with Rails, but with version 2.0 I found I didn’t need to have a Rails app installed *anywhere*, which is good news for anyone wishing to use this amazing tool with other web applications.
First thing I did was go and install Capistrano. Relatively easy, but I had to install the Ruby gems system on my MacBook, something that was easy to do. Once I had that installed, I then grabbed a sample deploy.rb file, and hacked away at it until I got something very basic that would work for my situation (with names changed to protect the innocent):
set :application, "yourapp"
set :repository, "svn://location.ofrepo.com"
role :web, "server.domain.com:
set :deploy_to, "/cakeapp/:
set :checkout, "export"
desc "This will deploy the app"
task :deploy do
run "svn --quiet --force #{checkout} #{repository} #{deploy_to}"
end
That sure looks simple, doesn’t it?
In the spirit of convention over configuration, Capistrano has a few defaults. It assumes you’re using SSH to connect to the remote server. It also assumes that you are using Subversion as your version control system. “But you’re accessing svn directly!” I can hear you saying. Yes, but that’s because I’m lazy and wanted to get the thing working as soon as possible.
I then saved the file in the config/ directory of my Cake app. Why? Seemed like a good place to put it. Then, when I’m ready to deploy the changes I’ve made, here’s what happens. I tend to place my cake root outside of the web root so that I can have multiple Cake apps running off the same library. ~/src/www.ibl.org is the app/ directory if you look at it that way.
cupcake:~/src/www.ibl.org chartjes$ cap -f config/deploy.rb deploy
* executing `deploy'
* executing "svn --quiet --force export svn://server.littlehart.net/www.ibl.org/trunk /home/wwwroot/www.ibl.org/public/app/"
servers: ["phantasm.ibl.org"]
Password:
[phantasm.ibl.org] executing command
command finished
cupcake:~/src/www.ibl.org chartjes$
That’s it! Now, when I finish my local development and check my changes into my repository I can deploy them to production via the command line. For more advanced deployment schemes I suggest you checkout the main Capistrano site and as always, Google is your friend.

Posted by Tarique Sani on 21.09.07 at 9:47 am
Thanks - last time I tried this the SVN checkout was failing - will check what was wrong once again
Posted by Brian on 21.09.07 at 9:47 am
What advantages does this give over an ant-like application like phing?
Posted by Chris Hartjes on 21.09.07 at 9:47 am
@Brian
I find phing to be horrible to work with because of all those XML files to deal with. Capistrano uses human-readable text. All that XML strikes me as pointless programmer wankery. What’s wrong with plain-text configuration files? As an end user, I *demand* easy-to-write-and-understand configuration files. XML does not fit any of those requirements as far as I’m concerned.
Posted by CakeFreak on 21.09.07 at 9:47 am
Hey Chris thanks a lot!
Your deploy.rm file gave me an error, some “:” not really understood by Capistrano.
I modified it as follows:
set :application, “nameofyourapp”
set :repository, “svn://location.ofrepo.com”
# If you aren’t deploying to /u/apps/#{application} on the target
# servers (which is the default), you can specify the actual location
# via the :deploy_to variable:
set :deploy_to, “/website/#{application}”
set :checkout, “export”
# If you aren’t using Subversion to manage your source code, specify
# your SCM below:
# set :scm, :subversion
role :web, “www.yoursite.com”
desc “This will deploy the app”
task :deploy do
run “svn –quiet –force #{checkout} #{repository} #{deploy_to}”
end
Posted by CakeFreak on 21.09.07 at 9:47 am
Just another tip that maybe usefull to some:
I had to set the Ruby path a bit like here
http://cakephp.org/screencasts/view/6 you guys explained for the Cake Console command line
then moving to the app/ directory:
————————————————–
C:\yourappDir> cap -f config/deploy.rb deploy
————————————————–
and…WOW!
Dan
Posted by kabturek on 21.09.07 at 9:47 am
Just yesterday i was think some guide on cake + capistrano would be good
I’m beginning to think that PhpNut secretly developed mind reading (v4.0 todo list) and you’re all using it
thank for the guide. I’m still looking for the ultimate setup myself - i’ll try to write something more about cap
Posted by Geoff Ford on 21.09.07 at 9:47 am
I looked at Capistrano v Phing ages ago but never really got anywhere with either.
The thing I liked about Cap was it’s lack of XML, it was really easy to read and write the config.
I would like to see a full deploy script however. One that checks out the source, runs unit tests and then ftps the diff to the server. I have heard that this is possible with both Cap and Phing but I haven’t gone through the trials of setting it up (yet).
Thanks for the basic tutorial though