简体   繁体   中英

How do I get Rails to read environment variables?

I've installed Nginx and Unicorn and trying to get it to work with my Rails 5 application on Ubuntu 14.04. I have a file in ~/myproject/config/secrets.yml which contains

development:
  secret_key_base: 547268978521e278fd572db969ce2d25cea9da7c6db4f7164942d02322177128704a4c76ed1f536dd7ac791b10ef0355b4542f21b128077657bdef156472ad81

test:
  secret_key_base: c1aae665f8ed25da55a457870526dcac878d5a3734ab2fd29b4ec32b4b31bbe2f16d3b72bf0cacf0c6c1c88e4862d35f45c2c10140d990388e5e2184459cd23e

# Do not keep production secrets in the repository,
# instead read values from the environment.
production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

Yet when I restart nginx and unicorn and attempt to visit my web page, I get this error in my unicorn logs ...

E, [2017-04-09T22:37:34.736220 #11291] ERROR -- : app error: Missing `secret_key_base` for 'production' environment, set this value in `config/secrets.yml` (RuntimeError)
E, [2017-04-09T22:37:34.736414 #11291] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/railties-5.0.2/lib/rails/application.rb:513:in `validate_secret_key_config!'
E, [2017-04-09T22:37:34.736462 #11291] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/railties-5.0.2/lib/rails/application.rb:246:in `env_config'
E, [2017-04-09T22:37:34.736491 #11291] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/railties-5.0.2/lib/rails/engine.rb:693:in `build_request'
E, [2017-04-09T22:37:34.736515 #11291] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/railties-5.0.2/lib/rails/application.rb:521:in `build_request'
E, [2017-04-09T22:37:34.736541 #11291] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/railties-5.0.2/lib/rails/engine.rb:521:in `call'
E, [2017-04-09T22:37:34.736646 #11291] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/unicorn-5.2.0/lib/unicorn/http_server.rb:562:in `process_client'
E, [2017-04-09T22:37:34.736680 #11291] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/unicorn-5.2.0/lib/unicorn/http_server.rb:658:in `worker_loop'
E, [2017-04-09T22:37:34.736709 #11291] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/unicorn-5.2.0/lib/unicorn/http_server.rb:508:in `spawn_missing_workers'
E, [2017-04-09T22:37:34.736738 #11291] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/unicorn-5.2.0/lib/unicorn/http_server.rb:132:in `start'
E, [2017-04-09T22:37:34.736767 #11291] ERROR -- : /home/rails/.gem/ruby/2.4.0/gems/unicorn-5.2.0/bin/unicorn:126:in `<top (required)>'
E, [2017-04-09T22:37:34.736793 #11291] ERROR -- : /home/rails/.gem/ruby/2.4.0/bin/unicorn:22:in `load'
E, [2017-04-09T22:37:34.736818 #11291] ERROR -- : /home/rails/.gem/ruby/2.4.0/bin/unicorn:22:in `<top (required)>'
E, [2017-04-09T22:37:34.736842 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/cli/exec.rb:74:in `load'
E, [2017-04-09T22:37:34.736867 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/cli/exec.rb:74:in `kernel_load'
E, [2017-04-09T22:37:34.736894 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/cli/exec.rb:27:in `run'
E, [2017-04-09T22:37:34.736939 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/cli.rb:332:in `exec'
E, [2017-04-09T22:37:34.736968 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
E, [2017-04-09T22:37:34.736996 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
E, [2017-04-09T22:37:34.737021 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor.rb:359:in `dispatch'
E, [2017-04-09T22:37:34.737050 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/cli.rb:20:in `dispatch'
E, [2017-04-09T22:37:34.737078 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor/base.rb:440:in `start'
E, [2017-04-09T22:37:34.737106 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/cli.rb:11:in `start'
E, [2017-04-09T22:37:34.737136 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/exe/bundle:34:in `block in <top (required)>'
E, [2017-04-09T22:37:34.737170 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/lib/bundler/friendly_errors.rb:100:in `with_friendly_errors'
E, [2017-04-09T22:37:34.737200 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/gems/bundler-1.13.7/exe/bundle:26:in `<top (required)>'
E, [2017-04-09T22:37:34.737230 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/bin/bundle:22:in `load'
E, [2017-04-09T22:37:34.737253 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/bin/bundle:22:in `<main>'
E, [2017-04-09T22:37:34.737266 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/bin/ruby_executable_hooks:15:in `eval'
E, [2017-04-09T22:37:34.737280 #11291] ERROR -- : /usr/local/rvm/gems/ruby-2.4.0/bin/ruby_executable_hooks:15:in `<main>'

In my project directory, /home/rails/myproject/.rbenv-vars, I have defined

SECRET_KEY_BASE=secretbase
APP_DATABASE_PASSWORD=apppassword

How do I get Rails to incorporate these values into the environment, or is there some other place I shoudl be putting these values?

  1. Use export SECRET_KEY_BASE=secretbase in shell
  2. Use Figaro Gem, I use it
  3. Use a local_env.yml File

More info here: rails environment variables

To declare env variable, you can add this to SECRET_KEY_BASE=secretbase .bashrc or .bash_profile in your home folder or export SECRET_KEY_BASE=secretbase in shell.

Now second point, to use it in yml file you should change extension of your yml file to yml.erb if you don't want to use any other Gem for this.because Rails need to know you are using ruby code in yml file. <%= %>' is a way to call var and methods in Rails so extension should be yml.erb .

This is how rails read env variable <%= ENV["SECRET_KEY_BASE"] %> To fix your error you should supply the value for SECRET_KEY_BASE . For this, you can use any of the following methods 1. Simply export using the shell. export SECRET_KEY_BASE=secretvalue 2. Create a .yml in config/ and load in application.rb 3. Use gems like figaro, dotenv

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM