问题
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