Ubuntu Hardy - Thin Web Server for Ruby
A variety of options are available to the sysadmin when serving Ruby on Rails applications. One of them is thin, a third-party web server to which the main web server connects by proxy (similar to Mongrel in a general setup). This article provides some basic information about installing and using thin.
This article assumes that you have Ruby, RubyGems, and Ruby on Rails installed on your Cloud Server. If you don't, see the Ubuntu Hardy Ruby on Rails article.
Thin is a gem, so you install it simply by running the following command:
sudo gem install thin
On a test Cloud Server with a basic RubyGems and Rails installation, the process installed the following gems:
rack-1.5.2 daemons-1.1.9 eventmachine-1.0.3 thin-1.6.1
To determine the thin verions, run the following command:
Each version has a unique number and name:
thin 1.6.1 codename Rebel Porpoise
Starting and Stopping thin
Starting thin requires you to navigate to the rails directory and run the following command:
thin start -d
The '-d' option runs thin in the background. If you omitted the '-d' option, thin would act in a similar manner to the WEBrick server and require an "open" terminal. In that case, a standard Ctl-C would kill the process.
To stop thin, you don't need to find the PID or search for a process to kill. You simply run the the following command:
The default environment for thin is development. To start it in a production environment, run the following command:
thin start -d -e production
It would be relatively unusual for a Rails application to need only one Ruby server, so to start a cluster of three servers would require the following command:
thin start --servers 3
The output shows three 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.
You can add thin to a runlevel (/etc/init.d/).
To start with, you need to create the script:
sudo thin install
Then, you add the script to the default runlevels:
sudo /usr/sbin/update-rc.d -f thin defaults
The output confirms the process:
Adding system startup for /etc/init.d/thin ... /etc/rc0.d/K20thin -> ../init.d/thin /etc/rc1.d/K20thin -> ../init.d/thin /etc/rc6.d/K20thin -> ../init.d/thin /etc/rc2.d/S20thin -> ../init.d/thin /etc/rc3.d/S20thin -> ../init.d/thin /etc/rc4.d/S20thin -> ../init.d/thin /etc/rc5.d/S20thin -> ../init.d/thin
Now you need to define which (if any) of the Rails applications to start on a reboot.
For example, assume that you have a Rails application located in the following location:
You want to start three thin servers and be in production mode when they are started, so you run the following command:
sudo thin config -C /etc/thin/testapp.yml -c /home/demo/public_html/testapp/ --servers 3 -e production
The following file was created:
The contents of the file are as follows:
pid: tmp/pids/thin.pid log: log/thin.log timeout: 30 max_conns: 1024 port: 3000 max_persistent_conns: 512 chdir: /home/demo/public_html/testapp environment: production servers: 3 address: 0.0.0.0 daemonize: true
Note that the number of servers and the environment are exactly as set. You can add as many or as few options to the command as you require, such as port numbers and so on. Several of the options can be manually changed if necessary.
Wen the Cloud Server is rebooted, the three thin servers will start automatically.
For information about all of the options that are available to you when using thin, run the following command:
To see how to proxy to the thin web server from Apache or Nginx, please see the following articles:
© 2011-2013 Rackspace US, Inc.
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License
See license specifics and DISCLAIMER