LoginSignup
15
14

More than 5 years have passed since last update.

capistrano + unicorn の最小構成を考える

Last updated at Posted at 2015-07-07

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でダウンタイム無しの再起動してくれるのは感動もの。

15
14
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
15
14