質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
unicorn

Unicornは、汎用のRackアプリケーションサーバ。RackとWebサーバーの機能を併せ持ちます。レスポンス処理や、Nginx単体がRackの機能をサポートしていない事から、一般的にはNginx+Unicorn+Railsの構成を取って用います。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Amazon EC2

Amazon EC2は“Amazon Elastic Compute Cloud”の略称です。Amazon Web Services(AWS)の一部であり、仮想化されたWebサーバーのコンピュータリソースをレンタルできるサービスです。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Q&A

解決済

1回答

971閲覧

本番環境の設定がうまくいきません

退会済みユーザー

退会済みユーザー

総合スコア0

unicorn

Unicornは、汎用のRackアプリケーションサーバ。RackとWebサーバーの機能を併せ持ちます。レスポンス処理や、Nginx単体がRackの機能をサポートしていない事から、一般的にはNginx+Unicorn+Railsの構成を取って用います。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Amazon EC2

Amazon EC2は“Amazon Elastic Compute Cloud”の略称です。Amazon Web Services(AWS)の一部であり、仮想化されたWebサーバーのコンピュータリソースをレンタルできるサービスです。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

0グッド

0クリップ

投稿2020/09/20 05:04

編集2020/09/22 05:44

##知りたいこと

EC2にRailsアプリの本番環境を構築しています。
ウェブサーバーを起動できない問題が発生しており、ログを見るにおそらく設定の記述がどこか間違えているのだと思います。

調べてみてもunicorn+nginxという同じ組み合わせでも記述が異なっていたりして、どこが間違えているのか理解することができませんでした。

頼り方が安易で大変失礼とは承知なのですが、設定ファイルの中身を添削していただけませんでしょうか。

##起きている問題

###1、unicornが起動できない

bundle exec unicorn_rails -c config/unicorn.rb -E production -D

▼エラーメッセージ

master failed to start, check stderr log for details

▼ログ(unicorn.stderr.log)

999 I, [2020-09-20T04:16:29.231266 #18113] INFO -- : Refreshing Gem list 1000 bundler: failed to load command: unicorn_rails (/home/ec2-user/.rbenv/versions/2.6.6/bin/unicorn_rails) 1001 NoMethodError: undefined method `fetch' for #<アプリ名::Application:0x0000000003f5d1a8> ・ ・ ・

###2、データベースを作成できない

rails db:create RAILS_ENV=production

▼エラーメッセージ

rails aborted! NoMethodError: undefined method `fetch' for #<アプリ名::Application:0x00000000023c9ab8> /var/www/アプリ名/config/environments/production.rb:17:in `block in <main>' /var/www/アプリ名/config/environments/production.rb:1:in `<main>' /var/www/アプリ名/config/environment.rb:5:in `<main>' bin/rails:4:in `<main>' Tasks: TOP => db:create => db:load_config => environment (See full trace by running task with --trace)

他にもunicornの起動に関しては、Cannot load database configuration:というエラーが出たことがあったり、

I, [2020-09-19T07:00:51.913723 #9104] INFO -- : Refreshing Gem list bundler: failed to load command: unicorn_rails (/home/ec2-user/.rbenv/versions/2.6.6/bin/unicorn_rails) ActiveSupport::MessageEncryptor::InvalidMessage: Cannot load database configuration: ActiveSupport::MessageEncryptor::InvalidMessage ・ ・ ・

今日試した時にはNoDatabaseErrorも発生しています。

I, [2020-09-22T04:42:19.543564 #2605] INFO -- : Refreshing Gem list I, [2020-09-22T04:42:20.452976 #2605] INFO -- : listening on addr=0.0.0.0:3000 fd=9 E, [2020-09-22T04:42:20.454887 #2605] ERROR -- : Unknown database 'アプリ名_production' (ActiveRecord::NoDatabaseError) ・ ・ ・

###各設定ファイル

多分それぞれ原因は同じで、production.rbdatabase.ymlunicorn.rbなどのいずれかが間違えているのではないかと思います。
以下関係ありそうなファイルです。

production.rb Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. # Code is not reloaded between requests. config.cache_classes = true # Eager load code on boot. This eager loads most of Rails and # your application in memory, allowing both threaded web servers # and those relying on copy on write to perform better. # Rake tasks automatically ignore this option for performance. config.eager_load = true # Full error reports are disabled and caching is turned on. config.consider_all_requests_local = false config.action_controller.perform_caching = true # Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"] # or in config/master.key. This key is used to decrypt credentials (and other encrypted files). config.require_master_key = true # Disable serving static files from the `/public` folder by default since # Apache or NGINX already handles this. config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present? # Compress CSS using a preprocessor. # config.assets.css_compressor = :sass # Do not fallback to assets pipeline if a precompiled asset is missed. config.assets.compile = false # Enable serving of images, stylesheets, and JavaScripts from an asset server. # config.action_controller.asset_host = 'http://assets.example.com' # Specifies the header that your server uses for sending files. # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX # Store uploaded files on the local file system (see config/storage.yml for options). config.active_storage.service = :local # Mount Action Cable outside main process or domain. # config.action_cable.mount_path = nil # config.action_cable.url = 'wss://example.com/cable' # config.action_cable.allowed_request_origins = [ 'http://example.com', /http://example.*/ ] # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. # config.force_ssl = true # Use the lowest log level to ensure availability of diagnostic information # when problems arise. config.log_level = :debug # Prepend all log lines with the following tags. config.log_tags = [ :request_id ] # Use a different cache store in production. # config.cache_store = :mem_cache_store # Use a real queuing backend for Active Job (and separate queues per environment). # config.active_job.queue_adapter = :resque # config.active_job.queue_name_prefix = "アプリ名_production" config.action_mailer.perform_caching = false # Ignore bad email addresses and do not raise email delivery errors. # Set this to true and configure the email server for immediate delivery to raise delivery errors. # config.action_mailer.raise_delivery_errors = false # Enable locale fallbacks for I18n (makes lookups for any locale fall back to # the I18n.default_locale when a translation cannot be found). config.i18n.fallbacks = true # Send deprecation notices to registered listeners. config.active_support.deprecation = :notify # Use default logging formatter so that PID and timestamp are not suppressed. config.log_formatter = ::Logger::Formatter.new # Use a different logger for distributed setups. # require 'syslog/logger' # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name') if ENV["RAILS_LOG_TO_STDOUT"].present? logger = ActiveSupport::Logger.new(STDOUT) logger.formatter = config.log_formatter config.logger = ActiveSupport::TaggedLogging.new(logger) end # Do not dump schema after migrations. config.active_record.dump_schema_after_migration = false # Inserts middleware to perform automatic connection switching. # The `database_selector` hash is used to pass options to the DatabaseSelector # middleware. The `delay` is used to determine how long to wait after a write # to send a subsequent read to the primary. # # The `database_resolver` class is used by the middleware to determine which # database is appropriate to use based on the time delay. # # The `database_resolver_context` class is used by the middleware to set # timestamps for the last write to the primary. The resolver uses the context # class timestamps to determine how long to wait before reading from the # replica. # # By default Rails will store a last write timestamp in the session. The # DatabaseSelector middleware is designed as such you can define your own # strategy for connection switching and pass that into the middleware through # these configuration options. # config.active_record.database_selector = { delay: 2.seconds } # config.active_record.database_resolver = ActiveRecord::Middleware::DatabaseSelector::Resolver # config.active_record.database_resolver_context = ActiveRecord::Middleware::DatabaseSelector::Resolver::Session end
database.yml default: &default adapter: mysql2 pool: 5  timeout: 5000 encording: utf8 database: アプリ名 username: root password: パスワード host: localhost ・ ・ ・ production: <<: *default database: アプリ名_production username: root password: パスワード encoding: utf8 host: localhost
credentials.yml db: database: アプリ名 username: root password: パスワード socket: /var/lib/mysql/mysql.sock (シークレットキー)
unicorn.rb app_path = File.expand_path('../../', __FILE__) worker_processes 1 working_directory app_path pid "#{app_path}/tmp/pids/unicorn.pid" listen 3000 stderr_path "#{app_path}/log/unicorn.stderr.log" stdout_path "#{app_path}/log/unicorn.stdout.log" timeout 60 preload_app true GC.respond_to?(:copy_on_write_friendly=) && GC.copy_on_write_friendly = true check_client_connection false run_once = true before_fork do |server, worker| defined?(ActiveRecord::Base) && ActiveRecord::Base.connection.disconnect! if run_once run_once = false # prevent from firing again end old_pid = "#{server.config[:pid]}.oldbin" if File.exist?(old_pid) && server.pid != old_pid begin sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU Process.kill(sig, File.read(old_pid).to_i) rescue Errno::ENOENT, Errno::ESRCH => e logger.error e end end end after_fork do |_server, _worker| defined?(ActiveRecord::Base) && ActiveRecord::Base.establish_connection end

###試したこと
1、rails db:create RAILS_ENV=productionを実行する

実行した結果は前述の通りです。

2、マスターキー

"config直下にmaster.keyがないです"みたいなエラーが出たこともあって、今は配置済みです。
正直そこも、記事によって書いてあることが違う▼のがよくわからないのですが…

  • アプリ/config/master.key
  • アプリ/shared/config/master.key

最初はshared/configにマスターキーを置いていました。
今はアプリ直下のconfigにも配置してあり、本番環境にマスターキーが二つある状態です。
(これもエラーの原因になるのでしょうか?)

3、production.rbに追記したコードを削除

set :linked_files, fetch(:linked_files, []).push("config/master.key")

この一行を削除しました。

参考にした記事で「deploy.rbにこれを追記する」ということが書いてあったのですが、私の環境にはそのファイルがなかったためproduction.rbに書いていました。しかし調べるとCapistranoを導入していく中で生成されるファイルのようだったので「関係ないかな?」と思い削除しました。

###作業環境

  • macOS Catalina
  • Amazon linux2
  • Ruby 2.6.6
  • Rails 6
  • unicorn 5.4.1

同じような質問を繰り返していて大変申し訳ありません。
ググるのも下手なら質問も下手……という具合でお恥ずかしい限りです。

もしこの質問文の中に不要な情報(あるいは不足している情報)等あればご指摘をお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

y_waiwai

2020/09/20 06:14

コードを提示しましょう
yu_1985

2020/09/20 17:48

その部分だけではなくて、前後のログも合わせて確認してみてください。 エラーとして出てきているのがそこというだけで、根本原因がそことは限りません。
退会済みユーザー

退会済みユーザー

2020/09/22 05:47

文字数制限におさまりきらなかったので、こちらにログの一部を記載します。 NoMethodError: undefined method `fetch' 1002 /var/www/アプリ名/config/environments/production.rb:17:in `block in <main>' 1003 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/railties-6.0.3.2/lib/rails/railtie.rb:216:in `instance_eval' 1004 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/railties-6.0.3.2/lib/rails/railtie.rb:216:in `configure' 1005 /var/www/アプリ名/config/environments/production.rb:1:in `<main>' 1006 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require' 1007 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi' 1008 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' 1009 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi' 1010 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require' 1011 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/zeitwerk-2.4.0/lib/zeitwerk/kernel.rb:34:in `require' 1012 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/activesupport-6.0.3.2/lib/active_support/dependencies.rb:324:in `block in require' 1013 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/activesupport-6.0.3.2/lib/active_support/dependencies.rb:291:in `load_dependency' 1014 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/activesupport-6.0.3.2/lib/active_support/dependencies.rb:324:in `require' 1015 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/railties-6.0.3.2/lib/rails/engine.rb:613:in `block (2 levels) in <class:Engine>' 1016 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/railties-6.0.3.2/lib/rails/engine.rb:612:in `each' 1017 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/railties-6.0.3.2/lib/rails/engine.rb:612:in `block in <class:Engine>' 1018 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/railties-6.0.3.2/lib/rails/initializable.rb:32:in `instance_exec' 1019 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/railties-6.0.3.2/lib/rails/initializable.rb:32:in `run' 1020 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/railties-6.0.3.2/lib/rails/initializable.rb:61:in `block in run_initializers' 1021 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/2.6.0/tsort.rb:228:in `block in tsort_each' 1022 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/2.6.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component' 1023 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/2.6.0/tsort.rb:422:in `block (2 levels) in each_strongly_connected_component_from' 1024 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/2.6.0/tsort.rb:431:in `each_strongly_connected_component_from' 1025 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/2.6.0/tsort.rb:421:in `block in each_strongly_connected_component_from' 1026 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/railties-6.0.3.2/lib/rails/initializable.rb:50:in `each' 1027 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/railties-6.0.3.2/lib/rails/initializable.rb:50:in `tsort_each_child' 1028 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/2.6.0/tsort.rb:415:in `call' 1029 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/2.6.0/tsort.rb:415:in `each_strongly_connected_component_from' 1030 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/2.6.0/tsort.rb:349:in `block in each_strongly_connected_component' 1031 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/2.6.0/tsort.rb:347:in `each' 1032 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/2.6.0/tsort.rb:347:in `call' 1033 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/2.6.0/tsort.rb:347:in `each_strongly_connected_component' 1034 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/2.6.0/tsort.rb:226:in `tsort_each' 1035 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/2.6.0/tsort.rb:205:in `tsort_each' 1036 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/railties-6.0.3.2/lib/rails/initializable.rb:60:in `run_initializers' 1037 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/railties-6.0.3.2/lib/rails/application.rb:363:in `initialize!' 1038 /var/www/filma/config/environment.rb:5:in `<top (required)>' 1039 config.ru:4:in `require_relative' 1040 config.ru:4:in `block in <main>' 1041 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rack-2.2.3/lib/rack/builder.rb:125:in `instance_eval' 1042 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rack-2.2.3/lib/rack/builder.rb:125:in `initialize' 1043 config.ru:1:in `new' 1044 config.ru:1:in `<main>' 1045 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/unicorn-5.4.1/lib/unicorn.rb:56:in `eval' 1046 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/unicorn-5.4.1/lib/unicorn.rb:56:in `block in builder' 1047 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/unicorn-5.4.1/bin/unicorn_rails:139:in `block in rails_builder' 1048 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/unicorn-5.4.1/lib/unicorn/http_server.rb:795:in `build_app!' 1049 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/unicorn-5.4.1/lib/unicorn/http_server.rb:139:in `start' 1050 /home/ec2-user/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/unicorn-5.4.1/bin/unicorn_rails:209:in `<top (required)>' 1051 /home/ec2-user/.rbenv/versions/2.6.6/bin/unicorn_rails:23:in `load' 1052 /home/ec2-user/.rbenv/versions/2.6.6/bin/unicorn_rails:23:in `<top (required)>'
guest

回答1

0

ベストアンサー

変な質問を重ねてしまい大変申し訳ございませんでした。
もっと上手に質問すべく、もう少しわかる部分を増やしたいと思います。
丸投げのような質問で本当にすみませんでした。

投稿2020/09/22 10:51

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問