Capistranoの実行環境の作成方法2


Hostの中

```bash
ssh-keygen -t rsa
```

パスワードはjapan

```bash
su - japan
```

```bash
git clone your url
```


```bash
cd your project
```

```bash
vi Gemfile
```

この設定はdeploy用のbranch。

```Gemfile/deploy-branch
source 'https://rubygems.org'

gem 'capistrano'
gem 'capistrano-bundler'
gem 'capistrano-rails'
gem 'capistrano3-unicorn'
gem 'capistrano-maintenance'
```

```bash
bundle install --path vendor/bundle
```

capistranoで使えるコマンドを確認

```bash
bundle exec cap -T
```

```bash
eval `ssh-agent -s`
```

```bash
ssh-add ~/.ssh/id_rsa
```

development、staging、productionの共通設定をここに記述

```bash
vi config/deploy.rb
```

```deploy.rb//deploy-branch
lock '3.2.1'
set :user, 'japan' #実行ユーザー
set :application, 'your_project' #プロジェクト名
set :repo_url, 'git@github.com:your url' #gitのURL
set :scm, :git #gitを使う
set :deploy_via, :remote_cache #とりあえず、これを設定すると早いぐらいの理解
set :keep_releases, 5 #5回分のリリースをサーバーに保存
set :deploy_to, "/srv/www/#{fetch(:application)}" #deploy先の設定
shared_path = "#{fetch(:deploy_to)}/shared" #sharedファイルの保存先
release_path = "#{fetch(:deploy_to)}/current" #リリースファイルの保存先
set :linked_files, %w{config/database.yml config/application.yml} #sharedファイルをdeploy時にreleaseにコピー
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets bundle}
set :unicorn_pid, "#{shared_path}/tmp/pids/unicorn.pid"
set :unicorn_config_path, "#{release_path}/config/unicorn.rb"
after 'deploy:publishing', 'deploy:restart'
namespace :deploy do
  task :restart do
    invoke 'unicorn:restart'
  end
end
```

```bash
vi config/deploy/production.rb
```

```production.rb//deploy-branch
set :rails_env, 'production'
set :branch, 'master' #リリース時にbranchの指定
server '192.168.43.62', user: 'japan', roles: %w{web app db}, :primary => true #対象サーバー、ユーザー
#現在、使用しているsshのkeyを使って、githubからdeploy先にソースコードを持ってくる
set :ssh_options, keys: %w(/home/japan/.ssh/id_rsa), forward_agent: true, auth_methods: %w(publickey) 
set :unicorn_rack_env, 'production'
```

```bash
vi config/unicorn.rb
```

```bash/master-branch
app_path = "/srv/www/your_project"
 
worker_processes 4
preload_app true
timeout = 30
listen "#{app_path}/shared/tmp/sockets/unicorn.sock" #ここの設定をnginxの設定と合わせる必要がある。
working_directory = "#{app_path}/current"
rails_env = ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'production'
stderr_path "log/unicorn.log"
stdout_path "log/unicorn.log"
pid "#{app_path}/shared/tmp/pids/unicorn.pid"
preload_app true
before_fork do |server, worker|
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
  old_pid = "#{server.config[:pid]}.oldbin"
  puts "old_pid=#{old_pid}"
  if File.exists?(old_pid) && server.pid != old_pid
    begin
      Process.kill("QUIT", File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
    end
  end
end
after_fork do |server, worker|
  if defined?(ActiveRecord::Base)
    config = Rails.application.config.database_configuration[Rails.env]
    config['reaping_frequency'] = ENV['DB_REAP_FREQ'] || 10 # seconds
    config['pool']              = ENV['DB_POOL'] || 5
    ActiveRecord::Base.establish_connection(config)
  end
end
before_exec do |server|
  ENV["BUNDLE_GEMFILE"] = "/srv/www/your_project/current/Gemfile"
end
```

```bash
bundle exec cap production deploy
```

初回はshared/configにdatabase.ymlを作成する必要がある

```
vi /srv/www/your_project/shared/config/database.yml
```

初回はshared/configにapplication.ymlを作成する必要がある

```
vi /srv/www/your_project/shared/config/application.yml
```

front1でnokogiriのエラーが出たら、これを実行

```bash
bundle config build.nokogiri --use-system-libraries
```