Deploying Zend Framework applications using Capistrano

Are you tired of deploying manually your zf projects to production servers, do you want to automate this tedious and error prone process, do you want to be able to revert changes if you make some mistakes? If the answer to these questions is yes, this is the post for you.

Recently I started to exploring Ruby on Rails and one of the aspects I like of it is the ability to deploy projects using Capistrano an automated deployment tool. With Capistrano your deployment process (once configured the app) is simple as typing cap deploy, and if you make a mistake you can revert the things as before by just typing cap deploy:rollback, no more ssh, no more (s)ftp, nothing, just a command.

I tried to replicate this behavior also for Zend Framework projects and what I obtained is a brand new gem, named zend-framework-deploy. With this gem you can deploy ZF projects in the same manner of Rails ones. I developed a gem, because Ruby (and rails) are more extensible than php and I want to reuse existing capistrano code.

Installing the gem

So to use this technique you’ll need to install ruby and RubyGems. If you never did that, don’t worry it’s pretty simple with the right tools, just use Ruby Version Manager on any Unix platform. If you’re on Windows platform (aargh…) you can use Ruby Installer.

Once you have the gem command available in your shell type this command to install my gem

gem install zend-framework-deploy

and it will take care of anything, path setup, dependencies, etc.

Prepare the project

Now you can enable your project to be used with capistrano, to do that just use the bundled zf-capify executable provided

zf-capify /your/project/root
[add] writing '/your/project/root/application/configs/deploy.rb'
[add] writing '/your/project/root/Capfile'
[done] zf-capified!

The executable writes two files with the minimal settings to deploy your project, you can safely ignore the Capfile and just edit the deploy.rb file. It contains the capistrano variables to configure parameters for your deploy. This is the content of the generated file

set :application, "set your application name here"
set :repository,  "set your repository location here"
set :deploy_to, "set path where to deploy application"
set :zf_path, "set Zend Framework (remote) installation path here"

set :scm, :subversion
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`

role :web, "your web-server here"                          # Your HTTP server, Apache/etc

Just fill the strings with your parameters and you’re almost ready to make your first deploy, these are the meaning of various settings:

  • application: the name of your app, try to avoid spaces in that
  • repository: url of your repository
  • deploy_to: base path on the remote server which will contain the application
  • zf_path: remote path of Zend Framework release
  • scm: source code management type (git, subversion, etc)
  • role web: fill this with the dns name of your server

To test your configuration you should type this command in the root of your project

cap deploy:setup

It will try so ssh into your server and it will make the required directories for project deployment. If everything goes fine, next step is to make a deploy, you can do that by typing

cap deploy

This command will create a new folder under the releases directory in the deploy_to folder and it will checkout the content of your SCM head into. Then it will symlink the data folder in the project root and lastly it will symlink Zend Framework directories in the library folder. At this point you should have a working version of your zend framework application (if the exit code of the command is 0), to make it run point your webserver root to the deploy_to/current/public folder and you’re ok. (Please note that you still need to setup your database manually). Refer to the capistrano documentation for the full setup workflow and the structure of the deployment paths.

Following deployments

Once the setup is done, releasing a new version of your app is trivial, just cd (locally) in the root of the project and type cap deploy. The released project is automatically updated at the latest version present in your SCM. And if something goes wrong you can safely revert it to the previous state with cap deploy:rollback

I will explain the multistage configuration in a future article. Please post any problem in the comments and I’ll try to help you on solving that.

You can leave a response, or trackback from your own site.
Subscribe to RSS Feed