Youtilize

Read all about technology, web development and creative entrepreneurship

Deploying with Capistrano on Textdrive

Update: Instructions have been updated for Capistrano 2.0. More info can be found here and here.

Capistrano is a small RubyGem that is designed to run a number of scripts on a remote server and is used primarily to deploy applications. Think of it as an automated way to grab the latest revision of your application from Subversion, upload it to your domain and then restart the server to take into account the changes.

Assumptions

First of all, I’m assuming that you’ve already been able to setup your Ruby on Rails application on TextDrive. In other words, you’ve already configured and ran Lighttpd as well as Ruby. We will need to edit those configuration files, primarily those located in your /etc/lighttpd directory later on. If not, follow these directions.

I’m also assuming that your app is installed on a secondary domain (not primary domain of shared account) and is located in the following directory: /users/home/USER/domains/DOMAIN.tld/web. You can easily adjust the following walkthrough for a primary domain setup by adjusting the paths.

On top of it all, you must already have Subversion repository setup on your server and have committed at least one time to it. I realize this is also a pretty complex thing to setup, especially for someone new, so expect a post in the near future on it. For now, use the following directions.

Installing Capistrano

On your local machine, open up Terminal (I’m on a Mac) and install Capistrano by executing the following line:

gem install capistrano

This is the easy part and you should now be able to use the cap command in console.

Still with me so far? At this point, you’ve installed Capistrano and should now be ready to set it up.

Deployment Recipe

Capistrano uses a file called ‘Recipe’ to deploy your application. It provides it with directions to your server, Subversion repository and your main account username. Main account has access to all the repositories, so it’s easier to use.

Rails creates a default recipe for you in a file called deploy.rb in the /config directory. Open it up in your favorite editor and replace everything with the following recipe:

set :user, "" # Your main account username
set :domain, "" # Your secondary domain name. For ex: myname.com
set :txd_server, "" # The TextDrive server you're currently located on. For ex: nicola.textdrive.com
set :subversion_repo_name, "" # Your Subversion repo name. For ex: repos
set :application, "" # The name of your application (ie. youtilize)

set :repository, "svn+ssh://#{user}@#{txd_server}/users/home/#{user}/domains/#{domain}/svn/#{subversion_repo_name}/trunk" # Edit this path if you're installing on primary domain or your SVN repo is located elsewhere
set :deploy_to, "/users/home/#{user}/domains/#{domain}/web" # This is where files will be uploaded to

role :app, "#{user}@#{txd_server}" 
role :web, "#{user}@#{txd_server}" 
role :db, "#{user}@#{txd_server}" 

namespace :deploy do
  desc "Restart Lighttpd and Ruby" 
  task :restart, :roles => :app do
    run "pkill lighttpd; pkill ruby" # Kill processes
    run "nohup /users/home/#{user}/etc/rc.d/lighttpd.sh start; nohup /users/home/#{user}/etc/rc.d/rails.sh" # Start processes
  end
end

Go ahead and follow directions in the file to fill in the blanks with all your information.

Capistrano 2.0 doesn’t automatically use the Rail’s recipe we just setup, so run the following to put finishing touches on the recipe setup:

capify .

At this point you’ve given Capistrano a recipe to follow when deploying your application.

Setting up your directories

Capistrano now needs to install default directories and chmod them accordingly to them give proper permissions. Aren’t you happy you don’t have to deal with permissions?

To do this, delete everything in your /web directory of your secondary domain.

cd /users/home/USER/domains/DOMAIN.tld/web/
rm -rf .

It’ll give you an error about not being able to delete .textdrive file. That’s fine, leave it be.

What you’ve done now is effectively wiped out your whole application! Don’t worry, in a couple of minutes, Capistrano will put it back for you. It first needs to install a couple of directories of its own, so execute the following:

cap deploy:setup

This installs two directories in your /web directory: /web/shared, and /web/releases. It also installs a system link in /web/current that will point to the latest release of your application. So, your actual application will now live in /web/current NOT /web where your server is currently pointing to. So we need to adjust that:

Reconfiguring your Lighttpd

Grab both lighttpd.conf from /users/home/USER/etc/lighttpd and your APPNAME.conf from /users/home/USER/etc/lighttpd/vhosts.d (Rememeber, APPNAME is whatever you named your .conf file before). You can do this step via FTP.

In ligghtpd.conf, change:

server.document-root = base + "/domains/DOMAIN.tld/web/public/"

to:

server.document-root = base + "/domains/DOMAIN.tld/web/current/public/"

Next, in APPNAME.conf, change:

server.document-root = base + "/domains/DOMAIN.tld/web/public/"

to:

server.document-root = base + "/domains/DOMAIN.tld/web/current/public/"

Don’t forget to change DOMAIN.tld to your domain name. Re-upload the files to their proper directories and guess what:

You’re all set to deploy!

Ready, Set, Deploy!

To deploy your application, on your local machine execute the following in you app directory:

cap deploy

You will be asked for your server password, so type that in (probably multiple times) and watch the process. At the end, Capistrano will restart the server for you and you should be able to load your site in the browser. In case the server doesn’t start, start it manually first time around.

Don’t forget to commit your recent changes to the Subversion repository before deploying:

svn commit -m="Bug fixes" 
cap deploy

Best of luck and hope this saved you many grueling hours.

12 Comments

Joel
about 1 year ago

good read, though can you allow the code sections to scroll within it’s block (overflow: auto; ?) rather than show outside its block?

bbnnt
about 1 year ago

well joel, you’re picky.

David Jones
about 1 year ago

I’m using my primary domain and started to follow this tutorial but how do you get around the undeletable ”.textdrive” located in ~/web/public/.textdrive

You can’t checkout your app if that directory already exists…

Dimitry
about 1 year ago

David: Take a look at the following thread. Apparently .textdrive is there on purpose so that /public/ could not be removed for server integrity purposes.

You can however simply rename the folder and create your own /public/ dictory. First do ‘mv public public_old’ and then ‘mkdir public’.

More here: http://forum.textdrive.com/viewtopic.php?id=12550

Another solution is to just use a different directory for your site and link to that. Mine is under /web/current/ instead of /web/public/

Hope this helps!

Richard Crowley
about 1 year ago

I will be using this – thanks, Dimitry. I read Coda Hale’s post a while back about his Rails setup, and I must admit it was far over my head. This is a much better intro and I will likely be taking my site down soon to put it back together properly.

David Jones
about 1 year ago

A couple of notes:

“In ligghtpd.conf, change:” – obvious typo there :-)

I had to put “ssh_options[:paranoid] = false” in my deploy.rb file because there was an error about a fingerprint not matching.

Apart for that, I went through this and everything worked fine :-)

I also used ~/web/current/ as my primary site folder instead of ~/web/public/ like you suggest in the comment above.

Thanks Dimitry!

Patrick Berkeley
about 1 year ago

@David Jones: I was having the exact same problem. your solution worked. Thanks!

@Dmititry: Thanks a lot for the article. It’s been helpful. A few other things seem to have changed.

1. ‘rake remote:exec ACTION=setup’ doesn’t work. ‘rake remote:setup’ does.

2. ‘rake remote:setup’ does not create web/current. So, there is no need to reconfigure Lighttpd.

Also when I run ‘cap deploy’ I get the error ’`latest_revision’: Could not determine latest revision (RuntimeError)’ I think this may have to do with the fact that TextDrive does not allow you to access the same repository from both http and svn+ssh

Any suggestions are greatly appreciated.

Patrick Berkeley
about 1 year ago

Actually it’s ‘cap setup’ now instead of ‘rake remote:exec ACTION=setup’.

Patrick Berkeley
about 1 year ago

Got it working now.

Capistrano creates an alias of the current version. The alias is called ‘current’. So, you still need to reconfigure Lighttpd as mentioned in this article. In rails.sh you also need to edit the path for the secondary domain the same way you edit the lighttpd files.

Also, it was necessary to add ‘puts repository’ one the line after ‘set :repository’. According to Jamis Buck this is to “make sure it is getting set correctly.”

Dimitry
about 1 year ago

Patrick: I’m glad you figured it out. Thanks for the info :) I’ll be sure to update the post accordingly.

Mary Cook
8 months ago

Hiya. Thanks for the very useful tutorial.

Just in case anyone else had the same problem, I had to alter the /etc/rc.d/rails.sh file so that the path to dispatch.fcgi was correct: /users/home/$USER/web/current/public/dispatch.fcgi

Mary

tedster
7 months ago

TxTdrive seems to recommend mongrel for smaller noobie apps like the one I am working on.

Any directions from the wizards out there on mongrel?

Leave your thoughts

Name
Email
  – kept private
Website
  – optional
Human check
  – type in "youtilize"