前提・実現したいこと
Rails5をCapistrano3でAWSにデプロイしたいのですが、SSH接続に失敗しているようで上手くいきません。
Capistranoを導入し、Capfile、production.rb、deploy.rb、task.rb、EC2サーバーの設定とDB作成、Nginxの設定と再起動、git pushしてデプロイを実行すると以下のエラーが表示されてしまいました。
ローカルからssh -i "~/.ssh/test-keygen" ec2-user@54.226.13.75
でEC2にSSH接続できています。
またdry-runではエラーは出ていません。
アドバイスをいただけると幸いです。
環境:
Ruby 2.6.3
Rails 5.2.4.3
Capistrano 3.14.1
AWS Linux2
Nginx
Unicorn
設定参考:
(Capistrano編)世界一丁寧なAWS解説。EC2を利用して、RailsアプリをAWSにあげるまで
Rails5.1.2 + Webpacker + ReactをCapstranoでデプロイする手順書①
Rails5.1.2 + Webpacker + ReactをCapstranoでデプロイする手順書②
発生している問題・エラーメッセージ
$ bundle exec cap production deploy --trace SSHKit::Runner::ExecuteError: Exception while executing as ec2-user@54.226.13.75: Authentication failed for user ec2-user@54.226.13.75 /usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/runners/parallel.rb:15:in `rescue in block (2 levels) in execute' /usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/runners/parallel.rb:11:in `block (2 levels) in execute' Caused by: Net::SSH::AuthenticationFailed: Authentication failed for user ec2-user@54.226.13.75 /usr/local/bundle/gems/net-ssh-6.1.0/lib/net/ssh.rb:268:in `start' /usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/backends/connection_pool.rb:63:in `call' /usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/backends/connection_pool.rb:63:in `with' /usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/backends/netssh.rb:177:in `with_ssh' /usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/backends/netssh.rb:130:in `execute_command' /usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/backends/abstract.rb:148:in `block in create_command_and_execute' /usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/backends/abstract.rb:148:in `tap' /usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/backends/abstract.rb:148:in `create_command_and_execute' /usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/backends/abstract.rb:61:in `test' /usr/local/bundle/gems/capistrano-rbenv-2.2.0/lib/capistrano/tasks/rbenv.rake:10:in `block (3 levels) in <top (required)>' /usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/backends/abstract.rb:31:in `instance_exec' /usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/backends/abstract.rb:31:in `run' /usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/runners/parallel.rb:12:in `block (2 levels) in execute' Tasks: TOP => rbenv:validate
該当のソースコード
(Capfile) require 'capistrano/setup' require 'capistrano/deploy' require 'capistrano/rbenv' require 'capistrano/bundler' require 'capistrano/rails/assets' require 'capistrano/rails/migrations' Dir.glob('lib/capistrano/tasks/*.rb').each { |r| import r }
(config/deploy/production.rb) server '54.226.13.75', user: 'ec2-user', roles: %w{app db web} set :ssh_options, keys: '~/.ssh/test-keygen'
(config/deploy.rb) lock '3.14.1' set :application, 'capistrano-deploy' set :repo_url, 'git@github.com:****/capistrano-deploy.git' set :branch, 'master' set :deploy_to, '/var/www/rails' set :linked_files, fetch(:linked_files, []).push('config/settings.yml') set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system') set :keep_releases, 5 set :rbenv_ruby, '2.6.3' set :log_level, :debug namespace :deploy do desc 'Restart application' task :restart do invoke 'unicorn:restart' end desc 'Create database' task :db_create do on roles(:db) do |host| with rails_env: fetch(:rails_env) do within current_path do execute :bundle, :exec, :rake, 'db:create' end end end end desc 'Run seed' task :seed do on roles(:app) do with rails_env: fetch(:rails_env) do within current_path do execute :bundle, :exec, :rake, 'db:seed' end end end end after :publishing, :restart after :restart, :clear_cache do on roles(:web), in: :groups, limit: 3, wait: 10 do end end end
EC2内 (shared/config/settings.yml) production: secret_key_base: ローカルでrake secretで作成した乱数
EC2内 (/etc/nginx/conf.d/capistrano-deploy.conf) error_log /var/www/rails/capistrano-deploy/current/log/nginx.error.log; access_log /var/www/rails/capistrano-deploy/current/log/nginx.access.log; client_max_body_size 2G; upstream app_server { server unix:/var/www/rails/capistrano-deploy/current/tmp/sockets/.unicorn.sock fail_timeout=0; } server { listen 80; server_name 自分のIPアドレス; keepalive_timeout 5; root /var/www/rails/capistrano-deploy/current/public; try_files $uri/index.html $uri.html $uri @app; location @app { 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; location = /500.html { root /var/www/rails/capistrano-deploy/current/public; } }
mysql> SHOW DATABASES; +------------------------------+ | Database | +------------------------------+ | information_schema | | capistrano_deploy_production | | mysql | | performance_schema | +------------------------------+
(lib/capistrano/tasks/unicorn.rb) namespace :unicorn do task :environment do set :unicorn_pid, "#{current_path}/tmp/pids/unicorn.pid" set :unicorn_config, "#{current_path}/config/unicorn/production.rb" end def start_unicorn within current_path do execute :bundle, :exec, :unicorn, "-c #{fetch(:unicorn_config)} -E #{fetch(:rails_env)} -D" end end def stop_unicorn execute :kill, "-s QUIT $(< #{fetch(:unicorn_pid)})" end def reload_unicorn execute :kill, "-s USR2 $(< #{fetch(:unicorn_pid)})" end def force_stop_unicorn execute :kill, "$(< #{fetch(:unicorn_pid)})" end desc "Start unicorn server" task start: :environment do on roles(:app) do start_unicorn end end desc "Stop unicorn server gracefully" task stop: :environment do on roles(:app) do stop_unicorn end end desc "Restart unicorn server gracefully" task restart: :environment do on roles(:app) do if test("[ -f #{fetch(:unicorn_pid)} ]") reload_unicorn else start_unicorn end end end desc "Stop unicorn server immediately" task force_stop: :environment do on roles(:app) do force_stop_unicorn end end end
(config/unicorn/production.rb) $worker = 2 $timeout = 30 $app_dir = "/var/www/capistrano-deploy/current" $listen = File.expand_path 'tmp/sockets/.unicorn.sock', $app_dir $pid = File.expand_path 'tmp/pids/unicorn.pid', $app_dir $std_log = File.expand_path 'log/unicorn.log', $app_dir worker_processes $worker working_directory $app_dir stderr_path $std_log stdout_path $std_log timeout $timeout listen $listen pid $pid preload_app true before_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect! old_pid = "#{server.config[:pid]}.oldbin" if old_pid != server.pid begin Process.kill "QUIT", File.read(old_pid).to_i rescue Errno::ENOENT, Errno::ESRCH end end end after_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection end
(config/database.yml) production: adapter: mysql2 encoding: utf8 pool: 5 database: capistrano_deploy_production username: root password: socket: /var/lib/mysql/mysql.sock
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。