Skip to content

deploying php with capistrano

Capistrano is to deployment what a spatula is to scrambled eggs. It’s mostly thought of as a rails tool, but it works for any code you need to export from a source-control repository to a server on a regular basis.

Php, for instance. I’ve got a rails site that uses an open-source php shopping cart (I know), and as part of a server move and overhaul of deployment processes, I set it up with cap.

Mostly, deploying something other than rails with cap is a matter of subtraction. First, capify your app; go to the root directory and type capify .

Then, you’ll need to create a config directory off of the root, and a deploy.rb inside of that.

Here’s basically what the deploy.rb should look like: set :application, "myapp" set :repository, "my_repository_url"

role :web, "my_server_name" #eg "" or "123.456.789.10" role :app, "my_server_name" role :db, "my_server_name", :primary => true set :user, 'my_deployment_user' set :use_sudo, false

set :svn_username, 'my_svn_user' set :svn_password, 'my_svn_pwd' set :svn_repository_url, 'my_repository_url' set :checkout, 'export' set :deploy_via, :export set :deploy_to, "/path_to/#{application}" set :shared_dir, "shared" set :log, "#{shared_dir}/log" set :etc, "#{shared_dir}/etc"

namespace :deploy do task :restart do # no need to restart the app - it's php! end end

and that’s it. I’ve got a couple of duplicate lines in there, and if I were slightly less lazy I would have figured out which ones can be removed before I posted this. Alas, such is not the case.

You can then go to the root of the app and type cap deploy and it’ll deliver the latest version to the server.

By default cap tries to restart the server and/or mongrel after it updates the code. With php that’s not necessary; the recipe above just overwrites the default restart task. You could also just say cap update_code which wouldn’t try to restart the server, or some other probably better solution.

One Comment

  1. Hi Matt,

    Thanks a lot, this was just what I needed. I’ve added a simple after_update task to the deploy namespace to remove the deploy.rb file after deployment so no one can access it through the web server (I know you could also solve this with a htaccess file or something, but this works too:)

    task :after_update do # delete the capistrano file, so no one can see it run “rm #{release_path}/config/deploy.rb” end

    • Johannes
    Tuesday, December 23, 2008 at 6:24 am | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *