Running Ruby scripts in Apache2
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.
Tags: apache, cgi, linux, programming, ruby
March 29th, 2009 at 10:54 pm
I have an gutsy ubuntu, with installed apache2 and ruby,
mod-ruby is installed, but the test isn’t successfully ran.
Can you help me???
Thanks in advantage
Bilbo