Search code examples
ruby-on-railsshellunixenvironment-variablesunicorn

Passing many environment variables to Rails application ran by Unicorn


I need to pass a large number of environment variables to Rails application ran by Unicorn web server. The sample unicorn init script has the following lines:

APP_ROOT=/home/x/my_app/current
<...>
INIT_CONF=$APP_ROOT/config/init.conf
<...>
test -f "$INIT_CONF" && . $INIT_CONF

So I created a $APP_ROOT/config/init.conf, put all my variables there like this:

VAR1=value1
VAR2=value2

I even made this file executable (not sure if it is necessary)

And restarted Unicorn. But ENV["VAR1"] returns nothing in my application...

Is it supposed to work this way? If yes, what am I doing wrong? If no, then how can I pass many env vars into Rails app in a clean way? (without polluting global environment or putting all of them in the command line)

Update My investigation showed that shell file like this:

. init.conf
echo $VAR1

works as expected. But this one:

. init.conf
ruby -e "puts ENV['VAR1']"

does not. So . imports code into the script but env vars set this way are not transferred further.


Solution

  • You probably have to "export" the variables from within the config file. Does it work if you put

    export VAR1=value1
    export VAR2=value2
    

    into the config file?