Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 3 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

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

share|improve this answer

Try modruby.

share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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