capistrano + unicornを使ってみたいけど、あまりに高性能すぎるので最小構成をまずは作ってみたよ。
Gemfile
gem 'capistrano'
gem 'capistrano-rails'
gem 'capistrano-bundler'
gem 'capistrano3-unicorn'
config/deploy.rb
lock '3.4.0'
#gitリポジトリ
set :repo_url, 'git@bitbucket.org:hoge/hoge.git'
config/deploy/production.rb
#env、設定しすぎじゃね?
set :stage, :production
set :rails_env, :production
set :unicorn_rack_env, "production"
set :deploy_to, 'path/to/デプロイ先ディレクトリ'
set :unicorn_pid, "#{shared_path}/tmp/pids/unicorn.pid"
#デプロイ先のユーザとホストを指定
role :app, "user_id@nnn.nnn.nnn.nnn"
set :ssh_options, {
keys: [File.expand_path('~/~/.ssh/id_rsa.pub')],
forward_agent: true
}
unicorn.rbをunicorn/production.rbで作成する
config/unicorn/production.rb
#pidをフルパスで指定する
pid "path/to/デプロイ先ディレクトリ/shared/tmp/pids/unicorn.pid"
#この後は普通にunicorn.rb
ここまでの設定を記載した上でcapするとデプロイされて再起動されます。
#サーバにデプロイ
bundle exec cap production deploy
#unicornの再起動
bundle exec cap production deploy:restart
スッキリしないのが、unicornまわりでenvの設定とpidの設定
pidをフルパスで書かないといけないのはイヤだね、スマートに相対パスで書く方法があるはず。
学習コストはなかなか高いけど、deplpyしただけでassetsコンパイル/migrate/bundle/installをやってくれて、
restartでダウンタイム無しの再起動してくれるのは感動もの。