Running Rails on mod_ruby

Posted by Shugo Maeda Wed, 03 Aug 2005 05:34:00 GMT

I implemented Apache::RailsDispatcher to run Rails applications on mod_ruby. You have to use the SVN HEAD version of mod_ruby to use it.

Apache::RailsDispatcher can run multiple applications in the same process. It works like this:

  • require loads libraries into the top level, and they are shared with all applications.
  • require_dependency loads libraries into an anonymous module for each application.
  • In the development environment, the anonymous module is orphaned on each request. So required_dependency loads libraries every time.
  • In the production environment, the same anonymous module is used for the same application. So required_dependency loads libraries only at once.
  • Rails configurations such as ActiveRecord::Base.colorize_logging are reset on each request.

This hack is just a workaround until YARV supports multiple VM instances. We can get it in the near future, I hope.

To use Apache::RailsDispatcher, you have to write the following configuration in httpd.conf.

RubySafeLevel 0
# If you use RubyGem
# RubyRequire rubygems
RubyRequire apache/rails-dispatcher
RubyTransHandler Apache::RailsDispatcher.instance
<Location /appname>
  SetHandler ruby-object
  RubyHandler Apache::RailsDispatcher.instance
  RubyOption rails_uri_root /appname
  RubyOption rails_root /path/to/rails/root
  RubyOption rails_env production
</Location>

Please note that you can't override exinting classes like this:

class Array
  def cycle()
    self.each_with_index {|o, i| yield(o, %w(odd even)[i % 2])}
  end
end

You should prepend Object:: to the class name:

class Object::Array
  def cycle()
    self.each_with_index {|o, i| yield(o, %w(odd even)[i % 2])}
  end
end

This behaivour is same as Kernel.load(filename, true). If you don't like this, please convince Matz to change it.

Leave a comment, View comments, View trackbacks

Your Comments.

Leave your own response

Other posts about this post.

This post has been discussed on the following web sites / blogs. If you wish to trackback to this post please use the following trackback address: http://blog.shugo.net/articles/trackback/2

Spread the word.

Shugo's Blog supports RSS (Real Simple Syndication), and Trackbacks from other blogs.

RSS feed for this post Trackback URI

Your Reply

Comment Form.

Fields denoted with a "*" are required.

You may also like to leave your email or website.