Nginx Passenger Rail

The following are the steps used to setup thepropshop.org, a ruby on rails application that uses nginx and passenger.

At the time of this writing Ubuntu Raring was used.


 * 1) Install Ruby 2.0

sudo add-apt-repository ppa:brightbox/ruby-ng-experimental sudo apt-get update sudo apt-get install -y ruby2.0 ruby2.0-dev ruby2.0-doc

Setup ruby 2.0 links. Select ruby 2.0 with the following command.

sudo update-alternatives --config ruby

Setup gem 2.0 links. Select gem 2.0 with the following command.

sudo update-alternatives --config gem


 * 1) Install Passenger and NGINX

sudo gem install bundle passenger sudo passenger-install-nginx-module --auto --prefix=/opt


 * 1) Create an init script

Copy the following into /etc/init.d/nginx


 * 1) ! /bin/sh


 * 1) BEGIN INIT INFO
 * 2) Provides:          nginx
 * 3) Required-Start:    $all
 * 4) Required-Stop:     $all
 * 5) Default-Start:     2 3 4 5
 * 6) Default-Stop:      0 1 6
 * 7) Short-Description: starts the nginx web server
 * 8) Description:       starts nginx using start-stop-daemon
 * 9) END INIT INFO

PATH=/opt/nginx/sbin:/sbin:/bin:/usr/sbin:/usr/bin DAEMON=/opt/nginx/sbin/nginx NAME=nginx DESC=nginx

test -x $DAEMON || exit 0

if [ -f /etc/default/nginx ] ; then . /etc/default/nginx fi
 * 1) Include nginx defaults if available

set -e

case "$1" in start)        echo -n "Starting $DESC: "        start-stop-daemon --start --quiet --pidfile /opt/nginx/logs/$NAME.pid \                --exec $DAEMON -- $DAEMON_OPTS        echo "$NAME."        ;;  stop) echo -n "Stopping $DESC: " start-stop-daemon --stop --quiet --pidfile /opt/nginx/logs/$NAME.pid \ --exec $DAEMON echo "$NAME." ;; restart|force-reload)        echo -n "Restarting $DESC: "        start-stop-daemon --stop --quiet --pidfile \                /opt/nginx/logs/$NAME.pid --exec $DAEMON        sleep 1        start-stop-daemon --start --quiet --pidfile \                /opt/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS        echo "$NAME."        ;;  reload) echo -n "Reloading $DESC configuration: " start-stop-daemon --stop --signal HUP --quiet --pidfile    /opt/nginx/logs/$NAME.pid \ --exec $DAEMON echo "$NAME." ;;     *)                N=/etc/init.d/$NAME            echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2            exit 1            ;;    esac    exit 0

sudo chmod +x /etc/init.d/nginx sudo /usr/sbin/update-rc.d -f nginx defaults sudo service nginx start

Status check. Run the following command:

passenger-memory-stats

The above command should return something similar to:

- Apache processes - Please set the APXS2 environment variable to your 'apxs2' executable's filename, or set the HTTPD environment variable to your 'httpd' or 'apache2' executable's filename.
 * WARNING: The Apache executable cannot be found.

- Nginx processes - PID  PPID  VMSize   Private  Name --- 8372 1     38.9 MB  ? nginx: master process /opt/nginx/sbin/nginx 8373 8372  39.3 MB  ? nginx: worker process
 * 1) Processes: 2
 * 2) Total private dirty RSS: 0.00 MB (?)

Passenger processes - PID  VMSize    Private  Name -- 8354 216.2 MB  ? PassengerWatchdog 8357 489.3 MB  ? PassengerHelperAgent 8362 227.8 MB  ? PassengerLoggingAgent
 * 1) Processes: 3
 * 2) Total private dirty RSS: 0.00 MB (?)


 * 1) Configure NGINX

Edit `/opt/nginx/conf/nginx.conf` to be similar to the following file. The most important section is the `server { ... }` chunk.

worker_processes 1;

events { worker_connections 1024; }

http { passenger_root /var/lib/gems/2.0.0/gems/passenger-4.0.19; passenger_ruby /usr/bin/ruby2.0;

include      mime.types; default_type application/octet-stream;

sendfile       on;

keepalive_timeout 65;

server { listen      80; server_name localhost; root /home/ubuntu/thepropshop/public; passenger_enabled on;

rack_env production; location / { rack_env production; passenger_enabled on; root /home/ubuntu/thepropshop/public; try_files $uri $uri/ /index.html; }

# redirect server error pages to the static page /50x.html #       error_page   500 502 503 504  /50x.html; location = /50x.html { root  html; }   } }


 * 1) Install Rails App

cd; hg clone https://bitbucket.org/osrf/thepropshop cd thepropshop sudo bundle install rake db:migrate RAILS_ENV=production rake assets:precompile


 * 1) Restart nginx

sudo service nginx restart


 * 1) Troubleshooting


 * 1) Turn on backtraces:

vim config/environments/production.rb:

Make the following changes

# Full error reports are disabled and caching is turned on. config.consider_all_requests_local      = true config.action_controller.perform_caching = false