[英]Rails doesn't see environment variable
I am trying to move a Rails application into production but I am having a problem with Rails not seeing my environment variable.我正在尝试将 Rails 应用程序移入生产环境,但 Rails 出现问题,看不到我的环境变量。
I have the password for my database setup in my .bashrc file like我的 .bashrc 文件中有数据库设置的密码,例如
export APP_NAME_DATABASE_PASSWORD=secretkey
In irb在 irb
ENV["APP_NAME_DATABASE_PASSWORD"]
returns secretkey
.返回
secretkey
。
Using使用
RAILS_ENV=production rails c
and just只是
rails c
returns secretkey
but when starting the application I get返回
secretkey
但在启动应用程序时我得到
Access is denied (using password: NO)
I am using a slightly modified version of the init script on " How To Deploy a Rails App with Unicorn and Nginx on Ubuntu 14.04 " to start unicorn.我正在使用“ 如何在 Ubuntu 14.04 上使用 Unicorn 和 Nginx 部署 Rails 应用程序”中的 init 脚本的稍微修改版本来启动 unicorn。
It is being hosted on Ubuntu Server 14.04.它托管在 Ubuntu Server 14.04 上。
export APP_NAME_DATABASE_PASSWORD=secretkey
is most likely scoping your environment variable to a bash process. export APP_NAME_DATABASE_PASSWORD=secretkey
很可能将您的环境变量范围限定为 bash 进程。 Since Unicorn doesn't run as a child of bash process, it doesn't have access to this environment variable.由于 Unicorn 不作为 bash 进程的子进程运行,因此它无权访问此环境变量。
I'd recommend storing your ENV vars in a single place, such as application.yml
and loading them into your ruby environment at the start of your application.我建议将您的 ENV 变量存储在一个地方,例如
application.yml
并在您的应用程序开始时将它们加载到您的 ruby 环境中。 There are some great tools that do this.有一些很棒的工具可以做到这一点。 I'd recommend looking into Figaro: https://github.com/laserlemon/figaro .
我建议查看 Figaro: https : //github.com/laserlemon/figaro 。
Here's another post relevant to your question: Re-source .bashrc when restarting unicorn?这是与您的问题相关的另一篇文章: Re-source .bashrc when restarting unicorn?
I had a similar problem.我有一个类似的问题。 A simple solution for you would be to run:
一个简单的解决方案是运行:
export RAILS_ENV=production && rails c
The reason is that when you do not use the export, the rails can not see the environment variable but you see its value when you execute echo
on the console.原因是当你不使用export的时候,rails是看不到环境变量的,但是在控制台执行
echo
时候可以看到它的值。
Here is another option:这是另一种选择:
http://railsguides.net/how-to-define-environment-variables-in-rails/
http://railsguides.net/how-to-define-environment-variables-in-rails/
Put your environment variables in local_env.yml
, and then put this in application.rb
将您的环境变量放入
local_env.yml
,然后将其放入application.rb
config.before_configuration do
env_file = File.join(Rails.root, 'config', 'local_env.yml')
YAML.load(File.open(env_file)).each do |key, value|
ENV[key.to_s] = value.to_s
end if File.exists?(env_file)
end
For the ones using zsh:对于使用 zsh 的用户:
echo 'export APP_NAME_DATABASE_PASSWORD=value' >> ~/.zshenv
source ~/.zshenv
Check if it has really been set using:使用以下命令检查它是否确实已设置:
echo $APP_NAME_DATABASE_PASSWORD # value
For running rails applications you might also want to do spring stop
.对于运行 rails 应用程序,您可能还需要执行
spring stop
。
或者你忘记安装gem 'dotenv-rails'
¯_(ツ)_/¯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.