CentOS - thin web server for Ruby

There are variety of options open to the sysadmin when serving Ruby applications.

One of them is thin. This is a 3rd party web server that is proxied to from the main web server (similar to mongrels in a general setup). Let's take a look at installing thin.


Prerequisites

I am assuming you have Ruby and Rubygems installed on your Slice. If you don't, please see the CentOS Ruby on Rails article.

Installation

Thin is a rubygem and so installation couldn't be easier:

sudo gem install thin

On the test Slice with a basic rubygems and Rails installation, the process installed the following gems:

rack-0.9.1
eventmachine-0.12.6
daemons-1.0.10
thin-1.0.0

Thin basics

There will be separate articles for proxying to thin from different web servers (Apache, Nginx, etc).

As such, we'll only look at the basics of thin, leaving Virtual Host configurations for later.

To determine the thin verion:

thin -v

As is often the case in the wacky world of the developer, each version has a unique name:

thin 1.0.0 codename That's What She Said

Starting and stopping

Starting thin requires you to navigate to the rails directory and issue this command:

thin start -d

The '-d' option runs it in the background. If you omitted the '-d' option, it would act in a similar manner to the webbrick server and requires an 'open' terminal. In that case, a standard 'Ctl -C' would kill the process.

In any case the resulting output will be shown as follows:

!! Ruby 1.8.5 is not secure please install cgi_multipart_eof_fix:
   gem install cgi_multipart_eof_fix

We need to follow the instructions and install a security fix for Ruby v.1.8.5 before thin will run correctly on our slice.

gem install cgi_multipart_eof_fix

Now let's try to start thin again:

thin start -d

To stop thin, you don't need to worry about finding the PID or searching for something to kill:

thin stop

Environment

The default environment for thin is development. To start it in a production environment is easy:

thin start -d -e production

Cluster

It would be relatively unusual for a Rails application to only need 1 ruby server, so to start a cluster of 3 would require this:

thin start --servers 3

The output shows 3 servers being started sequentially from port 3000 (the default port).

Stopping the cluster is just as easy:

thin stop --servers 3

Again, the output is very clear: a quit signal is sent to each PID.

Runlevels

You can add thin to a runlevel (/etc/init.d/) with ease.

To start with you need to create the script:

sudo thin install

Then add the script to the default runlevels:

sudo /sbin/chkconfig --level 345 thin on

We can check that thin is set to run on the run levels specified by issuing:

sudo /sbin/chkconfig --list thin

The output confirms the process:

thin             0:off   1:off   2:on    3:on    4:on    5:on    6:off

Excellent.

Now we need to define which (if any) of the rails applications to start on a reboot.

Just for example, assume I have a rails application located here:

/home/demo/public_html/testapp/

I want to start 3 thin servers and be in production mode when they are started:

sudo thin config -C /etc/thin/testapp.yml -c /home/demo/public_html/testapp/  --servers 3 -e production

Have a look at the file that was created:

cat /etc/thin/testapp.yml

The contents:

pid: tmp/pids/thin.pid
log: log/thin.log
timeout: 30
port: 3000
max_conns: 1024
max_persistent_conns: 512
environment: production
chdir: /home/demo/public_html/testapp
require: []

address: 0.0.0.0
servers: 3
daemonize: true

As you can see, there are several options that can be tweaked by hand if needed.

Note the server numbers and environment are exactly as we set them. You can, of course, add as many or as few options to the command as you require, such as port numbers and so on.

When the Slice is rebooted, the 3 thin servers will now start automatically.

More

As with most applications, there is more than I can go into here but please do check out all the options that are available to you:

thin --help

Summary

Thin is an established method of serving Ruby on Rails applications. I hope this introduction outlines how easy and sysadmin friendly thin actually is.

To see how to proxy to the thin web server from Apache or Nginx, please see the next few articles.

Article Comments:

Jared commented Sun May 24 04:03:18 UTC 2009:

When I first tried to install thin I found out that it needed rack.

gem install rack

Diarmid commented Sun Jul 19 18:54:10 UTC 2009:

When I attempt to start thin I get the following text, though it does start ok.

Ruby version is not up-to-date; loading cgimultiparteof_fix

How do I update the Ruby version? It is currently 1.8.5

DaveMiller commented Mon Feb 13 17:18:43 UTC 2012:

sudo /sbin/chkconfig --level 345 thin on

Results in error reading information on service thin: No such file or directory

Fix is to move the service from rc.d, where it installs, to init.d:

sudo mv /etc/rc.d/thin /etc/rc.d/init.d/thin

And then

sudo /sbin/chkconfig --level 345 thin on

venkatesh commented Fri Dec 28 07:43:14 UTC 2012:

Before #thin s I add thin to Gemfie, and it works! Thank you!

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)