Ruby on Rails
mod_ruby

What is mod_ruby?

mod_ruby embeds the Ruby interpreter into the Apache web server, allowing Ruby CGI scripts to be executed natively. These scripts will start up much faster than without mod_ruby.”modruby.net

What is the latest version of mod_ruby?

Version 1.2.6 was released on 2006-06-15.

Using mod_ruby with Rails

Rails currently requires that you lower the \RubySafeLevel to zero. This can be done in the httpd conf with something like this:

<IfModule mod_ruby.c>
  RubyRequire apache/ruby-run
  RubySafeLevel 0
  <Files *.rb>
    SetHandler ruby-object
    RubyHandler Apache::RubyRun.instance
  </Files>
</IfModule>

update .htaccess

See the comments. At time of writing you need to change:

RewriteBase /dispatch.cgi

to:
RewriteBase /dispatch.rb

Shared interpreter limitations

mod_ruby uses a shared interpreter per Apache process, which means that multiple Rails applications on the same Apache setup with mod_ruby would share the framework classes. This doesn’t work well with the Rails model of using class-level configuration attributes, so it’s considered unsafe to use mod_ruby and Rails with more than one application running per Apache setup, because different applications may start sharing the classes.

To overcome this, use a special dispatcher by Shugo Maeda (the author of mod_ruby) – http://blog.shugo.net/articles/2005/08/03/running-rails-on-mod_ruby

Troubleshooting obscure errors with mod_ruby

Using mod_ruby you have to keep in mind that mod_ruby (like any other Apache module) shares the namespace (and libraries) with other Apache modules (such as mod_php). If you are getting messages that your Rails application cannot connect to MySQL, for instance, it has likely to do with a clash of PHP’s and ruby’s MySQL bindings, which are linked to different libraries.

An example of this is when your Rails app can connect to MySQL from CGI, native webrick and FCGI but refuses to do it under mod_ruby:


$ tail -f /var/log/httpd/error_log 

[Wed Jun 15 18:32:21 2005] [error] mod_ruby: /usr/local/lib/ruby/
gems/1.8/gems/activerecord-1.10.1/lib/active_record/
onnection_adapters/mysql_adapter.rb:39:in `real_connect':
Can't connect to MySQL server on 'localhost' (49) (Mysql::Error)

To avoid this, always take care that your Ruby bindings for databases (and any other binary libraries that Ruby gems might be using) are compiled referring to the same includes and libraries that your other Apache modules use.