May 9, 2009

Nginx with THIN

Nginx with thin is supposed to be very fast combination basically for RubyOnRails while the most advanced tour towards is making this a production ready environment. While the best affect of this flavour is generating the aspect of speedy web modes.

Just to ensure that this combination works fine, you need to have an upstream server configured provided by various web domains.

Configure Thin for your System

Each application will have a configuration file - this is similar to having a mongrel_cluster.yml in a Mongrel setup.

There are a number of options for how you configure your applications to be started/stopped - we prefer creating a directory for all Thin instances and then using the init script to control them. This is baked right in - to create the /etc/thin directory and install the init script simply run:

$ sudo thin install

Then, to configure thin to start at system boot:

on RedHat-like systems:

$ sudo /sbin/chkconfig --level 345 thin on

on Debian-like systems (Ubuntu):

$ sudo /usr/sbin/update-rc.d -f thin defaults

on Gentoo:

$ sudo rc-update add thin default

Configure Thin for your Application

To generate the config file you can run the following from within the root of your application:

$ sudo thin config -C /etc/thin/.yml -c <rails-app-root-path> --servers  -e 

Replace with the name of the configuration file, <rails-app-root-path> with the path to the root of your application, with the number of Thin processes to be started and with the environment in which to run the code. For example:

$ sudo thin config -C /etc/thin/myapp.yml -c /var/rails/myapp --servers 5 -e production

This will generate a configuration file in the /etc/thin directory we created earlier. This file will contain something similar to:

---
pid: tmp/pids/thin.pid
timeout: 30
log: log/thin.log
max_conns: 1024
require: []

environment: production
max_persistent_conns: 512
servers: 5
daemonize: true
socket: /tmp/thin.sock
chdir: /var/rails/myapp

You are now ready to start the thin service (which will start all of the Thin's configured in the /etc/thin directory):

on RedHat-like systems (you will need to substitute the relevant command to start the service on other distros):

$ sudo service thin start

Setup Nginx

If you already have Nginx installed (perhaps if you're moving over from Mongrel) then you can skip this step.

Installing From a Binary

Some operating systems have binary versions of Nginx available.

Windows

http://www.kevinworthington.com/nginx-for-windows/ (0.6.35 at the time of writing) is compiled currently for 32-bit Windows systems only and has been tested for Vista, XP, and 2000.

Ubuntu/Debian (Linux)

The version available for your setup can be determined by using

$ apt-cache showpkg nginx

Then, to install, run

$ sudo apt-get install nginx

Installing From Source

To install Nginx from source, grab the latest stable version of Nginx (0.6.35 at the time of writing) and build it:

$ curl -O http://sysoev.ru/nginx/nginx-0.6.35.tar.gz
$ tar xzvf nginx-0.6.35.tar.gz
$ cd nginx-0.6.35
$ ./configure --sbin-path=/usr/local/sbin --with-http_ssl_module
$ make
$ sudo make install
$ sudo chmod +x /usr/local/sbin/nginx

This will put the binary at /usr/local/sbin/nginx (otherwise it will live under /usr/local/nginx/bin)

The configuration file will, by default, live at /usr/local/nginx/conf/nginx.conf - you can change this when building if desired.

We also use an init script to start and stop Nginx - courtesy of Geoffrey Grosenbach and Ryan Norbauer.

Grab the init script and install on your system as a service as before.

Configure Nginx to talk to Thin

The next step is to configure Nginx. The sample configuration will setup a single site using a cluster of Thin's over the unix socket interface. To use this config, ensure the paths are correct for your system and change the hostname and you should be good to go!