Nginx + Thin are greate, but auto deployment makes them even awesome! Here's HOW-TOs by using capistrano:

First assumed that you've set up a Nginx-Thin server, if you don't, please read this post.

Typically, there're few things you will always do after cap deploy:

  1. Bundle install: This is most important, otherwise your app might not work at all. This part is included in this post

  2. Copy config files: database.yml, thin_config.yml, some scripts... and so on.

  3. Precompile assets: This is of course you must do for performance, I think maybe it could be done before deploy but anyway I do this after cap deploy.

  4. Migration

  5. Restart thin servers and nginx server.

And we're going to make these things done automatically. Here's a sample configuration for deploy.rb

require "bundler/capistrano"

set :application, "iSee"

set :user, 'dayen'                           # ssh login user
set :domain, ''                              # domain or ip
set :deploy_to_dir, "/home/dayen/work/rails/iSee"
set :local_repository,  "/Users/tpy/panorama/iSee"
set :repository,  "/home/dayen/work/rails/iSee" # should be /home/bitnami/apps/myapp in this tutorial
set :use_sudo, false                           # if turn on, all capistrano maked dir would be of root

set :branch, 'master'
set :scm, "git"

role :web, domain                          # Your HTTP server, Apache/etc
role :app, domain                          # This may be the same as your `Web` server
role :db,  domain, :primary => true        # This is where Rails migrations will run

set :deploy_to, deploy_to_dir
set :deploy_via, :export

default_run_options[:pty] = true

set :git_shallow_clone, 1
set :scm_verbose, true

after "deploy:restart", "deploy:cleanup"

set :bundle_cmd, 'source $HOME/.bash_profile && bundle'
default_run_options[:shell] = false
# default_run_options[:shell] = '~/bash'

#================== tasks ====================

namespace :deploy do

  task :copy_config_files, :roles => [:app] do
    db_config = "#{path}/database.yml"
    thin_config = "#{path}/thin_config.yml"
    run "cp #{db_config} #{release_path}/config/database.yml"
    run "cp #{thin_config} #{release_path}/"

  task :precompile do
    run "cd #{release_path}; source $HOME/.bash_profile && bundle exec rake assets:precompile RAILS_ENV=production"

  task :migration do
    run "cd #{release_path}; source $HOME/.bash_profile && bundle exec rake db:migrate RAILS_ENV=production"

  task :config_nginx do
    pre = File.basename(previous_release)
    cur = File.basename(release_path)
    run "#{sudo} sed 's/#{pre}/#{cur}/g' /etc/nginx/sites-available/default"

  task :restart_thin_server do
    run "cd #{previous_release}; source $HOME/.bash_profile && thin stop -C thin_config.yml"
    run "cd #{release_path}; source $HOME/.bash_profile && thin start -C thin_config.yml"

  task :restart_nginx do
    run "#{sudo} service nginx restart"


after "deploy:finalize_update", # after this step, excute the following task


comments powered by Disqus