简体   繁体   English

phusion 乘客看不到环境变量?

[英]phusion passenger not seeing environment variables?

We are running ubuntu servers with Nginx + Phusion Passenger for our rails 3.0x apps.我们正在为我们的 rails 3.0x 应用程序运行带有 Nginx + Phusion 乘客的 ubuntu 服务器。

I have an environment variable set in /etc/environment on the test machines:我在测试机器上的 /etc/environment 中设置了一个环境变量:

MC_TEST=true

If I run a console (bundle exec rails c) and output ENV["MC_TEST"] I see 'true'.如果我运行一个控制台(bundle exec rails c)并输出 ENV["MC_TEST"] 我看到'true'。 But, if I put that same code on a page ( <%= ENV["MC_TEST"] %> ) it doesn't see anything.但是,如果我将相同的代码放在页面上( <%= ENV["MC_TEST"] %> ),它什么也看不到。 That variable does not exist.该变量不存在。

Which leads me to question:这让我提出问题:

1 - What is the proper way to get environment variables into passenger with nginx (not apache SetEnv)? 1 - 使用 nginx(不是 apache SetEnv)将环境变量放入乘客的正确方法是什么?

2 - Why does Passenger not have a proper environment? 2 - 为什么乘客没有合适的环境?

Passenger fusion v4+ enables reading of environment variables directly from bashrc file.乘客融合 v4+ 支持直接从 bashrc 文件读取环境变量。 Make sure that bashrc lives in the home folder of the user under which passenger process is executed (in my case, it was ubuntu, for ec2 linux and nginx)确保 bashrc 位于执行乘客进程的用户的主文件夹中(在我的情况下,它是 ubuntu,用于 ec2 linux 和 nginx)

Here is the documentation which goes into details of bashrc这是详细介绍 bashrc 的文档

I've the same problem with you when use passenger with nginx and nginx init script on ubuntu.在 ubuntu 上使用带有 nginx 和 nginx init 脚本的乘客时,我遇到了同样的问题。 The reason is that I use sudo service nginx restart(installed by init script) to start up nginx and原因是我使用 sudo service nginx restart(由init脚本安装)来启动nginx和
it was running by root and the root did not get your login user environment variable.它由 root 运行,root 没有获取您的登录用户环境变量。 There are two solutions for this.对此有两种解决方案。 One is running nginx manually.一种是手动运行nginx。

sudo service nginx stop
sudo -E /path/to/your/nginx

one is add env to your nginx init script一种是将 env 添加到您的 nginx init 脚本中

export MC_TEST=true

The latter solution is somehow ugly, But it works.后一种解决方案有点难看,但它有效。 And I think the better way is found a configuration to tell the init script to preserve the login user env.我认为更好的方法是找到一个配置来告诉 init 脚本保留登录用户 env。

I got another ugly solution.我得到了另一个丑陋的解决方案。

env_file = '/etc/environment'
if File.exist?(env_file)
  text = File.open(env_file).read
  text.each_line do |line|
    key, val = line.split('=', 2)
    ENV[key] = val.strip
  end
end

With nginx you can use the variable passenger_env_var to it.使用 nginx,您可以使用变量passenger_env_var。 See an example below请参阅下面的示例

passenger_env_var GEM_HOME /home/foo/.rbenv/rubygems;  
passenger_env_var GEM_PATH /home/foo/.rbenv/rubygems/gems;

So for your case所以对于你的情况

passenger_env_var MC_TEST true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Phusion乘客未读取.zshrc环境变量 - Phusion Passenger not reading .zshrc environment variables 为Phusion Passenger应用程序设置环境变量 - Setting environment variables for Phusion Passenger applications Phusion Passenger 4和nginx在Ubuntu Linux中看不到环境变量 - Phusion Passenger 4 & nginx cannot see environment variables in Ubuntu Linux 使用Shibboleth属性作为Ruby环境变量(Phusion Passenger) - Using Shibboleth Attributes as Ruby Environment Variables (Phusion Passenger) Ubuntu上的Phusion Passenger在vendor目录中未看到插件 - Phusion Passenger on Ubuntu not seeing plugin in vendors directory 耙子使用的环境值与乘载乘客设置不同 - Rake not using the same environment value as phusion passenger setting GitLab + Apache + Phusion Passenger-&gt;没有这样的文件或目录-config / environment.rb - GitLab + apache + Phusion Passenger -> No such file or directory - config/environment.rb 在Phusion Passenger上使用Capistrano设置Ruby on Rails应用程序环境 - Setting the Ruby on Rails application environment using Capistrano on Phusion Passenger Phusion Passenger使用相同的文档根设置子域的环境 - Phusion Passenger set Environment for subdomain using same document root 看到文档根目录而不是我的 rails 应用程序正在运行,Ubuntu VPS with Phusion Passenger - Seeing the document root instead of my rails app running, Ubuntu VPS with Phusion Passenger
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM