How to force NginX into production mode in Rails 4?

Posted June 13, 2014 4.6k 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.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
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= (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:


# 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

set -e

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

# app settings

# environment settings
CMD="cd $APP_ROOT; ~/.rbenv/bin/rbenv exec bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E production"

# 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
    sig 0 && echo >&2 "Already running" && exit 0
    echo "Starting $APP_NAME"
    su - $USER -c "$CMD"
    echo "Stopping $APP_NAME"
    sig QUIT && exit 0
    echo >&2 "Not running"
    echo "Force stopping $APP_NAME"
    sig TERM && exit 0
    echo >&2 "Not running"
    sig USR2 && echo "reloaded $APP_NAME" && exit 0
    echo >&2 "Couldn't reload, starting '$CMD' instead"
    sig USR1 && echo rotated logs OK && exit 0
    echo >&2 "Couldn't rotate logs" && exit 1
    echo >&2 $USAGE
    exit 1

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.