I'm trying to get Apache to interpret my Ruby files but I don't understand how to do it. I'm not concerned with Rails right now, only Ruby. What I've found by Googling only confuses me. Any advice?


The most common way to run ruby code in a webserver environment is by having the code implement a rack interface. It is a very simple API that allows the webserver to speak to your ruby application. For Apache, the most common rack handler is passenger (aka. mod_rails and mod_rack). Almost all current ruby web frameworks (like rails, sinatra, camping, ...) connect to the webserver using a rack handler. You can use one of those frameworks, but you can also write rack apps directly without those. All ruby application servers (mongrel, thin, unicorn, rainbows, ...) implement the rack interface.

Another alternative would be to use (f)cgi, but I'd advise you to stay away from that route, as the interface and protocols are awkward and do not really provide modern management facilities.


You can execute ruby scripts with fast cgi. Look at the fcgi project for more information.


I've been trying to find the simple answer to this question for a while now, and figured out all you have to do is put the ruby script in your webserver's cgi-bin directory and load the corresponding URL. So for example, in my default CentOS 6.2/httpd install, I put a script called hello.rb in /var/www/cgi-bin/ and then loaded up http://localhost/cgi-bin/hello.rb in my browser and it executed the script and showed me what went to STDOUT (i.e. from puts, etc).

What happens here is every time the URL is loaded, the script runs completely. This is not ideal for a high volume webserver or where speed is crucial -- so something like fastCGI keeps the script running always, but you have to write the script to handle that.

hope that helps.


Try modruby.

  • I tried it. It didn't do anything and if there's any documentation, they did a great job of hiding it. – Jason Swett Jan 27 '11 at 17:35
  • That didn't work, either. Invalid command 'RubyAddPath' Is this some kind of esoteric thing nobody ever does? I'm surprised by how hard it is to get Ruby working with Apache compared to PHP. – Jason Swett Jan 27 '11 at 18:57
  • Add "LoadModule ruby_module "path_to_mod/mod_ruby.so" – alvosu Jan 27 '11 at 22:17
  • Adding AddHandler ruby-object .rbx, LoadModule ruby_module /path/to/mod_ruby, RubyRequire apache/ruby-run, and RubyHandler Apache::RubyRun.instance to httpd.conf should be enough for a simple configuration. – Chris S Feb 16 '11 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.