Ubuntu, Apache, Rails and Thin walk into a bar...
So I recently encountered a situation where passenger refused to work with apache. Needless to say there were custom distros and untested mods involved in the stack, failing which passenger plays nicely with apache.
After a day of fretting and apt-fixing-in-circles, I decided to use a different app server for my rails app. Enter thin.
Having now setup thin in production once (and passenger many times) there is a sense of concern creeping up on me. There are enough articles that state / claim that thin out performs every other rack server under the sun and perhaps it does (haven't had time to benchmark myself). The major concern is for the methods for handling process and thread spawning, controlling memory leaks and restarting - all of which impacts the servers' ability to scale.
In the thin configuration I detail out below, apache2 does most of the heavy lifting with regards to load balancing, request queuing, etc. I see no way to get this solution to auto-scale the number of thin instances running as per demand. With that in mind, and little other options, here are the steps taken to get thin running with apache.
Step 1: Prerequisites
If you are reading this, I assume you have already installed RVM, a ruby of your choosing, a database of your choosing and our trusted friend apache2. If not, please get this stuff setup before moving to step 2.
Step 2: Thin
Install the gem
gem install thin
Test it out
thin -v thin 1.0.0 codename That's What She Said
Step 3: Runlevels for Thin
To make sure that your application starts up when the server is restarted and you have those oh-so-familiar start / stop / restart commands at your beck-n-call.
thin install sudo /usr/sbin/update-rc.d -f thin defaults
The first line basically just outputs a file and makes a couple of directories and the second adds the file to the many different run levels in Ubuntu.
Step 4: Rails and Thin
Now we need to configure thin for each individual rails application that we want it to run. Below is an example for a single app. For more information about configuration options in thin please Google the documentation. This command will simply create the file /etc/thin/myapp.yml.
thin config -C /etc/thin/myapp.yml -c /path/to/myapp/ --servers 3 -p 5000 -e production
Step 5: Preparing Apache
Because we will be using reverse proxies to get apache to hand off requests to the thin servers, we will need some additional modules to be enabled.
sudo a2enmod proxy sudo a2enmod proxy_balancer sudo a2enmod proxy_http sudo a2enmod rewrite
Please restart apache once all these 4 are installed.
Step 6: The Virtual Host
This is an example vhost file that you can use. Please note that the number of servers you specified in step 3 should match the number of balance members you have in your vhost, and the starting ports must match and increment by one.
<VirtualHost *:80> ServerName domain.com DocumentRoot /path/to/myapp/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] # Custom log file locations ErrorLog /path/to/where/you/want/error.log CustomLog /path/to/where/you/want/access.log combined </VirtualHost>
Step 7: The Finale
Start up thin, restart apache and visit your website.
sudo /etc/init.d/thin start sudo /etc/init.d/apache2 restart



















