Caution! - Many of these posts are creepy-old in the Ruby on Rails world (before 1.0!)
The :author => Charles Abbott now blogs here

Mongrel Cluster a quick note - and extra notes

2007-05-20   [ 0 comments ]

A few quick notes concerning Mongrel load balancing setup behind an Apache proxy server. These are a few gotcha's I encountered that should be shared with the rest of the webosphere (hopefully if you have the same problem you'll be directed here for a quick bit of help).

First, I am assuming the following: You have apache2 up and running, and you are trying to proxy requests to mongrel_rails - which you have at least one server running...

If you receive an Apache error stating that you do not have permission to access a particular directory (which normally you do, except when you try to proxy).. and you see in the apache2/error.log

client denied by server configuration: proxy:http://127.0.0.1:8000/

Or something similar... then you need to do the following. Edit apache's proxy.conf which is nicely configured right off the bat to be a pain in the butt:

sudo nano /etc/apache2/mods-available/proxy.conf

Inside you will see something like the following - make yours match this - all I did was comment out the "Deny from all" and add an "Allow from 127.0.0.1" -- this assumes that you are only proxying on the localhost.

<Proxy *>
AddDefaultCharset off
Order deny,allow
#Deny from all
Allow from 127.0.0.1
</Proxy>



Extra mongrel_cluster notes

Next lets cover a quick tutorial (more for my benefit than anything else) on exactly how do you setup the mongrel cluster. These are very specific instructions for Ubuntu - sorry I dont have the time to expand these to be very thorough or general, again I assume you already have mongrel_rails in place and working (see my previous post for a gotcha there).

Also - I put a blank line in between shell commands. If there isn't a blank line between something then consider it all to be on one line when you type it into the shell console.

Im also basing this off a particular setup that was quite different from one I had dealt with before - so be warned this may not match exactly what you have.

sudo gem install mongrel_cluster

sudo cp /var/lib/gems/1.8/gems/mongrel_cluster-0.2.1/resources/mongrel_cluster /etc/init.d/mongrel_cluster

sudo chmod +x /etc/init.d/mongrel_cluster

There you just installed mongrel_cluster then took added the mongrel_cluster command to your init folder - and prepared it for on-bootup execution. Now if you look inside the mongrel_cluster file:

nano /etc/init.d/mongrel_cluster

You will see this line:

# Gracefully exit if the controller is missing.
which mongrel_cluster_ctl >/dev/null || exit 0

If you try to run mongrel_cluster at this point (or at least on my system) -

You will not get any output from mongrel_cluster because it cannot find the mongrel_cluster_ctl script

Thats why you will also need to do this:

sudo ln -s /var/lib/gems/1.8/bin/mongrel_cluster_ctl /usr/bin/mongrel_cluster_ctl

Good, almost there - now create a directory in /etc called "mongrel_cluster" (This is the Required name!!!)

sudo mkdir /etc/mongrel_cluster

Now, go into your rails project directory (so that if you were to type ls you would see the app/ config/ log/ public/ etc... folders). Then create your mongrel_cluster config:

sudo mongrel_rails cluster::configure -e development -p 8000 -N 3 -c /var/www/rails_project_directory -a 127.0.0.1 --user www-data --group www-data

This will update the file config/mongrel_cluster.yml in your rails app. You can test it by doing "sudo mongrel_rails cluster::start" -- check that it correctly created your pid files inside the log/ directory - then do a "sudo mongrel_rails cluster::stop".

Now, link that config into your mongrel_cluster directory:

sudo ln -s /var/www/rails_project_directory/config/mongrel_cluster.yml /etc/mongrel_cluster/your_project_name.yml

Finally setup the server to autostart the mongrel_cluster upon reboot:

sudo /usr/sbin/update-rc.d -f mongrel_cluster defaults

So, now your mongrel cluster should boot up and dazzle you with its ease of setup. So - lets take a big look at that VirtualHost setup in your apache config - how, now, do you configure your VHost to support multiple mongrel servers? Not too hard, here is what you will need inside your VHost config to make it properly proxy requests to Mongrel.

ServerName www.mysite.com
ServerAlias mysite.com *.mysite.com

ProxyPass / balancer://mongrel_cluster
ProxyPassReverse / balancer://mongrel_cluster
ProxyPreserveHost on

<Proxy balancer://mongrel_cluster>
BalancerMember http://127.0.0.1:8000
BalancerMember http://127.0.0.1:8001
BalancerMember http://127.0.0.1:8002
</Proxy>

Well, I lied.. I thought this would be a quick post - but it wasn't. I guess it doesn't matter, I wont loose any sleep over it.

Until next time, happy coding =)

:author => "Charles Abbott"
Ruby and Rails - Server Administration
 

What?

Who?              Link?



Frameworks Good or Bad?   :date => "2007-10-06"
Where is ForTheCode.com Going?   :date => "2007-09-23"
Refactoring - Vital to Software Development   :date => "2007-09-23"
Mongrel Cluster a quick note - and extra notes   :date => "2007-05-20"
Linux Mongrel and Rails   :date => "2007-05-15"
form_remote_tag revisited   :date => "2007-01-07"
How To: Ubuntu 6.10 Edgy on Rails part 3   :date => "2006-12-30"
How To: Ubuntu 6.10 Edgy on Rails part 2   :date => "2006-12-24"
How To: Ubuntu 6.10 Edgy on Rails   :date => "2006-12-22"
verify ... 5.times do cycle   :date => "2006-09-25"
country_select, country_options_for_select, mail_to   :date => "2006-09-05"
Generate and Send Email in Rails   :date => "2006-08-26"
FDF Model, gsub, and send_data   :date => "2006-08-18"
Active Directory Authentication with acts_as_authenticated   :date => "2006-08-17"
Apache2 proxy with Lighttpd - FastCGI for Rails   :date => "2006-08-08"
reverse! && a simple file Upload Class   :date => "2006-07-29"
send_file - a link to download a file   :date => "2006-07-24"
Environments (production, development, test) and cache_pages   :date => "2006-07-04"
.class .methods .instance_variables   :date => "2006-06-14"
select_tag :multiple => true   :date => "2006-06-01"
FileUtils, action_controller rescues   :date => "2006-05-20"
file_field_tag, File.size, File.path, FileUtils.mv   :date => "2006-05-15"
javascript_include_tag, stylesheet_link_tag   :date => "2006-05-02"
submit_to_remote, form_remote_tag, script.aculo.us   :date => "2006-04-30"
periodically_call_remote, simple_format   :date => "2006-04-26"
observe_field - Ajax!   :date => "2006-04-21"
h method, TextHelper, sanitize(), strip_tags()   :date => "2006-04-15"
Rails API :My API [.count(), link_to, text_area :size]   :date => "2006-04-13"
Rails - HTML Select Tag   :date => "2006-04-05"
Pruning Old Sessions   :date => "2006-03-21"
If Elsif Else, and Searching Too!   :date => "2006-03-17"
SHA1 - A quick update   :date => "2006-03-15"
Initialized! good, Authorized? Great! part 2   :date => "2006-03-11"
Initialized! good, Authorized? Great!   :date => "2006-03-08"
Forms and Routing in RoR   :date => "2006-03-06"
My First RoR Post !   :date => "2006-03-05"