bundle exec cap production deploy のエラーを解決

soichirohara@SoichironoMacBook-Pro coffee_passport % bundle exec cap production deploy 
00:00 git:wrapper
      01 mkdir -p /tmp
    ✔ 01 ec2-user@18.179.97.156 0.610s
      Uploading /tmp/git-ssh-c2f9fb0fc8f10de72794.sh 100.0%
      02 chmod 700 /tmp/git-ssh-c2f9fb0fc8f10de72794.sh
    ✔ 02 ec2-user@18.179.97.156 0.511s
00:01 git:check
      01 git ls-remote git@github.com:Harasou21/coffee_passport.git HEAD
      01 32ffebaf22e8ab45d1bae017fce9c3f1fd1a0ed3	HEAD
    ✔ 01 ec2-user@18.179.97.156 2.546s
00:04 deploy:check:directories
      01 mkdir -p /var/www/coffee_passport/shared /var/www/coffee_passport/releases
    ✔ 01 ec2-user@18.179.97.156 0.612s
00:05 deploy:check:linked_dirs
      01 mkdir -p /var/www/coffee_passport/shared/log /var/www/coffee_passport/shared/tmp/pids /var/www/coffee_passport/shared/tmp/cache /var/www/coffee_passport/shared/tmp/sockets /var/www/coffee_passpo…
    ✔ 01 ec2-user@18.179.97.156 0.610s
00:05 deploy:check:make_linked_dirs
      01 mkdir -p /var/www/coffee_passport/shared/config
    ✔ 01 ec2-user@18.179.97.156 0.510s
00:06 deploy:check:linked_files
      ERROR linked file /var/www/coffee_passport/shared/config/master.key does not exist on 18.179.97.156


　とあったので、 /var/www/coffee_passport/shared/config/master.key を作成し、マスターキーを設置


 git:clone
      01 git clone --mirror git@github.com:Harasou21/coffee_passport.git /var/www/coffee_passport/repo
      01 fatal: destination path '/var/www/coffee_passport/repo' already exists and is not an empty directory.
#<Thread:0x00007f8c170c2808@/Users/soichirohara/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sshkit-1.21.2/lib/sshkit/runners/parallel.rb:10 run> terminated with exception (report_on_exception is true):
Traceback (most recent call last):
	15: from /Users/soichirohara/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sshkit-1.21.2/lib/sshkit/runners/parallel.rb:12:in `block (2 levels) in execute'
	14: from /Users/soichirohara/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:31:in `run'
	13: from /Users/soichirohara/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:31:in `instance_exec'
	12: from /Users/soichirohara/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/capistrano-3.16.0/lib/capistrano/scm/tasks/git.rake:30:in `block (3 levels) in eval_rakefile'
	11: from /Users/soichirohara/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:92:in `within'
	10: from /Users/soichirohara/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/capistrano-3.16.0/lib/capistrano/scm/tasks/git.rake:31:in `block (4 levels) in eval_rakefile'
	 9: from /Users/soichirohara/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:100:in `with'
	 8: from /Users/soichirohara/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/capistrano-3.16.0/lib/capistrano/scm/tasks/git.rake:32:in `block (5 levels) in eval_rakefile'
	 7: from /Users/soichirohara/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/capistrano-3.16.0/lib/capistrano/scm/git.rb:47:in `clone_repo'
	 6: from /Users/soichirohara/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/capistrano-3.16.0/lib/capistrano/scm/git.rb:83:in `git'
	 5: from /Users/soichirohara/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:80:in `execute'
	 4: from /Users/soichirohara/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:148:in `create_command_and_execute'
	 3: from /Users/soichirohara/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:148:in `tap'
	 2: from /Users/soichirohara/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:148:in `block in create_command_and_execute'
	 1: from /Users/soichirohara/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sshkit-1.21.2/lib/sshkit/backends/netssh.rb:170:in `execute_command'
/Users/soichirohara/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sshkit-1.21.2/lib/sshkit/command.rb:97:in `exit_status=': git exit status: 128 (SSHKit::Command::Failed)
git stdout: Nothing written
git stderr: fatal: destination path '/var/www/coffee_passport/repo' already exists and is not an empty directory.
	1: from /Users/soichirohara/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sshkit-1.21.2/lib/sshkit/runners/parallel.rb:11:in `block (2 levels) in execute'
/Users/soichirohara/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sshkit-1.21.2/lib/sshkit/runners/parallel.rb:15:in `rescue in block (2 levels) in execute': Exception while executing as ec2-user@18.179.97.156: git exit status: 128 (SSHKit::Runner::ExecuteError)
git stdout: Nothing written
git stderr: fatal: destination path '/var/www/coffee_passport/repo' already exists and is not an empty directory.
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as ec2-user@18.179.97.156: git exit status: 128
git stdout: Nothing written
git stderr: fatal: destination path '/var/www/coffee_passport/repo' already exists and is not an empty directory.


Caused by:
SSHKit::Command::Failed: git exit status: 128
git stdout: Nothing written
git stderr: fatal: destination path '/var/www/coffee_passport/repo' already exists and is not an empty directory.

Tasks: TOP => git:create_release => git:update => git:clone
(See full trace by running task with --trace)
The deploy has failed with an error: Exception while executing as ec2-user@18.179.97.156: git exit status: 128
git stdout: Nothing written
git stderr: fatal: destination path '/var/www/coffee_passport/repo' already exists and is not an empty directory.


** DEPLOY FAILED
** Refer to log/capistrano.log for details. Here are the last 20 lines:


 DEBUG [2a9c6a55] Command: ( export RBENV_ROOT="$HOME/.rbenv" RBENV_VERSION="2.6.5" ; /usr/bin/env mkdir -p /var/www/coffee_passport/shared/log /var/www/coffee_passport/shared/tmp/pids /var/www/coffee_passport/shared/tmp/cache /var/www/coffee_passport/shared/tmp/sockets /var/www/coffee_passport/shared/vendor/bundle /var/www/coffee_passport/shared/public/system /var/www/coffee_passport/shared/public/uploads /var/www/coffee_passport/shared/public/assets )

  INFO [2a9c6a55] Finished in 0.485 seconds with exit status 0 (successful).

  INFO [80e28b62] Running /usr/bin/env mkdir -p /var/www/coffee_passport/shared/config as ec2-user@18.179.97.156

 DEBUG [80e28b62] Command: ( export RBENV_ROOT="$HOME/.rbenv" RBENV_VERSION="2.6.5" ; /usr/bin/env mkdir -p /var/www/coffee_passport/shared/config )

  INFO [80e28b62] Finished in 0.485 seconds with exit status 0 (successful).

 DEBUG [c840d079] Running [ -f /var/www/coffee_passport/shared/config/master.key ] as ec2-user@18.179.97.156

 DEBUG [c840d079] Command: [ -f /var/www/coffee_passport/shared/config/master.key ]

 DEBUG [c840d079] Finished in 0.498 seconds with exit status 0 (successful).

 DEBUG [a300cb69] Running [ -f /var/www/coffee_passport/current/REVISION ] as ec2-user@18.179.97.156

 DEBUG [a300cb69] Command: [ -f /var/www/coffee_passport/current/REVISION ]

 DEBUG [a300cb69] Finished in 0.482 seconds with exit status 1 (failed).

 DEBUG [a421d19f] Running [ -f /var/www/coffee_passport/repo/HEAD ] as ec2-user@18.179.97.156

 DEBUG [a421d19f] Command: [ -f /var/www/coffee_passport/repo/HEAD ]

 DEBUG [a421d19f] Finished in 0.487 seconds with exit status 1 (failed).

 DEBUG [2b311b18] Running if test ! -d /var/www/coffee_passport; then echo "Directory does not exist '/var/www/coffee_passport'" 1>&2; false; fi as ec2-user@18.179.97.156

 DEBUG [2b311b18] Command: if test ! -d /var/www/coffee_passport; then echo "Directory does not exist '/var/www/coffee_passport'" 1>&2; false; fi

 DEBUG [2b311b18] Finished in 0.532 seconds with exit status 0 (successful).

  INFO [92269722] Running /usr/bin/env git clone --mirror git@github.com:Harasou21/coffee_passport.git /var/www/coffee_passport/repo as ec2-user@18.179.97.156

 DEBUG [92269722] Command: cd /var/www/coffee_passport && ( export RBENV_ROOT="$HOME/.rbenv" RBENV_VERSION="2.6.5" GIT_ASKPASS="/bin/echo" GIT_SSH="/tmp/git-ssh-3f1f66c3ab602d68da8c.sh" ; /usr/bin/env git clone --mirror git@github.com:Harasou21/coffee_passport.git /var/www/coffee_passport/repo )

 DEBUG [92269722] 	fatal: destination path '/var/www/coffee_passport/repo' already exists and is not an empty directory.

とエラーが変わった。




fatal: destination path '/var/www/coffee_passport/repo' already exists and is not an empty directory.

git stderr: fatal: destination path '/var/www/coffee_passport/repo' already exists and is not an empty directory.



```

 sudo vim /etc/nginx/conf.d/rails.conf

```

をEC2ターミナルで実行後に

```
upstream app_server {
  # Unicornと連携させるための設定
  server unix:/var/www/リポジトリ名/shared/tmp/sockets/unicorn.sock;
}

# {}で囲った部分をブロックと呼ぶ。サーバの設定ができる
server {
  # このプログラムが接続を受け付けるポート番号
  listen 80;
  # 接続を受け付けるリクエストURL ここに書いていないURLではアクセスできない
  server_name Elastic IP;

  # クライアントからアップロードされてくるファイルの容量の上限を2ギガに設定。デフォルトは1メガなので大きめにしておく
  client_max_body_size 2g;

# 接続が来た際のrootディレクトリ
  root /var/www/リポジトリ名/current/public;

# assetsファイル(CSSやJavaScriptのファイルなど)にアクセスが来た際に適用される設定
  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
    root /var/www/リポジトリ名/current/public;
  }

  try_files $uri/index.html $uri @unicorn;

  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://app_server;
  }

  error_page 500 502 503 504 /500.html;
}
```


```config/unicorn.rb

#サーバ上でのアプリケーションコードが設置されているディレクトリを変数に入れておく
app_path = File.expand_path('../../../', __FILE__)  # 「../」が一つ増えている

#アプリケーションサーバの性能を決定する
worker_processes 1

#アプリケーションの設置されているディレクトリを指定
working_directory "#{app_path}/current"  # 「current」を指定

#Unicornの起動に必要なファイルの設置場所を指定
pid "#{app_path}/shared/tmp/pids/unicorn.pid"  # 「shared」の中を参照するよう変更

#ポート番号を指定
listen "#{app_path}/shared/tmp/sockets/unicorn.sock"  # 「shared」の中を参照するよう変更

#エラーのログを記録するファイルを指定
stderr_path "#{app_path}/shared/log/unicorn.stderr.log"  # 「shared」の中を参照するよう変更

#通常のログを記録するファイルを指定
stdout_path "#{app_path}/shared/log/unicorn.stdout.log"  # 「shared」の中を参照するよう変更
（省略）
```

　にする。


[ec2-user@ip-172-31-25-189 ~]$ sudo systemctl reload nginx
[ec2-user@ip-172-31-25-189 ~]$ sudo systemctl restart nginx