LoginSignup
8
7

More than 5 years have passed since last update.

(11)Capistranoでdeploy #2|EC2でgit+RonR+unicorn+nginx+capistrano+Jenkins

Last updated at Posted at 2013-03-05

その11|Capistranoでdeployをstageとproductionで分けてみる。

1.これまでの目次

  1. EC2+gitリモートサーバ構築#1
  2. (2)EC2+gitリモートサーバ構築#2
  3. (3)EC2にrvm+rubyインストール
  4. (4)EC2にrailsインストール
  5. (5)EC2にrails起動テスト
  6. (6)EC2にunicorn準備
  7. (7) EC2にnginx準備
  8. (8) EC2にnginx経由のunicorn
  9. (9)Capistranoでdeploy#1

2.概要

複数の環境にデプロイする事が良くある。今回はstage環境とproduction(本番)環境別に、サーバが分かれていると仮定してdeploy.rbの書き方を纏めてみた。

3.deploy.rb|共通設定を記述

config/deploy.rb
equire 'bundler/capistrano'
require 'capistrano/ext/multistage'

set :application, "アプリケーション名"
set :repository,  "ssh://gituser@xx.xxx.xxx.xxx/home/gituser/repos/hogehoge.git"
set :scm, :git

set :user, "artist"
ssh_options[:port] = "22"
ssh_options[:forward_agent] = true
default_run_options[:pty] = true

#
# ommit upgrade and asset folders
#
set :normalize_asset_timestamps, false

#
# releaseフォルダ内の古いソースコードは過去3つ分までにしよう
#
set :keep_releases, 3

#
# cap deploy:setup 後、/www/shitedama の権限変更
#
namespace :setup do
  task :fix_permissions do
    sudo "chown -R #{user}.#{user} #{deploy_to}"
  end
end
after "deploy:setup", "setup:fix_permissions"

4.環境別のファイルを作成

config/にdeployディレクトリを作成し、その下に環境毎に設定ファイルを作成する。今回は、サーバのIPとdeployディレクトリが違うだけ。

1)stage用|staging.rb
config/deploy/staging.rb
#set :branch, 'stging'
#set :rails_env, 'staging'


set :deploy_to, "/www/#{application}"

role :web, "54.249.238.224"
role :app, "54.249.238.224"
role :db, '54.249.238.224', :primary => true # rake:db:migrateを実行するサーバ
2)production用|production.rb
config/deploy/production.rb
#set :branch, 'prod'
#set :rails_env, 'production'
set :deploy_to, '/usr/local/shite-dama-prod'

role :web, "54.249.248.8"
role :app, "54.249.248.8"
role :db, '54.249.248.8', :primary => true # rake:db:migrateを実行するサーバ

5.デプロイ実行

各ファイル名を指定してcapを実行する。

cap staging deploy:setup
cap staging deploy
cap production deploy:setup
cap production deploy

6.注意点と重要点

  1. deploy.rbのrequire 'capistrano/ext/multistage'が必要です。
  2. ローカルから行うので、どちらのサーバからもgitリポジトリへアクセス出来るようにssh公開鍵の設置。そして、デプロイユーザであるartistユーザが両サーバに存在し、/home/artist/.ssh/autorized_keysにデプロイする人の公開鍵が設置されている必要があります。
8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7