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

Published on June 15, 2012.

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, "git@github.com:username/appname.git"

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, "xx.xxx.xx.xxx" # Your HTTP server, Apache/etc
role :app, "xx.xxx.xx.xxx" # This may be the same as your `Web` server
role :db, "xx.xxx.xx.xxx", :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')}"
   end
 end

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

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.

Now visit your server ip address and you should see a working version of your rails application. This tutorial just covered the basics but there will be more to keeping your server secure and managing it. If you’re looking for more resources beyond this tutorial check out railscasts or peepcode.

© 2017