在 Apache 中设置 Ruby CGI
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2245634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting up Ruby CGI in Apache
提问by Joó ádám
I want to use Ruby in Apache through CGI. I have the following in my configuration file:
我想通过 CGI 在 Apache 中使用 Ruby。我的配置文件中有以下内容:
DocumentRoot /home/ceriak/ruby
<Directory /home/ceriak/ruby>
Options +ExecCGI
AddHandler cgi-script .rb
</Directory>
test.rbis a testfile placed under /home/ceriak/ruby/, #!/usr/bin/rubyincluded on the first line and given executable permissions. Still, when I visit localhost/test.rbI get a download window and can obtain the source code.
test.rb是一个放置在 下的测试文件/home/ceriak/ruby/,#!/usr/bin/ruby包含在第一行并赋予可执行权限。尽管如此,当我访问时,localhost/test.rb我会得到一个下载窗口并可以获得源代码。
Interestingly, when I place the same script under /usr/lib/cgi-bin/and call localhost/cgi-bin/test.rbit works as supposed.
有趣的是,当我将相同的脚本放在下面/usr/lib/cgi-bin/并调用localhost/cgi-bin/test.rb它时,它会按预期工作。
(Apache2 on Ubuntu 9.10.)
(Ubuntu 9.10 上的 Apache2。)
Any idea?
任何的想法?
回答by robbrit
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?
- 你的文件是可执行的吗?你可以通过去使它可执行
chmod +x /path/to/file - 您是否输出了正确的内容类型?
- 您的标题和输出之间是否有一个空白的换行符?
- 你设置好配置后重启Apache了吗?
If you did all that, it should work fine. I have this as my test.rb file:
如果你做了所有这些,它应该可以正常工作。我有这个作为我的 test.rb 文件:
#!/usr/bin/env ruby
puts <<EOS
Content-type: text/html
<html><body>hi</body></html>
EOS
回答by jayant
I ran in to the same situation and was able to fix it by adding the following line after AddHandler:
我遇到了同样的情况,并且能够通过在以下行之后添加以下行来修复它AddHandler:
Require all granted
回答by Matt
Double check that mod_cgi is enabled; the default Yosemite http.conf has it disabled.
仔细检查 mod_cgi 是否已启用;默认的 Yosemite http.conf 已禁用它。
回答by DaveGauer
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:
使用以下命令设置 CGI 目录:
- The
+ExecCGIoption - Access control that allows the visitors you want (
Require all granted, for example) - Set a handler for CGI scripts with
AddHandlerorSetHandler(see note below)
- 该
+ExecCGI选项 - 访问控制,允许您想要的访问者(
Require all granted例如) - 使用
AddHandler或设置 CGI 脚本的处理程序SetHandler(请参阅下面的注释)
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 SetHandlerinstead:
注意:要使用 CGI 而不必使用特定的文件扩展名,例如.rb,您可以使用SetHandler:
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
现在目录中的所有内容都将被视为 CGI 脚本,这可能是您想要的,并且您可以关闭扩展,这可能看起来更好和/或不通知访问者底层技术: http://example.com/test
Finally, check that mod_cgiis enabled (where ${modpath}is correct for your system):
最后,检查是否mod_cgi已启用(${modpath}对您的系统来说是正确的):
LoadModule cgi_module ${modpath}/mod_cgi.so
Don't forget to restart Apache after making your changes. On Slackware, for example, we do this:
不要忘记在进行更改后重新启动 Apache。例如,在 Slackware 上,我们这样做:
$ sudo /etc/rc.d/rc.httpd restart
The script
剧本
Don't forget the she-bang (#!) to run the script with the Ruby interpreter.
不要忘记#!使用 Ruby 解释器运行脚本的 she-bang ( )。
Output a Content-type, a newline, and then the body of your response:
输出 a Content-type、换行符,然后是响应正文:
#!/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!):
确保文件是可执行的(由 Apache!):
$ chmod +x /home/ceriak/ruby/test.rb
These two Apache documents are very helpful:
这两个Apache文档很有帮助:
https://httpd.apache.org/docs/2.4/howto/cgi.html
https://httpd.apache.org/docs/2.4/howto/cgi.html

