How can I execute Ruby scripts via Apache (not necessarily using Rails)?

Solution 1:

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.

Solution 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.

Solution 3:

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