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:
- Install Ruby Enterprise Edition
- Install nginx-passenger
- Adjust paths
- Modify the
/etc/init.d/nginx
script - Reinstall all gems for REE
- Tweak the nginx config
- Add
nginxensite
andnginxdissite
scripts to make it more ubuntu-y
$ /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.