Hosting Ruby on Rails is complicated, right? Not necessarily. Let's take a look at a fast and flexible way to serve up your Rails application.
Rails on Swiftiply / Mongrel
If you have done any reading or work with Rails you have probably heard of Mongrel. According to the Mongrel site, "Mongrel is a fast HTTP library and server for Ruby".
You may not have heard of Swiftiply yet. But you will. Well, actually I guess you just did. But you will no doubt be hearing more about it. Swiftiply "is a very fast, narrowly targetted clustering proxy". The Swiftiply website says "scaling your web applications should be easy". Sounds good to me. Let's see if it is!
We will assume that you have already properly installed and configured the following:
- OS: Ubuntu 7.10
- Ruby: 1.8.6
- RubyGems: 1.0.1
- Swiftiply: 0.6.1.1
- Mongrel: 1.1.3
- Rails: 2.0.2
Installation: It's a breeze.
I told you it was a breeze.
sudo gem install mongrel --include-dependencies
sudo gem install swiftiply --include-dependencies
Configuration: Piece of cake.
Let's go ahead and configure Swiftiply first. Swiftiply does not install a configuration file for you, so you are creating a new blank file in the following step:
Here is the configuration that I used:
sudo vim /etc/swiftiply.conf
Amazingly that is all that is required in the way of configuration. If you wanted to serve up other applications on other domains or sub-domains, simply repeat the "map:" section for each.
cluster_address: 123.45.678.9 # Use your IP here
outgoing: 127.0.0.1:30000 # Where Mongrels will connect
docroot: /var/www/sample.com/current # Rails root
Start up Swiftiply with:
With Swiftiply running, now all you need to do is start Mongrel(s) from within the directory of the Rails app you wish to serve. We let the system know that we want to use the special Swiftiply version of Mongrel with the "env SWIFT=1" directive. We also tell them to connect to the IP and port that you defined in your swiftiply.conf, like so:
swiftiply -c /etc/swiftiply.conf
env SWIFT=1 mongrel_rails start -d -a 127.0.0.1 -p 30000
You may also wish to include the directive "-e production", but I ran out of room on the line! ;)
Now if you want multiple mongrels serving up your app, just start another one up exactly the same. Same IP and port.
And now... your done.
And that is it. You have your basic Swiftiply / Mongrel stack setup.
Keep your eyes on Swiftiply as there will be some exciting new features be adding in the coming weeks and months.
With a blank Rails 1.2.5 application I show the following memory usages in "top":
Ruby (Mongrel): 47M
For some statistics on performance of Swiftiply, please see the post "Testing Various Configurations of Rails, Merb, Swiftiply and Nginx" on Webficient.
Stuff To Know
Here are a couple things to remember about Swiftiply as you are setting it up:
- Each application you wish to serve will be linked to ONE port.
- All mongrels serving that application will connect to the exact same ONE port.
- Even if you have (n) mongrels, they all connect to the exact same ONE port per application.
- Swiftiply can serve multiple applications, just repeat the part from "map:" down in the swiftiply.conf file assigning your new application a single unique port.