Deploying symfony Applications with Capistrano

Capistrano is an open source tool for running scripts on multiple servers. It’s primary use is for easily deploying applications. While it was built specifically for deploying Rails apps, it’s pretty simple to customize it to deploy other types of applications. We’ve been working on creating a deployment “recipe” to work with symfony applications to make our job a lot easier.

Prerequisites

  • Must have SSH access to the server you are deploying to.
  • Must have Ruby and RubyGems installed on your machine (not required for deployment server)’

Installing Capistrano

sudo gem install capistrano

Setup your project to use Capistrano

cd path/to/your/app
capify .

This will create a few files for you. It will create a Capfile in the root of your project. It will also create a directory named config (if it doesn’t exist already) and place a file named deploy.rb in the config folder. That’s where you will add/change any application-specific settings for your deployment recipe.

Here are the required settings (just replace with your app-specific variables):

Customize Capistrano for symfony

Now, to get capistrano to play nice with symfony, we have to override some of the built-in functions. You’ll want to add this to your deploy.rb file:

Now, you can start the deployment process! To get your server setup with the file structure that Capistrano expects, you can run

cap deploy:setup

This command will create the following folder structure on your server:

|-- deploy_to_path
  |-- current (symlink)
  |-- releases
    |-- 20100512131539
    |-- 20100509150741
    |-- 20100509145325
  |-- shared
    |-- cached-copy
    |-- log
    |-- pids
    |-- system

The folders in the releases directory will be the actual deployed code, timestamped. The pids folder in the shared directory is only used for Rails applications, so you can ignore it completely. Capistrano symlinks your log directory from your app to the log directory in the shared folder so that it doesn’t get erased when you deploy a new version of your code.

This is an important step! Before you deploy the application for the first time, it’s really a good idea to scp your databases.yml file up to your server so you can set the proper credentials for your production server and keep it out of your repository. Make sure you put the databases.yml file into the shared/system directory, because we already have a task that will symlink it when each new code version is deployed.

Now, to deploy your application for the first time, you can run:

cap deploy:cold

This will deploy your application, create the db, models, forms, filters, and run all of your migrations.

Now, whenever you need to deploy a new version of your code, you can just run:

cap deploy

If you need to deploy and run your migrations you can call:

cap deploy:migrations

We’ve also added a custom task to run your test suite on the production server. You can invoke this by calling:

cap deploy:testall

This will deploy the application, rebuild the test database, then run all of the tests.

If you want to see all of the Capistrano tasks available, you can run:

cap -T

We’ve been using this setup for a little while now, and it’s saved us a ton of time when we need to push changes for a site to the production server.

Share This
  • http://everzet.com everzet

    Hi. I’ve updated your scripts in my repo. Take a look: http://github.com/everzet/capifony

  • http://everzet.com everzet

    Hi. I've updated your scripts in my repo. Take a look: http://github.com/everzet/capifony

  • http://travisonrails.com Travis Roberts

    @everzet Nice! I’ll definitely give your gem a try. Good work!

  • http://twitter.com/travisroberts Travis Roberts

    @everzet Nice! I'll definitely give your gem a try. Good work!

  • http://twitter.com/b00giZm Pascal / P.Stylez

    Could you explain why you call 'symfony doctrine:build –all –and-load –no-confirmation' in the deploy:start task? Doesn't the '–and-load' flag clear the DB and throws in some fresh fixtures? If so, this does not seem to be a good idea for a production DB ;)

    • http://twitter.com/travisroberts Travis Roberts

      Good catch! I've removed the –and-load argument from the start task.

  • http://twitter.com/b00giZm Pascal Cremer

    Could you explain why you call ‘symfony doctrine:build –all –and-load –no-confirmation’ in the deploy:start task? Doesn’t the ‘–and-load’ flag clear the DB and throws in some fresh fixtures? If so, this does not seem to be a good idea for a production DB ;)

    • http://travisonrails.com Travis Roberts

      Good catch! I’ve removed the –and-load argument from the start task.

  • Trashcanmail

    Hello,

    When the capistrano script log to my distant server via ssh its current durectory is not the application directory it self.
    So when it try to make a
    cap deploy:cold
    especialy when it entered in the cache-clear part,
    it’s just like if i do:

    php5 /a_dir/another_dir/yet_another_one/htdocs/releases/20110710064203/symfony cache:clear
    So I get this error:
    You must be in a symfony project directory.

    Any idea how to fix that pb?

    Thx by advance!

  • Trashcanmail

    Hello,

    When the capistrano script log to my distant server via ssh its current durectory is not the application directory it self.
    So when it try to make a
    cap deploy:cold
    especialy when it entered in the cache-clear part,
    it’s just like if i do:

    php5 /a_dir/another_dir/yet_another_one/htdocs/releases/20110710064203/symfony cache:clear
    So I get this error:
    You must be in a symfony project directory.

    Any idea how to fix that pb?

    Thx by advance!