Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I can't seem to get my server to load up the environment variables. In my ideal setup I would like 1 user (deployer) to serve multiple applications on a single server. Ubuntu 12.04, Nginx, Unicorn.

In my apps I have a .env file that contain all the environment variables the app needs.

Here are my failed attempts:

I start unicorn with the special shell script. I can't use source here so I don't have any idea how to load the environment variables in it.

#!/bin/sh
### BEGIN INIT INFO
# Provides:          unicorn
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Manage unicorn server
# Description:       Start, stop, restart unicorn server for a specific application.
### END INIT INFO
set -e

# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=<%= current_path %>
PID=<%= unicorn_pid %>
CMD="cd <%= current_path %>; bundle exec unicorn -D -c <%= unicorn_config %> -E <%= rails_env %>"
AS_USER=<%= user %>
set -u

# source doesn't work ...
source <%= current_path %>/.env

OLD_PIN="$PID.oldbin"

sig () {
  test -s "$PID" && kill -$1 `cat $PID`
}

oldsig () {
  test -s $OLD_PIN && kill -$1 `cat $OLD_PIN`
}

run () {
  if [ "$(id -un)" = "$AS_USER" ]; then
    eval $1
  else
    su -c "$1" - $AS_USER
  fi
}

case "$1" in
start)
  sig 0 && echo >&2 "Already running" && exit 0
  run "$CMD"
  ;;
stop)
  sig QUIT && exit 0
  echo >&2 "Not running"
  ;;
force-stop)
  sig TERM && exit 0
  echo >&2 "Not running"
  ;;
restart|reload)
  sig HUP && echo reloaded OK && exit 0
  echo >&2 "Couldn't reload, starting '$CMD' instead"
  run "$CMD"
  ;;
upgrade)
  if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
  then
    n=$TIMEOUT
    while test -s $OLD_PIN && test $n -ge 0
    do
      printf '.' && sleep 1 && n=$(( $n - 1 ))
    done
    echo

    if test $n -lt 0 && test -s $OLD_PIN
    then
      echo >&2 "$OLD_PIN still exists after $TIMEOUT seconds"
      exit 1
    fi
    exit 0
  fi
  echo >&2 "Couldn't upgrade, starting '$CMD' instead"
  run "$CMD"
  ;;
reopen-logs)
  sig USR1
  ;;
*)
  echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
  exit 1
  ;;
esac

I also tried setting the ENV values manually in the unicorn.rb configuration script. But somehow this doesn't even work!

before_exec do |server|
  ENV["START_BEFORE_EXEC"] = "Hello"
end

puts "UNICORN ENV DEBUG:"
ENV.each{ |k, v| puts "#{k}:tt#{v}" }

In the unicorn.log file I can see the environment variables but sadly not the START_BEFORE_EXEC one ... how is this possible?

EDIT: Working but not how I imagine it should work:

I finally got it working by placing some ruby code after the configuration block in application.rb. It read in the file and places it manually in ENV. There has to be a better way but for now it works. Is there a better way? I'll test it and accept an answer if ok.

share|improve this question
    
Does changing your CMD line to START_BEFORE_EXEC=Hello bundle exec unicorn ... work? –  cjc Aug 13 '12 at 2:48
    
Did this. Not displayed on the "UNICORN ENV DEBUG" output. Good idea though. Edited my question because I sort of figured out a way. But it's far from perfect. –  SpoBo Aug 13 '12 at 10:45
    
you might want to ask this over at StackOverflow, where there are more Ruby developers who may have run into this, than Sysadmins who are less familiar. –  cjc Aug 13 '12 at 11:28
    
But I think the environment variables should be passable / settable from the OS, no? What's the correct way to set the ENV variables in a Nginx / Unicorn setup? –  SpoBo Aug 13 '12 at 11:33
    
We've tended to run the unicorn bit through bluepill (so it can kick unicorn if it exceeds memory, bring it back up if it falls down, etc. The env variables are set through bluepill configuration. The nginx configuration isn't involved with this at all, as nginx just functions as a reverse proxy. –  cjc Aug 13 '12 at 11:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.