Running Ruby via Apache

I wanted to run Ruby with Apache

I wondered if I could execute Ruby files on Apache the same way I run PHP scripts, so I tried it out. No Rails here—just a connectivity check.

Environment

Procedure

I assume Apache and Ruby are already installed.

  1. Edit httpd.conf. Open C:\\Apache24\\conf\\httpd.conf in a text editor.
  2. Update directory settings. Around line 380 you will find:
<Directory "c:/Apache24/cgi-bin">
        AllowOverride None
        Options None
        Require all granted
</Directory>

Comment that block out and add the following instead:

<Directory "c:/Apache24/htdocs">
        AllowOverride All
        Options +ExecCGI
        Order allow,deny
        Allow from all
        Require all granted
</Directory>
  1. Enable the CGI handler. Around line 430 you will see:
#AddHandler cgi-script .cgi

Add the next line below it:

AddHandler cgi-script .cgi .rb

Now .rb files will be treated as CGI scripts.

  1. Place the Ruby file. Put your file under C:\\Apache24\\htdocs and write it like this:
#!C:/Ruby25-x64/bin/ruby.exe

# Specify the Ruby executable above.

puts "Content-type: text/html\n\n" # Append \n\n or Apache will throw an error (check the logs).

puts "<html><head></head><body>Hello</body></html>"

Make sure the shebang points to your Ruby executable and that you output the Content-type header.

  1. Verify. Start Apache and access http://localhost/filename.rb. If “Hello” prints, you are done!