8

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?

Jason Swett
  • 1,458
  • 4
  • 23
  • 37

4 Answers4

3

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.

Holger Just
  • 3,315
  • 1
  • 16
  • 23
2

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.

carillonator
  • 805
  • 3
  • 12
  • 22
1

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

Vagmi Mudumbai
  • 183
  • 1
  • 7
0

Try modruby.

alvosu
  • 8,357
  • 24
  • 22
  • 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
  • http://perfectionlabstips.wordpress.com/2008/12/23/writing-apache-handlers-with-mod_ruby/ – mattdm Jan 27 '11 at 17:38
  • 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
  • Mod_ruby is not maintained anymore and does some surprising things. You probably don't really want to use this. – Holger Just Feb 20 '11 at 12:55