This guide explains the way to setup a production server using Capistrano.
- Capistrano is a development gem which assist the developer to run commands on the production server (something like a Heroku toolbelt)
- Hence, it is installed and configured on developer's computer
# Gemfile
# Use Capistrano for deployment
gem 'capistrano', '~> 3.0.1'
gem 'capistrano-rails', '~> 1.1.0'
gem 'capistrano-rvm', '~> 0.1.0'
gem 'capistrano-bundler'
- Run
bundle install
- Run
cap install STAGES=staging,production
- Edit the file
Capfile
# Capfile
# Make sure the below is uncommented
require 'capistrano/rvm'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
- Edit the file
config/deploy/staging.rb
with your own user, domain and application:
set :stage, :staging
role :app, %w{localhost}
role :web, %w{localhost}
role :db, %w{localhost}
set :application, 'depot'
set :repo_url, 'ssh://deploy@localhost:2222/~/git/depot.git'
set :branch, 'master'
set :deploy_to, '/var/www/stapi/staging'
set :ssh_options, {
forward_agent: false,
user: 'root',
port: 2222
}
- This assumes your local is already git initizated (Why shouldn't you!??)
- After you have capify and edited all the Capistrano config, you should push it to the server:
git remote add staging ssh://user@host/~/git/depot.git
git push staging master
cap staging deploy:check # make sure everything is okay
# then finally...
cap staging deploy
# or for production
cap production deploy
# Add this in config/deploy.rb
# and run 'cap production deploy:seed' to seed your database
desc 'Runs rake db:seed'
task :seed => [:set_rails_env] do
on primary fetch(:migration_role) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :rake, "db:seed"
end
end
end
end
# This is found in /config/deploy.rb
# This works on apache/passenger
# To run it, use cap production deploy:restart
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
# Your restart mechanism here, for example:
execute :touch, release_path.join('tmp/restart.txt')
end
end
end
- A fun task that works only on Mac to say a message when deployment is completed
# /config/deploy.rb
namespace :deploy do
desc 'Says a message when deployment is completed'
task :say do
system("\\say Capistrano Deployment Completed! Good Job!")
end
end
after :finished, 'deploy:say'
- Nokogiri gem installation error: run
yum install libxml2 libxml2-devel libxslt libxslt-devel