Set SECRET_KEY_BASE in production using a .env file

此生再无相见时 提交于 2019-12-12 14:22:48

问题


I've a .env file in my root folder in production. This file defines SECRET_KEY_BASE which is used in config/secrets.yml. The problem is that I can't manage to load my .env file before my config/secrets.yml. I've tried using the dotenv gem without success.

Any idea on how todo this in production?

I don't want to set it globally for my webmaster user on the production server. The SECRET_KEY_BASE value should only be accessable for by application.

I'm using rails 4.1.


回答1:


I too use dotenv gem. It works for me in almost all case.

This is my configuration of dotenv gem (yes, i put dotenv in Gemfile). I just create an aaaaa.rb initializer file.

config/initializers/aaaaaa.rb
#obscure name because rails load initializers/* files based on alphabets 
 require 'dotenv'
 Dotenv.load  

And, cases which it doesn't, i do this this finally in config/boot.rb file

ENV["SECRET_KEY_BASE"] = "foobar"



回答2:


I was also having this problem. Here is how I got it to work. I followed documentation to initialize dotenv early:

# config/application.rb
Bundler.require(*Rails.groups)

Dotenv::Railtie.load

HOSTNAME = ENV['HOSTNAME']

But then I came across this error (issue #155):

gems/dotenv-rails-1.0.2/lib/dotenv/rails.rb:17:in `load': undefined method `join' for nil:NilClass (NoMethodError)

The workaround (also documented in issue #155) is to replace Dotenv::Railtie.load with:

Dotenv.load(File.expand_path("../../.env.#{Rails.env}", __FILE__))

Apparently this is only a problem when using rails 4.1.




回答3:


Was also having this problem, but manage to get it to work by having this in my secrets.yml file:

production:
  secret_key_base: ENV["SECRET_KEY_BASE"]

It worked after removing the <%= %>



来源:https://stackoverflow.com/questions/23439148/set-secret-key-base-in-production-using-a-env-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!