LoginSignup
21
21

More than 5 years have passed since last update.

(9)Capistranoでdeploy#1|EC2でgit+RonR+unicorn+nginx+capistrano+Jenkins

Last updated at Posted at 2013-03-03

その9|Capistranoでdeployしてみる#1

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

2.インストール

ローカルマシンにgemでインストール。インストール後、capコマンドが使えるかどうか確認

gem install capistrano
gem install capistrano-ext
gem install capistrano_colors

3.設定ファイル作成

railsプロジェクトルートディレクトリに移動して、capify .とターミナルから打つと、設定ファイルが作成されます。

[add] writing './Capfile'
[add] writing './config/deploy.rb'
[done] capified!

4.config/deploy.rbを編集

set :application, "sample"
set :repository,  "ssh://gituser@xx.xxx.xxx.xxx/home/gituser/repos/sample.git"
set :scm, :git
set :deploy_to, "/deploy/www/#{application}"
set :rails_env, "production"


# set :scm, :git # You can set :scm explicitly or Capistrano will make an intelligent guess based on known version control directory names
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`

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

role :web, "xx.xxx.xxx.xxx"                          # Your HTTP server, Apache/etc
role :app, "xx.xxx.xxx.xxx"                          # This may be the same as your `Web` server
role :db,  "xx.xxx.xxx.xxx", :primary => true # This is where Rails migrations will run
# cap deploy:setup 後、/deploy/www/sample の権限変更
namespace :setup do
  task :fix_permissions do
    sudo "chown -R #{user}.#{user} #{deploy_to}"
  end
end
after "deploy:setup", "setup:fix_permissions"

5.deployユーザの作成|artistユーザ

  • デプロイ:setup時(後述)、ディレクトリを作成したり権限の変更を行う。その為に、sudoコマンドを使用出来るユーザ(=capistranoでdeployする時にリモート側で実行するユーザ)を予め作成しておく。 wheelグループはsudo可能なので、wheelグループに追加する。
sudo useradd -m -s /bin/bash artist
sudo usermod -G wheel artist
  • そしてwheelグループのユーザがpasswdなしでsudoのコマンドが実行出来るようにする。
sudo su -
visudo

%wheel ALL=(ALL) NOPASSWD: ALL
この行のコメントアウトを削除する。

%wheel ALL=(ALL) ALL
この行はコメントアウトする。

  • デプロイ先の/vaw/wwwは、先にリモートサーバに作成しておく。wwwディレクトリのオーナーはartistユーザに。
mkdir /var/www
chown -Rf artist. /var/www 
  • デプロイユーザartistに対してもcapistranoはSSH+公開鍵認証で接続するので、ローカルの公開鍵をリモートサーバの/home/artist/.ssh/authorized_keysに登録しておく。
sudo su - artist
mkdir .ssh
chmod 700 .ssh
cd .ssh
touch authorized_keys
chmod 600 authorized_keys
cat id_rsa.pub >> authorized_keys

※再終行のcatが分からない人はEC2+gitリモートサーバ構築#1を参考に。

5.デプロイ|setup

まず、デプロイ先を作成。ローカルのターミナルから。

cd RAILS_ROOT_DIR
cap deploy:setup

リモートサーバのdeployディレクトリ以下に、

/deploy/www/sample/
/deploy/www/sample/releases
/deploy/www/sample/shared
/deploy/www/sample/shared/log
/deploy/www/sample/shared/pids
/deploy/www/sample/shared/system

が出来ているはず。

6.デプロイ

cap deploy
21
21
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
21
21