Setting up Ruby CGI in Apache

我与影子孤独终老i 提交于 2019-11-28 23:22:51

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

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

Require all granted

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

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!