为Phusion Passenger应用程序设置环境变量

时间:2021-09-30 11:29:33

I've set up Passenger in development (Mac OS X) and it works flawlessly. The only problem came later: now I have a custom GEM_HOME path and ImageMagick binaries installed in "/usr/local". I can put them in one of the shell rc files that get sourced and this solves the environment variables for processes spawned from the console; but what about Passenger? The same application cannot find my gems when run this way.

我已经在开发中设置了Passenger(Mac OS X),它可以完美运行。后来唯一的问题是:现在我在“/ usr / local”中安装了自定义GEM_HOME路径和ImageMagick二进制文件。我可以把它们放在一个获取源代码的shell rc文件中,这解决了从控制台生成的进程的环境变量;但乘客怎么样?以这种方式运行时,相同的应用程序无法找到我的宝石。

4 个解决方案

#1


12  

I know of two solutions. The first (documented here) is essentially the same as manveru's—set the ENV variable directly in your code.

我知道两个解决方案。第一个(此处记录)与manveru的基本相同 - 直接在代码中设置ENV变量。

The second is to create a wrapper around the Ruby interpreter that Passenger uses, and is documented here (look for passenger_with_ruby). The gist is that you create (and point PassengerRuby in your Apache config to) /usr/bin/ruby_with_env, an executable file consisting of:

第二种是围绕Passenger使用的Ruby解释器创建一个包装器,并在此处记录(查找passenger_with_ruby)。要点是你创建(并将Apache配置中的PassengerRuby指向)/ usr / bin / ruby​​_with_env,这是一个可执行文件,包括:

#!/bin/bash
export ENV_VAR=value
/usr/bin/ruby $*

Both work; the former approach is a little less hackish, I think.

两者都有效;我认为,前一种方法不那么苛刻。

#2


2  

Before you do any requires (especially before requiring rubygems) you can do:

在你做任何要求之前(特别是在需要rubygems之前)你可以做:

ENV['GEM_HOME'] = '/foo'

This will change the environment variable inside this process.

这将更改此过程中的环境变量。

#3


2  

I found out that if you have root priviledges on computer then you can set necessary environment variables in "envvars" file and apachectl will execute this file before starting Apache.

我发现如果您在计算机上拥有root权限,那么您可以在“envvars”文件中设置必要的环境变量,apachectl将在启动Apache之前执行此文件。

envvars typically is located in the same directory where apachectl is located - on Mac OS X it is located in /usr/sbin. If you cannot find it then look in the source of apachectl script.

envvars通常位于apachectl所在的同一目录中 - 在Mac OS X上它位于/ usr / sbin中。如果找不到它,请查看apachectl脚本的源代码。

After changing envvars file restart Apache with "apachectl -k restart".

更改envvars文件后,使用“apachectl -k restart”重启Apache。

#4


1  

I've run into this issue as well. It appears that Passenger doesn't passthrough values set using the SetEnv apache directive - which is unfortunate.

我也遇到过这个问题。似乎Passenger没有通过使用SetEnv apache指令设置的值 - 这是不幸的。

Perhaps it might be possible to set environment variables in your environment.rb or boot.rb (assuming you're talking about a Rails app; I'm not familiar with Rack but presumably it has similar functionality)

也许有可能在你的environment.rb或boot.rb中设置环境变量(假设你在谈论Rails应用程序;我不熟悉Rack,但可能它有类似的功能)

#1


12  

I know of two solutions. The first (documented here) is essentially the same as manveru's—set the ENV variable directly in your code.

我知道两个解决方案。第一个(此处记录)与manveru的基本相同 - 直接在代码中设置ENV变量。

The second is to create a wrapper around the Ruby interpreter that Passenger uses, and is documented here (look for passenger_with_ruby). The gist is that you create (and point PassengerRuby in your Apache config to) /usr/bin/ruby_with_env, an executable file consisting of:

第二种是围绕Passenger使用的Ruby解释器创建一个包装器,并在此处记录(查找passenger_with_ruby)。要点是你创建(并将Apache配置中的PassengerRuby指向)/ usr / bin / ruby​​_with_env,这是一个可执行文件,包括:

#!/bin/bash
export ENV_VAR=value
/usr/bin/ruby $*

Both work; the former approach is a little less hackish, I think.

两者都有效;我认为,前一种方法不那么苛刻。

#2


2  

Before you do any requires (especially before requiring rubygems) you can do:

在你做任何要求之前(特别是在需要rubygems之前)你可以做:

ENV['GEM_HOME'] = '/foo'

This will change the environment variable inside this process.

这将更改此过程中的环境变量。

#3


2  

I found out that if you have root priviledges on computer then you can set necessary environment variables in "envvars" file and apachectl will execute this file before starting Apache.

我发现如果您在计算机上拥有root权限,那么您可以在“envvars”文件中设置必要的环境变量,apachectl将在启动Apache之前执行此文件。

envvars typically is located in the same directory where apachectl is located - on Mac OS X it is located in /usr/sbin. If you cannot find it then look in the source of apachectl script.

envvars通常位于apachectl所在的同一目录中 - 在Mac OS X上它位于/ usr / sbin中。如果找不到它,请查看apachectl脚本的源代码。

After changing envvars file restart Apache with "apachectl -k restart".

更改envvars文件后,使用“apachectl -k restart”重启Apache。

#4


1  

I've run into this issue as well. It appears that Passenger doesn't passthrough values set using the SetEnv apache directive - which is unfortunate.

我也遇到过这个问题。似乎Passenger没有通过使用SetEnv apache指令设置的值 - 这是不幸的。

Perhaps it might be possible to set environment variables in your environment.rb or boot.rb (assuming you're talking about a Rails app; I'm not familiar with Rack but presumably it has similar functionality)

也许有可能在你的environment.rb或boot.rb中设置环境变量(假设你在谈论Rails应用程序;我不熟悉Rack,但可能它有类似的功能)