Ubuntu Hardy - 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.
Prerequisites
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 Ubuntu Hardy - thin web server for Ruby article for more details.
Rails application
To start with, 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 modules
Apache will need the proxy and rewrite modules enabled.
Depending on your Apache install you may need to issue all the following commands:
sudo a2enmod proxy
sudo a2enmod proxy_balancer
sudo a2enmod proxy_http
sudo a2enmod rewrite
Once done, reload Apache:
sudo /etc/init.d/apache2 force-reload
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/apache2/sites-available/domain.com
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>
BalancerMember http://127.0.0.1:5000
BalancerMember http://127.0.0.1:5001
BalancerMember http://127.0.0.1:5002
</Proxy>
# Redirect all non-static requests to thin
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://thinservers%{REQUEST_URI} [P,QSA,L]
ProxyPass / balancer://thinservers/
ProxyPassReverse / balancer://thinservers/
ProxyPreserveHost on
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
# Custom log file locations
ErrorLog /home/demo/public_html/railsapp/log/error.log
CustomLog /home/demo/public_html/railsapp/log/access.log combined
</VirtualHost>
Familiar?
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.
Enable
Don't forget to enable the vhost:
sudo a2ensite domain.com
And restart Apache:
sudo /etc/init.d/apache2 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
max_conns: 1024
port: 5000
max_persistent_conns: 512
chdir: /home/demo/public_html/railsapp
environment: production
servers: 3
address: 0.0.0.0
daemonize: true
Stating thin
Now we'ce 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 0.0.0.0:5000 ...
Starting server on 0.0.0.0:5001 ...
Starting server on 0.0.0.0:5002 ...
Cool.
Navigate
All that's left is to navigate to your domain:
http://www.domain.com
Where you will be greeted with the rails welcome page.
Restart
As we created an init script for the thin servers you can restart them in the normal way:
sudo /etc/init.d/thin restart
Summary
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.
PickledOnion


Article Comments:
Peter commented Fri May 16 07:19:56 UTC 2008:
I followed this and it worked pretty well, except the thin processes keep crashing. Looking at the logs, I found the problem. See this post for details of the fix:
http://pennysmalls.com/2008/03/13/changing-to-thin-from-mongrel/
I assume this will affect anyone using this setup.
Now I just need to tame Thin's hunger for memory...
dimas priyanto commented Fri Jul 11 16:40:31 UTC 2008:
what the next steps to configure our servers, so it can handle Rails with HTTPS ..??
Victor commented Fri Aug 15 22:17:03 UTC 2008:
I get the same error as Brendan.
After finishing all of your Thin instructions, In response to:
sudo /etc/init.d/thin start
I get:
[restart] /etc/thin/mysitename.yml ... Stopping server on 0.0.0.0:5000 ... Can't stop process, no PID found in tmp/pids/thin.5000.pid Stopping server on 0.0.0.0:5001 ... Can't stop process, no PID found in tmp/pids/thin.5001.pid Stopping server on 0.0.0.0:5002 ... Can't stop process, no PID found in tmp/pids/thin.5002.pid Starting server on 0.0.0.0:5000 ... Starting server on 0.0.0.0:5001 ... Starting server on 0.0.0.0:5002 ...
Can you please take a look?
Victor
Peter commented Thu Oct 30 00:56:27 UTC 2008:
hello, I'm also getting the above error. when I do a sudo /etc/init.d/thin start everything runs as if it's normal but when goign to the browser I get a blank screen. so I decided do to a sudo /etc/init.d/thin restart and I get the below error. Please can anyone help me on this, it's been bugging me for ages now. I've even tried a ps and nothing is showing, which is understandable when looking below.
root@server51205:/home/mumpe/html/petermumford# sudo /etc/init.d/thin restart [restart] /etc/thin/petermumford.yml ... Stopping server on 83.170.93.206:5000 ... Can't stop process, no PID found in tmp/pids/thin.5000.pid Stopping server on 83.170.93.206:5001 ... Can't stop process, no PID found in tmp/pids/thin.5001.pid Stopping server on 83.170.93.206:5002 ... Can't stop process, no PID found in tmp/pids/thin.5002.pid Starting server on 83.170.93.206:5000 ... Starting server on 83.170.93.206:5001 ... Starting server on 83.170.93.206:5002 ...
Hope to here from someone? Peter..
Peter commented Thu Oct 30 13:34:41 UTC 2008:
Ok, so I've got rails working with Thin. I had to change the port numbers from 300* to 500*. The problem I have now is when I update a file on the server nothing gets updated. I have to restart Thin (sudo /etc/init.d/thin restart) for the changes to take effect.
Can anyone help and let me know why this might be happening?
Sohail commented Sat Jan 03 17:19:49 UTC 2009:
At the command:
sudo thin config -C /etc/thin/railsapp.yml -c /home/demo/public_html/railsapp/ --servers 3 -p 5000 -e production
I changed "railsapp" to my app's name ("recoveryapp") and the command fails, claiming there is no /etc/thin/recoveryapp.yml. In fact, I can't even sudo touch /etc/thin/recoveryapp.yml.
Any suggestions? Should I switch to mongrel?
val commented Fri Nov 13 18:39:30 UTC 2009:
nice & cool. what about nginx?
Steve Scruggs commented Wed Dec 09 13:03:30 UTC 2009:
Had the same errors (can't find pid's) doing the thin stop after starting with sudo thin start -C railsapp.yml you can use the same config file to stop sudo thin stop -C railsapp.yml and it finds the processes.
Hope this helps.