How to force NginX into production mode in Rails 4?

June 13, 2014 3.5k views
I am running a Rails 4 app on a VPS with Ubuntu, NginX and Unicorn. When I SSL into my server and update the app via git or run rake tasks on the database, my app always switches to `development` mode and I can't get it into `production` mode. Typing `RAILS_ENV=production` seems to have no effect at all. When I do $ rails console $ Rails.env I get > --> development all the time. What must I do to force NginX into `production` mode? Actually, I don't want Nginx to **ever** run in `development` mode. How can this be achieved? Thanks for any help.
2 Answers
It's not nginx that is running in development mode. It's your application. When you start your application with unicorn_rails, pass -E production to start it in production mode.
  • Thanks but actually I've never used unicorn_rails before. I used to just say "service unicorn restart". And it worked. When I run "unicorn_rails -E production" I get a bunch of errors like: "E, [2014-06-13T19:56:16.625524 #13919] ERROR -- : adding listener failed addr=0.0.0.0:8080 (in use)" What am I missing?
  • It’s saying something is already running on port 8080. It’s in use. If you want to use <code>service unicorn restart</code> you can probably configure it to run in production. What’s the upstart or init script you’re using look like?

Hi Andrew,

4 years on and I’m still struggling with this issue :-)

This is my upstart (?) script inside /etc/init.d:

#!/bin/sh

### BEGIN INIT INFO
# Provides:          unicorn
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the unicorn app server
# Description:       starts unicorn using start-stop-daemon
### END INIT INFO

set -e

USAGE="Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>"

# app settings
USER="rails"
APP_NAME="MyApp"
APP_ROOT="/home/$USER/$APP_NAME"
ENV="production"

# environment settings
PATH="/home/$USER/.rbenv/shims:/home/$USER/.rbenv/bin:$PATH"
CMD="cd $APP_ROOT; ~/.rbenv/bin/rbenv exec bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E production"
PID="$APP_ROOT/shared/pids/unicorn.pid"
OLD_PID="$PID.oldbin"

# make sure the app exists
cd $APP_ROOT || exit 1

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

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

case $1 in
  start)
    sig 0 && echo >&2 "Already running" && exit 0
    echo "Starting $APP_NAME"
    su - $USER -c "$CMD"
    ;;
  stop)
    echo "Stopping $APP_NAME"
    sig QUIT && exit 0
    echo >&2 "Not running"
    ;;
  force-stop)
    echo "Force stopping $APP_NAME"
    sig TERM && exit 0
    echo >&2 "Not running"
    ;;
  restart|reload|upgrade)
    sig USR2 && echo "reloaded $APP_NAME" && exit 0
    echo >&2 "Couldn't reload, starting '$CMD' instead"
    $CMD
    ;;
  rotate)
    sig USR1 && echo rotated logs OK && exit 0
    echo >&2 "Couldn't rotate logs" && exit 1
    ;;
  *)
    echo >&2 $USAGE
    exit 1
    ;;
esac

As far as I can tell the Rails environment gets set to production but I might be wrong.

Would be glad if you could have another look.

Thanks!

Have another answer? Share your knowledge.