Ruby-Enterprise Edition, Fusion Passenger, and Nginx on Ubuntu Jaunty

Posted by Tejus Parikh on July 26, 2009

I’ve got a 512MB Slicehost instance that I needed to launch a new Rails app (details coming soon). Since 512MB isn’t a whole lot of memory anymore, I wanted to optimize the services on this machine as much as possible. The most efficient setup appeared to be running nginx, Passenger-Nginx, and Ruby Enterprise Edition. It was actually pretty easy to get all this running and would have been even easier had I followed the correct order of operations. Nginx doesn’t support dynamically compiled runtime modules. Everything has to be built-in at compile time. Because of this, running:


$ sudo aptitude install nginx

will get you nothing useable besides the init script. Instead, the best approach is:
  1. Install Ruby Enterprise Edition
  2. Install nginx-passenger
  3. Adjust paths
  4. Modify the /etc/init.d/nginx script
  5. Reinstall all gems for REE
  6. Tweak the nginx config
  7. Add nginxensite and nginxdissite scripts to make it more ubuntu-y
The first two steps are pretty self-explanatory and start with the directions found here. Of course, if you are installing nginx, you want to the following instead of the command for apache:

$ /opt/ruby-enterprise/bin/passenger-install-nginx-module

Next you want to tweak the ubuntu /etc/init.d/nginx script to reflect the new paths. You need to change the two lines that read:

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

DAEMON=/usr/sbin/nginx

to

PATH=/opt/nginx/sbin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

DAEMON=/opt/nginx/sbin/nginx

Next, you need to add the Ruby Enterprise Edition interpreter to your path. Open /etc/login.defs and update the paths settings with the location of Ruby Enterprise. Installing gems is pretty straight forward. There are a few small tweaks to make to the nginx config file so that it works more like the ubuntu package. At the top of nginx.conf add/change these settings:

user www-data;

worker_processes  4;



error_log  /var/log/nginx/error.log;

pid        /var/run/nginx.pid;

In the http section, add:

    include /etc/nginx/sites-enabled/*;

Finally, it’s time to create the nginxensite and nginxdissite scripts:

#!/bin/bash



# nginxensite



if [ -z $1 ]; then

        echo 

        echo "You must specify a site name"

        exit 0

fi



NGINX_CONF=/etc/nginx

CONF_FILE="$1"

AVAILABLE_PATH="$NGINX_CONF/sites-available/$CONF_FILE"

ENABLED_PATH="$NGINX_CONF/sites-enabled/$CONF_FILE"



echo 

if [ -e $AVAILABLE_PATH ]; then

        ln -s $AVAILABLE_PATH $ENABLED_PATH



        echo "$1 has been enabled"

        echo "run /etc/init.d/nginx reload to apply the changes"        

else

        echo "$AVAILABLE_PATH does not exist"

        exit 1

fi

and

#!/bin/bash



# nginxdissite



if [ -z $1 ]; then

        echo 

        echo "You must specify a site name"

        exit 0

fi



NGINX_CONF=/etc/nginx

CONF_FILE="$1"

AVAILABLE_PATH="$NGINX_CONF/sites-available/$CONF_FILE"

ENABLED_PATH="$NGINX_CONF/sites-enabled/$CONF_FILE"



echo 

if [ -e $ENABLED_PATH ]; then

        rm $ENABLED_PATH



        echo "$1 has been disabled"

        echo "run /etc/init.d/nginx reload to apply the changes"        

else

        echo "$ENABLED_PATH does not exist, ignoring"

fi

There you go, a working nginx install for your rails apps in less than an hour.

Related Posts:

Tejus Parikh

Tejus is an software developer, now working at large companies. Find out when I write new posts on twitter, via RSS or subscribe to the newsletter: