8
votes

We are running ubuntu servers with Nginx + Phusion Passenger for our rails 3.0x apps.

I have an environment variable set in /etc/environment on the test machines:

MC_TEST=true

If I run a console (bundle exec rails c) and output ENV["MC_TEST"] I see 'true'. But, if I put that same code on a page ( <%= ENV["MC_TEST"] %> ) it doesn't see anything. 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)?

2 - Why does Passenger not have a proper environment?

4

4 Answers

7
votes

Passenger fusion v4+ enables reading of environment variables directly from bashrc file. 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)

Here is the documentation which goes into details of bashrc

4
votes

I've the same problem with you when use passenger with nginx and nginx init script on ubuntu. The reason is that I use sudo service nginx restart(installed by init script) to start up nginx and
it was running by root and the root did not get your login user environment variable. There are two solutions for this. One is running nginx manually.

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

one is add env to your nginx init script

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.

0
votes

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
0
votes

With nginx you can use the variable passenger_env_var to it. 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;