Setting environment variables for Phusion Passenger applications
Asked Answered
P

4

7

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.

Pleader answered 17/9, 2008 at 2:56 Comment(1)
Update: since Passenger 2.2.3, the "SetEnv directive is supported:blog.phusion.nl/2009/06/17/… properly. Technique from Ben's answer served me very well but now there's no need to use it anymore (in most cases).Pleader
F
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.

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:

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

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

Feathery answered 17/9, 2008 at 8:54 Comment(3)
Just in case someone else runs into the same problem I had: don't forget to chmod +x /usr/bin/ruby_with_env.Wharve
Since this is an old answer, I just want to confirm this answer still seems to be the recommended way of handling this issue. More references: blog.phusion.nl/2008/12/16/… and: dev.mensfeld.pl/2011/09/…Mullins
More recently looks like this has changed. Since Phusion 4.0, you can use Apache directives 'PassEnv' and 'SetEnv': modrails.com/documentation/…Leukas
D
2

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

ENV['GEM_HOME'] = '/foo'

This will change the environment variable inside this process.

Deface answered 17/9, 2008 at 4:30 Comment(0)
C
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.

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.

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

Carmacarmack answered 12/12, 2008 at 16:39 Comment(0)
C
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.

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)

Cretic answered 17/9, 2008 at 3:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.