Automated PHP Deployment With Capistrano
Sure, I’d like to spend my days and nights writing Ruby and cap deploy ing it to success, but I have to face the facts.
I Still Have PHP Apps To Maintain.
I have no one to blame but myself. I wrote them. And I still work where they were written. There’s no escape. After using Capistrano more than one time, you’ll be completely spoiled.
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.
Without going into all the details of Capistrano (following the link will give you a nice overview), it uses a deploy script (recipe) to understand your deployment environment and required tasks.
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.
First, we need a Capistrano recipe. Currently, it can only be auto generated for a Rails app, so psst, here it is. Download this and create a directory at the root of your PHP app and name it config.Capistrano wants its recipe to be in the _config_ directory, so do as it likes. Now lets open it up and make it work with us. Once again, a complete description of the Capistrano recipe file is bit out of scope for this little post, so please "read up":http://manuals.rubyonrails.com/read/book/17. We'll edit all the normal items
We only deploy to one server, so edit…
Capistrano defaults to checkout when pulling from subversion. I prefer an export, if you do too, then add…
If you look on your deployment server, you should see 2 directories that were just created.
Inside the block we’ll just tell Capistrano exactly what it needs to do in order to deploy our app.
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…
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!