18

I want to use Ruby in Apache through CGI. I have the following in my configuration file:

DocumentRoot /home/ceriak/ruby

<Directory /home/ceriak/ruby>
    Options +ExecCGI
    AddHandler cgi-script .rb
</Directory>

test.rb is a testfile placed under /home/ceriak/ruby/, #!/usr/bin/ruby included on the first line and given executable permissions. Still, when I visit localhost/test.rb I get a download window and can obtain the source code.

Interestingly, when I place the same script under /usr/lib/cgi-bin/ and call localhost/cgi-bin/test.rb it works as supposed.

(Apache2 on Ubuntu 9.10.)

Any idea?

0

4 Answers 4

7

Few things to check:

  • is your file executable? You can make it executable by going chmod +x /path/to/file
  • did you output the correct Content-type?
  • is there a blank newline between your headers and your output?
  • did you restart Apache after setting the configuration?

If you did all that, it should work fine. I have this as my test.rb file:

#!/usr/bin/env ruby

puts <<EOS
Content-type: text/html

<html><body>hi</body></html>
EOS
Sign up to request clarification or add additional context in comments.

Comments

1

I ran in to the same situation and was able to fix it by adding the following line after AddHandler:

Require all granted

Comments

1

Double check that mod_cgi is enabled; the default Yosemite http.conf has it disabled.

Comments

1

To sum up all of the fine advice in these answers and your question itself (I had to do every single one of these things since I was starting from a blank slate):

httpd.conf

Set up the CGI directory with:

  • The +ExecCGI option
  • Access control that allows the visitors you want (Require all granted, for example)
  • Set a handler for CGI scripts with AddHandler or SetHandler (see note below)

Example:

<Directory /home/ceriak/ruby>
    Options +ExecCGI
    AddHandler cgi-script .rb
    Require all granted
</Directory>

Note: to use CGI without having to use a specific file extension like .rb, you can use SetHandler instead:

SetHandler cgi-script

Now everything in the directory will be treated as a CGI script, which is likely what you want anyway and you can leave the extensions off, which may look nicer and/or not inform visitors of the underlying technology: http://example.com/test

Finally, check that mod_cgi is enabled (where ${modpath} is correct for your system):

LoadModule cgi_module ${modpath}/mod_cgi.so

Don't forget to restart Apache after making your changes. On Slackware, for example, we do this:

$ sudo /etc/rc.d/rc.httpd restart

The script

Don't forget the she-bang (#!) to run the script with the Ruby interpreter.

Output a Content-type, a newline, and then the body of your response:

#!/usr/bin/env ruby

puts "Content-type: text/html"
puts
puts "<html><body>Hello World!</body></html>"

Make sure the file is executable (by Apache!):

$ chmod +x /home/ceriak/ruby/test.rb

These two Apache documents are very helpful:

https://httpd.apache.org/docs/2.4/howto/cgi.html

https://httpd.apache.org/docs/current/mod/mod_cgi.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.