Posts Tagged ‘apache’

Running Ruby scripts in Apache2

Wednesday, April 2nd, 2008

Create a directory in your webroot (e.g. /var/www/ruby/ on Debian/Ubuntu) to store and execute your Ruby scripts and add the following directives to a .htaccess file in that directory:

AddHandler cgi-script .rb
Options +ExecCGI

Note: these directives can (and should) be specified in the Apache sites configuration file if you have permission to edit it.

To test that files in the created directory are being properly executed, copy (or download) the following code into a file named test.rb within the directory:

#!/usr/bin/env ruby
require "cgi"
cgi = CGI.new("html4")
cgi.out{
  cgi.html{
    cgi.head{ "\n"+cgi.title{"Ruby is working!"} } +
    cgi.body{ "\n"+
      cgi.h1 { "Ruby is working!" } + "\n"+
      cgi.p { "Now it's time to get on with some real work." }
    }
  }
}

Change the permissions on your file so that it is executable (chmod 774 should work fine) and visit the page you created (e.g. http://example.com/ruby/test.rb) with a web browser.

Hopefully, you should see a successful Ruby test page.