全容
自作railsアプリケーションのAWS ec2へデプロイ作業中、unicorn起動でエラーが発生。
概ね後述の記事を参考に進めていますが、イレギュラー要因として製造途中でrailsのバージョン変更を行っている事による必要ファイル不足と、その対応内容に誤りがあるのかと考えています。
現在も自力で調査中ですが知見が浅く煮詰まっているおり、有識者の方ご助言をお願いいたします。
前提
・自作railsアプリケーションのAWS ec2へデプロイ作業中
・DB:MySQL
・Nginx + unicornを使用
手順参考記事
独学ではじめてAWSのEC2にデプロイする方法⑨(Railsの起動)
問題発生箇所
Railsの起動 - Rails5.2以降の場合 - ユニコーンを起動 の項目実行中
[ec2-user@ip-172-31-23-189 ~]$ cd /var/www/[リポジトリ] [ec2-user@ip-172-31-23-189 <app名>]$ bundle exec unicorn_rails -c config/unicorn.rb -E production -D
エラーログ
unicorn.stderr.log
NoMethodError: Cannot load `Rails.application.database_configuration`: undefined method `[]' for nil:NilClass (erb):31:in `<main>' /home/ec2-user/.rbenv/versions/2.6.7/lib/ruby/2.6.0/erb.rb:901:in `eval' /home/ec2-user/.rbenv/versions/2.6.7/lib/ruby/2.6.0/erb.rb:901:in `result' /home/ec2-user/.rbenv/versions/2.6.7/lib/ruby/gems/2.6.0/gems/railties-5.2.0/lib/rails/application/configuration.rb:172:in `database_configuration' /home/ec2-user/.rbenv/versions/2.6.7/lib/ruby/gems/2.6.0/gems/activerecord-5.2.0/lib/active_record/railtie.rb:133:in `block (2 levels) in <class:Railtie>' /home/ec2-user/.rbenv/versions/2.6.7/bin/unicorn_rails:23:in `load' /home/ec2-user/.rbenv/versions/2.6.7/bin/unicorn_rails:23:in `<top (required)>'
要因と思われるファイル
database.yml
# SQLite version 3.x # gem install sqlite3 # # Ensure the SQLite 3 gem is defined in your Gemfile # gem 'sqlite3' # default: &default # adapter: sqlite3 # pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 20 } %> # timeout: 5000 adapter: mysql2 database: application username: user password: password host: 127.0.0.1 encoding: utf8 development: <<: *default # database: db/development.sqlite3 # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default # database: db/test.sqlite3 production: <<: *default database: <%= Rails.application.credentials.db[:database] %> username: <%= Rails.application.credentials.db[:username] %> password: <%= Rails.application.credentials.db[:password] %> socket: <%= Rails.application.credentials.db[:socket] %> # database: db/production.sqlite3
secrets.yml
# Be sure to restart your server when you modify this file. # Your secret key is used for verifying the integrity of signed cookies. # If you change this key, all old signed cookies will become invalid! # Make sure the secret is at least 30 characters and all random, # no regular words or you'll be exposed to dictionary attacks. # You can use `rails secret` to generate a secure secret key. # Make sure the secrets in this file are kept private # if you're sharing your code publicly. # Shared secrets are available across all environments. # shared: # api_key: a1B2c3D4e5F6 # Environmental secrets are only available for that specific environment. development: secret_key_base: f09da86c3b58ea7e9e7a04583a57a87042a1b7b40c3eceff17a79e3150e4e5e3c33946fbb56a94a330aba88cd2f65fd8294e64b22fdce9ce437dc100c92e0910 test: secret_key_base: 54c58eb1f7871163fd1b43e1d8b924554ed4726545c871efc296b38334f050b678db07a93e85c4e18425b4d219a5b09c1c8159178adeeb73007785f4cad58495 # Do not keep production secrets in the unencrypted secrets file. # Instead, either read values from the environment. # Or, use `bin/rails secrets:setup` to configure encrypted secrets # and move the `production:` environment over there. production: secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
補足情報
・環境変数
[ec2-user@ip-10-0-0-218 application]$ rails secret 74ee9843ed4348706a038834fe0c72c272b4a6607a33518ed9b9d9c5e413d9e23ee547f678acb28d46208ffeec40ee214c2d0258d77127f439013ebaab89badd [ec2-user@ip-10-0-0-218 application]$ env | grep SECRET_KEY_BASE SECRET_KEY_BASE=74ee9843ed4348706a038834fe0c72c272b4a6607a33518ed9b9d9c5e413d9e23ee547f678acb28d46208ffeec40ee214c2d0258d77127f439013ebaab89badd [ec2-user@ip-10-0-0-218 application]$ env | grep DATABASE_PASSWORD DATABASE_PASSWORD=*Password
・rails versionは初期(rails new時)はrails5.1.6だったが途中でrails5.2.0に修正しているため、「credentials.yml」は存在せず、【rails5.2】master.keyを本番環境(AWS EC2)に設定(追加作成)する方法を参考にcredentials.yml.encを作成しています。その際のvi入力内容は以下になります。
credentials.yml
# aws: # access_key_id: 123 # secret_access_key: 345 db: database: application username: root password: *Password socket: /var/lib/mysql/mysql.sock # Used as the base secret for all MessageVerifiers in Rails, including the one protecting cookies. secret_key_base: e03e94e3d8fa517cb8fea182df0f0dae250df10c44f4a8c14b82c194c60dd8be2eaf68a1adf1e6b36078df6c47811c73a77ae8f19cf4791205b7f7a67f6620e0
・また、master.keyはec2上にファイル手動作成し中身はローカルからコピーしています
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/08/08 07:02