Automated PHP Deployment With Capistrano

I Still Have PHP Apps To Maintain.

But Capistrano is only for crazy Rails apps right? Nope. Its simply a platform for automation. It just so happens that deployment usually requires quite a bit of steps. Hmmm...lots of steps...automation... a match made in heaven. So sure, Capistrano was thought up with Rails in mind, but created openly so you're able to use it any way you'd like.

Capistrano's power is in its remoting features. You tell it what to do on that other server, and it does it. Beauty. So, if your PHP deployment process requires an svn export combined with a few other repetitive steps you have to do every time, Capistrano is here to help you too.

Ok ok ok, Lets Get On With It

So, we have a PHP app that we need to deploy to server X. It needs to be sucked out of its repository, its version symlinked to a doc_root, and then a shared directory symlinked into the site. This is a pretty common deployment scenario, so lets go with this.

We'll edit all the normal items

set :application, "php_app"  set :repository, "{application}/trunk"

We only deploy to one server, so edit...

role :web, ""

Now you need to add the directory you want to deploy to. Add..

Capistrano defaults to checkout when pulling from subversion. I prefer an export, if you do too, then add..

set :checkout, "export"

Ok, so if you're familiar with Capistrano, you know exactly what we did. If you're not, the most important thing to know is that Capistrano is going to suck your code from the repository defined, do everything it's told to do in /home/www/php_app on your deployment server,

Set'r Up

Believe it or not, we're half way done. Next, lets let Capistrano get the directory we just defined ready for its magic.

  cap setup  

If you look on your deployment server, you should see 2 directories that were just created.

  drwxrwxr-x    2 user  group      4096 Aug 16 01:48 releases  drwxr-sr-x    4 user  group      4096 Aug 16 01:48 shared  

WOW! We're already saving time! releases is where your app will be sucked down to. Capistrano exports your PHP app into a directory named after the current date/time. Each version you deploy will be kept in this directory. Shared is where you can keep files like uploaded assets or images that aren't kept in your repository, but need to be around every time you export from subversion. If your users upload files all day long, and you redeploy your PHP app from subversion, all your uploaded files will be gone. So we keep those in here.

Here's the deployment rub. Every time you deploy, you have to go symlink these directories from your /shared directory, into your current version of your PHP app. And a small bit of math can calculate the complexity when you start adding 2,3, or 6 asset folders. Yuck.

That's where Capistrano helps ya out.


Now, all ya gotta do to redeploy your site is...

  cap deploy  

Capistrano will go on to do everything you told it to do, on your deployment server. When its done, your directory should look like this...

  lrwxrwxrwx   1 user group   42 2006-08-16 01:32 current -> /home/www/php_site_/releases/200608162012 
 drwxrwxr-x   4 user group 4096 2006-08-16 01:31 releases  drwxr-xr-x   4 user group 4096 2006-06-24 02:08 shared  

And there you have it. Automated PHP deployment, with one simple command. Peering inside the current directory, you'll see where the photos directory has been symlinked into it.

The moral is, Capistrano isn't just for Rails deployment. Its powerful remote execution can be exploited to do anything you want. While our Rails apps get a lot for free when it comes to recipes, creating your own isn't complicated. Especially if its replacing repetitive tasks you've done over and over and over and over and over again.

You'll see the payoff the minute you get this set up. Especially on a Friday night at 5:30p when you're walking out the door. And its not just for Rails!



Other useful things

Icon organizer can help you to arrange your Desktop icons
The advantages of the Internet
Movie organizing software
Mac software programs
Programming C, C++, perl, PHP, java and javascript
Finding Mac Software As Well As Popular Mac Software
Php and Mysql development
Introduction to PHP
Php and Mysql Programming Security
MySQL – most popular open source database in the planet
Why use PHP for dynamic web development