いつも大変お世話になっております。

今回、初めてRailsアプリを作成し、VPSにデプロイしようとしているところです。

[(初心者向け）vpsを契約して、capistrano3でRailsアプリをデプロイするまで [その1
サーバー設定編]](https://qiita.com/ryo2132/items/f62690f0b16ec11270fe)  
[(初心者向け）vpsを契約して、Capistrano3でRailsアプリをデプロイするまで [その2
ローカル設定編]](https://qiita.com/ryo2132/items/03f5f52b43742f5aef10)

手順は上記ページを参考にさせてもらっています。  
現在はその2の「4-2 デプロイの実行」を行っているところなんですが、どうしても解決できないエラーが発生していて困っています。

まず、ターミナルで`bundle exec cap production deploy --trace`を実行したときに表示されるエラーは以下になります。  
あまりにも長いので関係ありそうなところだけ掲載します。

```

    /Users/user/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/sshkit-1.20.0/lib/sshkit/command.rb:97:in `exit_status=': bundle exit status: 1 (SSHKit::Command::Failed)
    bundle stdout: Nothing written
    bundle stderr: master failed to start, check stderr log for details
            1: from /Users/user/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/sshkit-1.20.0/lib/sshkit/runners/parallel.rb:11:in `block (2 levels) in execute'
    /Users/user/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/sshkit-1.20.0/lib/sshkit/runners/parallel.rb:15:in `rescue in block (2 levels) in execute': Exception while executing as user@150.95.152.189: bundle exit status: 1 (SSHKit::Runner::ExecuteError)
    bundle stdout: Nothing written
    bundle stderr: master failed to start, check stderr log for details
    cap aborted!
    SSHKit::Runner::ExecuteError: Exception while executing as user@150.95.152.189: bundle exit status: 1
    bundle stdout: Nothing written
    bundle stderr: master failed to start, check stderr log for details
    /Users/user/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/sshkit-1.20.0/lib/sshkit/runners/parallel.rb:15:in `rescue in block (2 levels) in execute'
    /Users/user/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/sshkit-1.20.0/lib/sshkit/runners/parallel.rb:11:in `block (2 levels) in execute'
    
    Caused by:
    SSHKit::Command::Failed: bundle exit status: 1
    bundle stdout: Nothing written
    bundle stderr: master failed to start, check stderr log for details
    /Users/user/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/sshkit-1.20.0/lib/sshkit/command.rb:97:in `exit_status='
    /Users/user/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/sshkit-1.20.0/lib/sshkit/backends/netssh.rb:170:in `execute_command'
    /Users/user/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/sshkit-1.20.0/lib/sshkit/backends/abstract.rb:148:in `block in create_command_and_execute'
    /Users/user/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/sshkit-1.20.0/lib/sshkit/backends/abstract.rb:148:in `tap'
    /Users/user/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/sshkit-1.20.0/lib/sshkit/backends/abstract.rb:148:in `create_command_and_execute'
    /Users/user/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/sshkit-1.20.0/lib/sshkit/backends/abstract.rb:80:in `execute'
    /Users/user/Products/blog/lib/capistrano/tasks/unicorn.rb:11:in `block in start_unicorn'
    /Users/user/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/sshkit-1.20.0/lib/sshkit/backends/abstract.rb:92:in `within'
    /Users/user/Products/blog/lib/capistrano/tasks/unicorn.rb:10:in `start_unicorn'
    /Users/user/Products/blog/lib/capistrano/tasks/unicorn.rb:53:in `block (3 levels) in <top (required)>'
    /Users/user/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/sshkit-1.20.0/lib/sshkit/backends/abstract.rb:31:in `instance_exec'
    /Users/user/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/sshkit-1.20.0/lib/sshkit/backends/abstract.rb:31:in `run'
    /Users/user/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/sshkit-1.20.0/lib/sshkit/runners/parallel.rb:12:in `block (2 levels) in execute'
    Tasks: TOP => unicorn:restart
    The deploy has failed with an error: Exception while executing as user@150.95.152.189: bundle exit status: 1
    bundle stdout: Nothing written
    bundle stderr: master failed to start, check stderr log for details
    ** Invoke deploy:failed (first_time)
    ** Execute deploy:failed
    
```

続いて、VPSの`unicorn.log`のエラー内容が以下になります。

```

    [fog][DEPRECATION] Fog::Storage::AWS is deprecated, please use Fog::AWS::Storage.
    [fog][WARNING] Unrecognized arguments: region, aws_access_key_id, aws_secret_access_key
    bundler: failed to load command: unicorn (/var/www/app/shared/bundle/ruby/2.6.0/bin/unicorn)
    TypeError: no implicit conversion of nil into String
      /var/www/app/shared/bundle/ruby/2.6.0/gems/fog-aws-3.5.2/lib/fog/aws/signaturev4.rb:12:in `+'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/fog-aws-3.5.2/lib/fog/aws/signaturev4.rb:12:in `initialize'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/fog-aws-3.5.2/lib/fog/aws/storage.rb:544:in `new'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/fog-aws-3.5.2/lib/fog/aws/storage.rb:544:in `setup_credentials'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/fog-aws-3.5.2/lib/fog/aws/storage.rb:527:in `initialize'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/fog-core-2.1.0/lib/fog/core/service.rb:115:in `new'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/fog-core-2.1.0/lib/fog/core/service.rb:115:in `new'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/fog-aws-3.5.2/lib/fog/aws/storage.rb:788:in `new'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/fog-core-2.1.0/lib/fog/core/services_mixin.rb:16:in `new'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/fog-core-2.1.0/lib/fog/storage.rb:22:in `new'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/carrierwave-2.0.0/lib/carrierwave/storage/fog.rb:68:in `eager_load'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/carrierwave-2.0.0/lib/carrierwave.rb:77:in `block in <class:Railtie>'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/lazy_load_hooks.rb:69:in `block in execute_hook'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/lazy_load_hooks.rb:62:in `with_execution_control'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/lazy_load_hooks.rb:67:in `execute_hook'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/lazy_load_hooks.rb:52:in `block in run_load_hooks'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/lazy_load_hooks.rb:51:in `each'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/lazy_load_hooks.rb:51:in `run_load_hooks'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/application/finisher.rb:118:in `block in <module:Finisher>'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/initializable.rb:32:in `instance_exec'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/initializable.rb:32:in `run'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/initializable.rb:61:in `block in run_initializers'
      /home/user/.rbenv/versions/2.6.3/lib/ruby/2.6.0/tsort.rb:228:in `block in tsort_each'
      /home/user/.rbenv/versions/2.6.3/lib/ruby/2.6.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
      /home/user/.rbenv/versions/2.6.3/lib/ruby/2.6.0/tsort.rb:431:in `each_strongly_connected_component_from'
      /home/user/.rbenv/versions/2.6.3/lib/ruby/2.6.0/tsort.rb:349:in `block in each_strongly_connected_component'
      /home/user/.rbenv/versions/2.6.3/lib/ruby/2.6.0/tsort.rb:347:in `each'
      /home/user/.rbenv/versions/2.6.3/lib/ruby/2.6.0/tsort.rb:347:in `call'
      /home/user/.rbenv/versions/2.6.3/lib/ruby/2.6.0/tsort.rb:347:in `each_strongly_connected_component'
      /home/user/.rbenv/versions/2.6.3/lib/ruby/2.6.0/tsort.rb:226:in `tsort_each'
      /home/user/.rbenv/versions/2.6.3/lib/ruby/2.6.0/tsort.rb:205:in `tsort_each'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/initializable.rb:60:in `run_initializers'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/application.rb:363:in `initialize!'
      /var/www/app/releases/20191008231529/config/environment.rb:5:in `<top (required)>'
      config.ru:4:in `require_relative'
      config.ru:4:in `block in <main>'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/builder.rb:55:in `instance_eval'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/builder.rb:55:in `initialize'
      config.ru:1:in `new'
      config.ru:1:in `<main>'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/unicorn-5.5.1/lib/unicorn.rb:54:in `eval'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/unicorn-5.5.1/lib/unicorn.rb:54:in `block in builder'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/unicorn-5.5.1/lib/unicorn/http_server.rb:794:in `build_app!'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/unicorn-5.5.1/lib/unicorn/http_server.rb:141:in `start'
      /var/www/app/shared/bundle/ruby/2.6.0/gems/unicorn-5.5.1/bin/unicorn:128:in `<top (required)>'
      /var/www/app/shared/bundle/ruby/2.6.0/bin/unicorn:23:in `load'
      /var/www/app/shared/bundle/ruby/2.6.0/bin/unicorn:23:in `<top (required)>'
    
```

**1,2行目のfogに関するエラーについて**  
`config/initializers/carrier_wave.rb`にてAWSの設定を行っています。  
各値はdotenvというGemを使って環境変数に設定しています。  
VPSには`config/master.key`を作成し、ローカルと同じ値を設定しました。

**3行目以降のエラーについて**  
UnicornはGemfileの`gem 'unicorn'`で入るはずだと思うんですが…  
一応、VPSで`gem install unicorn`を実行してから再デプロイするも状況変わらず。

CapistranoやらUnicornやらNginxやら初めてのことばかりでよくわかっていないので、どなたか解決法を教えていただけると大変助かります。
CC BY-SA 4.0
2019-10-08T23:49:36.080
59584
2023-07-12T09:02:45.323
2019-10-09T00:04:54.520
2238
26515
question
ruby-on-rails nginx capistrano unicorn
CapistranoでRailsアプリのデプロイ中にUnicornにてエラーが発生
【自己解決】

`config/deploy.rb`に以下を追記しました。

```

    set :default_env, {
      rbenv_root: "/home/user/.rbenv",
      path: "/home/user/.rbenv/bin:$PATH",
      region: ENV["S3_REGION"],
      aws_access_key_id: ENV["S3_ACCESS_KEY"],
      aws_secret_access_key: ENV["S3_SECRET_KEY"]
    }
    
```

Capistranoを使ったデプロイでは環境変数`.bash_profile`を読み込まずに実行されます。  
ですので、Capistrano(Unicorn?)の設定ファイルで明示的に環境変数を設定する必要があります。

See also: [Rails issue on cap production
deploy](https://stackoverflow.com/questions/34224394/rails-issue-on-cap-
production-deploy)

`.bashrc`に設定する方法もあるようです（未検証）。  
[Capistrano +
carrierwaveを使用した際に出るエラー解消](https://qiita.com/yoshis777/items/692dcebda3570017af6f)
CC BY-SA 4.0
2019-10-10T07:45:08.030
59617
2019-10-10T07:45:08.030
26515
59584
answer