Setting up a production server with ruby on rails (Deployment included) Part 3

Welcome to part 3 of setting up a production server with rails 3. In part 1 of this series we installed rvm, ruby, rails and setup a firewall. In part 2 of this series we installed mysql/postgresql, apache, and passenger. In part 3 of this tutorial we will install capistrano so we can deploy our application to our server.

What you will need

Make sure you have a git repository setup for your rails application so that you can deploy your app

Installing Capistrano

Now that our server is setup we can install capistrano. Logout of your server in go inside your local application folder. Once inside run gem install capistrano

After you have installed the capistrano gem lets capify our rails project. This will create a couple of files for us but the one that we're most interested in is the deploy.rb file. capify .

Open the deploy.rb file and add in your server and git information.

set :application, "appname"
set :repository, ""

set :scm, 'git'

set :user, 'usernameyousetupforserver'
set :branch, "master"

set :git_shallow_clone, 1
set :use_sudo, false
set :deploy_to, "/path/public/#{application}"
set :deloy_via, :remote_cache
set :keep_releases, 1
set :rails_env, "production"
set :migrate_target, :latest

default_run_options[:pty] = true
ssh_options[:forward_agent] = true

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

 namespace :deploy do
   task :start do ; end
   task :stop do ; end
   task :restart, :roles => :app, :except => { :no_release => true } do
     run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"

 namespace :deploy do
   desc "Recreate symlink"
   task :resymlink, :roles => :app do
     run "rm -f #{current_path} && ln -s #{release_path} #{current_path}"

After you have setup your information above run the following

cap deploy:setup
cap deploy
cap deploy:check

The above will setup the necessary files then deploy your application and finally check to see if you've encountered any errors while deploying.

nginx, capistrano, cloud, deployment, ruby on rails, ubuntu, linux,