Capistrano Deployment in Rails 3 (via the rvm-capistrano gem)


  • Go to your project path.
  • Add gem 'rvm-capistrano' to your Gemfile
  • Run bundle install for installing the gem
  • Run the command capify . This will create
    • [add] writing './Capfile'
    • [add] writing './config/deploy.rb'
    • [done] capified!
  • Modify the setting in your ./config/deploy.rb, sample settings is provided below 
  • 
    require "rvm/capistrano"
    set :application, "My Test App"            # Your application name
    set :domain, "testapp.com"                 # Domain name for your app
    set :repository,  "git@github.com:test/test.git" # The code repository url
    set :user, "test_user"           # The ssh user that has access to your server
    default_run_options[:pty] = true
    set :use_sudo, false
    
    set :scm, :git
    set :deploy_via, :remote_cache
    set :deploy_to, "/home/test/test_project_production"  # Server path to which the code is to be deployed
    
    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
    
    # Add RVM's lib directory to the load path.
    $:.unshift(File.expand_path('./lib', ENV['rvm_path']))
    
    # Load RVM's capistrano plugin.    
    set :rvm_ruby_string, '1.9.2'          # The ruby version in your server             
    set :rvm_type, :user                   # Don't use system-wide RVM
    
    # Various task that needs to be executed during the deployment process.
    namespace :deploy do
      task :start do 
        run "sudo /etc/init.d/nginx start" 
      end
      task :stop do 
        run "sudo /etc/init.d/nginx stop"    
      end
      task :restart, :roles => :app, :except => { :no_release => true } do
        run "sudo /etc/init.d/nginx restart"    
      end
      
      desc "Copies database credentials"
      task :copy_db_credentials do
        run "cp #{shared_path}/credentials/database.yml #{release_path}/config/database.yml"
      end
      
      desc "install the necessary prerequisites"
      task :bundle_install do
        run "cd #{release_path} && bundle install"
      end
    
    end
    
    before "deploy:assets:precompile", "deploy:bundle_install"
    before "deploy:assets:precompile", "deploy:copy_db_credentials"
    # after "deploy:update_code", "deploy:bundle_install"
    
    after "deploy", "deploy:migrate", "deploy:cleanup"
    
    
    
    In your ./Capfile uncomment the line load 'deploy/assets'

    Now you need to do some configuration in your server:
    • Login into the server with root access
    • For allowing test_user to run sudo commands without password we need to do the following changes
      • Run command: visudo
      • Add the following line test_user ALL= NOPASSWD: /etc/init.d/* after 
        • ## Allow root to run any commands anywhere
        • root    ALL=(ALL)       ALL
      • save and exit the file 
    Now from your local machine project path run the following commands
    • cap deploy:setup
    • cap deploy:check
    • cap deploy
    Note: You may get error saying "cannot create releases folder" in server
    • Login to your server go to your project path /home/test/test_project_production you will be seeing a "shared" folder  create another folder "releases" 
    • Go to "shared" folder create directories "system", "log", "credentials"
    • In credentials folder add your database.yml file with DB settings.

    Thats it!!.... Again Run the cap deploy . Your code will be successfully deployed to the server. :)

    Helpful links are: Deploying with Capistrano    Capistrano Tasks

Comments

Popular posts from this blog

How to SSH to remote server without entering password each time.

How to redirect a URL in NGINX

Ruby on Rails Installation on Windows 7