假設 bitnami Rails stack 安裝好在 ec2

remote ec2 server

mkdir ~/apps/myapp
cd ~/apps/myapp
git init --bare

local machine

~/.ssh/config
Host host_name
HostName ip_address_or_domain
User bitnami    # bitnami by default
IdentityFile /path/to/key.pem

make sure you can ssh host_name connect to your remote server. ssh-add key.pem if neccessary.

rails new myapp
cd myapp
in <code>Gemfile</code> add <code>gem 'capistrano'</code> then <code>bundle install</code>

git init .
git add .
git commit -m 'initial commit'

Install capistrano, then in your local application dir
capify .
Then
# in your config/deploy.rb
require 'bundler/capistrano'

set :application, "app_name"

set :user, 'bitnami'                           # ssh login user
set :domain, 'ip'                              # domain or ip 
set :deploy_to_dir, "/home/bitnami/apps/myapp"
set :local_repository,  "/path/of/your/local/repository"
set :repository,  "/path/to/remote/repository" # should be /home/bitnami/apps/myapp in this tutorial
ssh_options[:keys] = ["/path/to/key.pem"]      # used to connect to remote server   
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"

one more thing:
git remote add ec2 host_name:/home/bitnami/apps/myapp

ok, let's deploy,
git push ec2 master # dont forget push first
cap deploy

Gone for this far, theoretically it should be easy for the rest. However, by my experiences, it's still highly possible to get stucked in any step by any kind of errors.

A command problem might be the shell environment. On one linux machine, there might be different shell environments, sh, bash, cth, ..etc, and different shell environments usually corresponding to different rvm/ruby/rails paths. To solve this problem, one may want to decide the shell environment you want to use in deployment.

By default, capistrano use sh. If you don't want it be your choice, set false in your deploy.rb, ref here:
default_run_options[:shell] = false
or you can specify your favorite, ref here :
default_run_options[:shell] = '/bin/bash' # I haven't test this yet

Once into your favorite shell, there might still be different rvm loading path. Try this, it works for me, ref here:
set :bundle_cmd, 'source $HOME/.bash_profile && bundle'

good ref: here
this maybe: here, here

Comments

comments powered by Disqus