CentOS - Apache, Rails and thin

Following from the thin web server for Ruby article, let's look at creating and configuring Apache to proxy to thin so we can serve our Ruby on Rails application.


To get the most out of this article you need to have a couple of things preinstalled:

Firstly, you need Apache installed (see this article) - if you don't require PHP then please feel free to leave that section out.

Secondly, you will need to have thin installed - as mentioned above, please see the CentOS - thin web server for Ruby article for more details.

Rails application

As the default permissions only allow us, the 'demo' user, to browse our home folder, let's start off by giving Apache access to this folder as well:

chmod 755 /home/demo

Now we will need a basic Ruby on Rails application. Move into your public_html folder (create one if you do not have one already):

cd ~/public_html

Then create a Rails application. We'll use the default sqlite database for this example:

rails railsapp

Apache Virtual Host

So what's the plan?

Well, for our simple application we'll set 3 thin instances running from port 5000 and in production mode.

Let's start off by creating the virtual host:

sudo nano /etc/httpd/conf/httpd.conf

The following will suffice for a basic application:

<VirtualHost *:80>
  ServerName domain.com
  ServerAlias www.domain.com

  DocumentRoot /home/demo/public_html/railsapp/public

  RewriteEngine On

  <Proxy balancer://thinservers>

  # Redirect all non-static requests to thin
  RewriteRule ^/(.*)$ balancer://thinservers%{REQUEST_URI} [P,QSA,L]

  ProxyPass / balancer://thinservers/
  ProxyPassReverse / balancer://thinservers/
  ProxyPreserveHost on

  <Proxy *>
    Order deny,allow
    Allow from all

  # Custom log file locations
  ErrorLog  /home/demo/public_html/railsapp/log/error.log
  CustomLog /home/demo/public_html/railsapp/log/access.log combined



Does that look familiar?

Well, it is pretty much the same as the virtual host used to proxy to mongrel servers.

If you think about it, we don't need anything different as all we are doing is rerouting non-static requests to thin in the same way we would for mongrels.


Don't forget to enable the vhost we created by restarting Apache:

sudo /etc/init.d/httpd reload

If you get any port and NameVirtualHost errors then please read the Apache Virtual Host article which will take you through setting up said details.

Thin configuration

Next we need to configure thin on our application.

To kill two birds with one stone, we'll go straight into creating an init.d file so the thin servers are started on a Slice reboot.

Remember we want 3 servers running sequentially from port 5000 in production mode:

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

Have a quick check of the file we created:

cat /etc/thin/railsapp.yml

the contents of which look good:

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

servers: 3
daemonize: true

Starting thin

Now we've created our init script, let's start it:

sudo /etc/init.d/thin start

The terminal output confirms the actions:

[start] /etc/thin/railsapp.yml ...
Starting server on ... 
** Ruby version is not up-to-date; loading cgi_multipart_eof_fix
Starting server on ... 
** Ruby version is not up-to-date; loading cgi_multipart_eof_fix
Starting server on ... 
** Ruby version is not up-to-date; loading cgi_multipart_eof_fix



All that's left is to navigate to your domain:


Where you will be greeted with the rails welcome page.


As we created an init script for the thin servers you can restart them in the normal way:

sudo /etc/init.d/thin restart


Setting up a virtual host to proxy to the thin web server is fairly simple.

Add to that the ease of installing and configuring thin, the whole process in very quick and very powerful.

Article Comments:

Mike commented Mon Aug 17 18:58:07 UTC 2009:

A small mistake in

sudo nano /etc/http/conf/httpd.conf

must be "httpd" not http :)

Thank you for your articles! You rock!

Computer File Recovery commented Fri Oct 23 06:40:35 UTC 2009:

I'm trying to see what Mike was talking about, but I did it exactly the way you described here and it worked like a charm. I appreciate all the effort you put into this page. It saved me a huge headache!

Want to comment?

(not made public)


(use plain text or Markdown syntax)