How do I get Rails to read environment variables?

时间:2017-04-10 03:00:13

标签: ruby-on-rails nginx environment-variables ruby-on-rails-5 unicorn

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?

3 个答案:

答案 0 :(得分:1)

  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

答案 1 :(得分:0)

要声明env变量,您可以将其添加到主文件夹中的SECRET_KEY_BASE=secretbase .bashrc或.bash_profile,或导出shell中的SECRET_KEY_BASE=secretbase

现在第二点,要在yml文件中使用它,如果您不想使用任何其他Gem,则应将yml文件的扩展名更改为yml.erb。因为Rails需要要知道你在yml文件中使用ruby代码。<%= %>'是一种在Rails中调用var和方法的方法,所以扩展应该是yml.erb

答案 2 :(得分:0)

这是rails读取env变量<%= ENV["SECRET_KEY_BASE"] %>的方式 要修复错误,您应该提供SECRET_KEY_BASE的值。 为此,您可以使用以下任何方法 1.只需使用shell导出。 export SECRET_KEY_BASE=secretvalue 2.在config/中创建.yml并加载application.rb 3.使用像figaro, dotenv

这样的宝石