Skip to content

setup production development

LuoKevin edited this page Mar 25, 2018 · 18 revisions

Linode 主機安裝與 Capistrano

Ubuntu 12.04 + Ruby 2.0.0 with RVM deployment

Update and upgrade system

  • sudo apt-get update
  • sudo apt-get upgrade

Install mysql

  • sudo apt-get install mysql-server
  • sudo apt-get install libmysqlclient-dev

Changing the Time Zone

sudo dpkg-reconfigure tzdata

Install Build Tools and Library for Ruby 2.0.0

sudo apt-get install build-essential zlib1g-dev libssl-dev libreadline5 libyaml-dev

Create user: apps

sudo adduser apps
sudo passwd apps
sudo vim /etc/sudoers

make apps have sudo power

switch to apps

sudo su apps

Install ruby

curl -L | bash -s stable --ruby=2.0.0
source /home/apps/.rvm/scripts/rvm

Install Bundler

gem install bundler

Install libcurl4-openssl-dev for passenger

sudo apt-get install libcurl4-openssl-dev

Install XML parser

sudo apt-get install libxml2 libxml2-dev libxslt1-dev


gem install nokogiri

Install node.js

sudo apt-get install nodejs


apt-get install python g++ make
mkdir ~/nodejs && cd $_
wget -N
tar xzvf node-latest.tar.gz && cd `ls -rd node-v*`
make install

Install passenger

gem install passenger

Install nginx

複製底下的 output

which passenger-install-nginx-module

然後把 output 用 rvmsudo 執行

rvmsudo OUTPUT
# Choose "download, compile, and install Nginx for me"
# Accept defaults for any other questions it asks you

Install imagemagick

  • sudo apt-get remove imagemagick

  • sudo apt-get install libmagickcore-dev libmagickwand-dev

  • 下載 ImageMaigck

  • ./configure

  • make

  • sudo make install

然後 vim .bash_profile 加入


Install rmagick

gem install rmagick

Install git

sudo apt-get install git

setup a script to control Nginx

git clone git://
sudo mv rails-nginx-passenger-ubuntu/nginx/nginx /etc/init.d/nginx
sudo chown root:root /etc/init.d/nginx
sudo /etc/init.d/nginx restart

set up nginx.conf

vi /opt/nginx/conf/nginx.conf

insert following codes into the /opt/nginx/conf/nginx.conf

server {
      listen 80;
      root /home/apps/my_project/current/public;
      passenger_enabled on;

create folder for the project

sudo su apps
cd ~
mkdir ~/my_project

set up deploy key for apps

more ~/.ssh/
pbcopy < ~/.ssh/

Add the SSH key to GitHub

  • browse
  • click "Admin"
  • click "Deploy keys"
  • click "Add deploy key"
  • input "Title"
  • Paste the key into the "Key" field
  • Click "Add key"
  • Confirm the action by entering your GitHub password

Authenticate Github

ssh -T

Set up authorized keys for developers

vim /home/apps/.ssh/authorized_keys

Paste the ssh public keys into the "/home/apps/.ssh/authorized_keys" file


Set up Capistrano

vi /home/apps/my_project/shared/config/database.yml

  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: my_project
  pool: 5
  username: root
  password: '1xsedqwdqd'

create "/my_project/Capfile"

Paste following codes into the "/my_project/Capfile" file

load 'deploy/assets'
Dir['vendor/gems/*/recipes/*.rb','vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
load 'config/deploy' # remove this line to skip loading any of the default tasks

create "/config/deploy.rb"

Paste following codes into the "/config/deploy.rb" file

# -*- encoding : utf-8 -*-
require 'capistrano/ext/multistage'
require 'bundler/capistrano' #Using bundler with Capistrano
require 'cape'

set :stages, %w(staging production)
set :default_stage, "production"

Cape do
  mirror_rake_tasks :dev

create /config/deploy/production.rb

Paste following codes into the "/config/deploy/production.rb" file

# -*- encoding : utf-8 -*-
require 'rvm-capistrano'
default_environment["PATH"] = "/opt/ruby/bin:/usr/local/bin:/usr/bin:/bin"

set :application, "my_project"
set :repository,  ""
set :deploy_to, "/home/apps/my_project"

set :branch, "master"
set :scm, :git

set :user, "apps"
set :group, "apps"

set :deploy_to, "/home/apps/#{application}"
set :runner, "apps"
set :deploy_via, :remote_cache
set :git_shallow_clone, 1

set :use_sudo, false
set :rvm_ruby_string, '2.0.0'

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

set :deploy_env, "production"
set :rails_env, "production"
set :scm_verbose, true
set :use_sudo, false

namespace :deploy do

  desc "Restart passenger process"
  task :restart, :roles => [:web], :except => { :no_release => true } do
    run "touch #{current_path}/tmp/restart.txt"

namespace :my_tasks do
  task :symlink, :roles => [:web] do
    run "mkdir -p #{deploy_to}/shared/log"
    run "mkdir -p #{deploy_to}/shared/pids"
    symlink_hash = {
      "#{shared_path}/config/database.yml"   => "#{release_path}/config/database.yml",

    symlink_hash.each do |source, target|
      run "ln -sf #{source} #{target}"


namespace :remote_rake do
  desc "Run a task on remote servers, ex: cap staging rake:invoke task=cache:clear"
  task :invoke do
    run "cd #{deploy_to}/current; RAILS_ENV=#{rails_env} bundle exec rake #{ENV['task']}"

after "deploy:finalize_update", "my_tasks:symlink"

add 'rvm-capistrano' to Gemfile Paste following codes into the "/Gemfile" file, and bundle install

  gem "rvm-capistrano"

  group :development do
    gem "capistrano", "2.15.4"
    gem "capistrano-ext"
    gem 'capistrano-unicorn', :require => false
    gem "cape"

initial deployment cap deploy:setup

deploy cap deploy

remote db migrate cap deploy:migrate

You can’t perform that action at this time.