LoginSignup
12
13

More than 5 years have passed since last update.

Ruby on Rails + Nginx + AWS + Unicorn + Rbenv 環境へCapistranoでデプロイ

Last updated at Posted at 2015-06-07

はじめに

2015-08-16
大分、雑だったため追記/修正

Capistranoのデプロイ先の環境
- AWS
- Nginx
- Ruby on Rails
- Unicorn
- Rbenv

今回行うこと

AWS上のサーバーへデプロイ (Ruby環境へのデプロイをします。)

Capistanoのインストール

通常どおり、ディレクトリの作成

$ mkdir capistrano
$ cd capistrano
$ bundle init

Gemファイルの中身(Capistranoは3.x系を使用)

Gemfile.
# A sample Gemfile
source "https://rubygems.org"
gem "capistrano", "3.1.0"

# rails用gem
gem 'capistrano-rails'
gem 'capistrano-bundler'
gem 'capistrano3-unicorn'

Gemのインストール

$ bundle install --path vendor/bundle

Capistranoのインストール

bundle exec cap install

これで必要ディレクトリが生成されている

デプロイ用ファイル作成

デプロイのフローに関しては下記を参照
http://capistranorb.com/documentation/getting-started/flow/

config/deploy.rb
# config valid only for Capistrano 3.1
lock '3.1.0'

set :application, 'APPNAME'
set :repo_url, 'リポジトリPATH'

set :deploy_to, '/var/www/'
set :scm, :git
set :keep_releases, 5

# rbenvの設定 (環境によってrbenvの場所が違うと思います。)
set :default_env, {
  rbenv_root: "/usr/local/rbenv/",
  path: "/usr/local/rbenv/shims:/usr/local/rbenv/bin:$PATH"
}

# Unicorn周りの設定
set :unicorn_rack_env, "production"
set :unicorn_config_path, "config/unicorn.rb"
set :unicorn_pid, "/tmp/unicorn_APPNAME.pid"

# currentからsharedへsymlinkを貼るものを指定
set :linked_dirs, %w{bin bundle log tmp/pids tmp/cache tmp/sockets public/system}

# logを詳細表示
set :format, :pretty
set :log_level, :debug

namespace :deploy do
  desc 'Restart application'
  task :restart do
    invoke 'unicorn:restart'
  end

  after :finished, :restart
end

desc 'execute before deploy'
task :db_create do
  on roles(:db) do |host|
    execute "mysql -uroot -e 'CREATE DATABASE IF NOT EXISTS APPNAME_production;'"
  end
end

接続用の環境設定

config/deploy/production.rb
set :rails_env, :production
server 'ServerNmae',
  user: 'UserName',
  roles: %w{web app db}

4 デプロイ

初回デプロイの場合はまず、DB作成用のタスクを走らせる

$ bundle exec cap production db_create

次にデプロイをさせればOK

$ bundle exec cap production deploy

参考

bundle install中にエラーが出た場合の対処法

Amazon Linuxでbundle installのエラーに対処する
http://dev.classmethod.jp/cloud/aws/amazon-linux-bundler-error/

サーバー上で鍵交換をさせるのがイヤな人はssh-addで対応できるかと思います。

ssh-agentの使い方
http://qiita.com/isaoshimizu/items/84ac5a0b1d42b9d355cf

12
13
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
12
13