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.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.
测试。rb是位于/home/ceriak/ruby/, #之下的测试文件!第一行和给定的可执行权限中包含/usr/bin/ruby。但是,当我访问localhost/test时。我有一个下载窗口,可以获得源代码。
Interestingly, when I place the same script under /usr/lib/cgi-bin/
and call localhost/cgi-bin/test.rb
it works as supposed.
有趣的是,当我将相同的脚本放在/usr/lib/cgi-bin/下并调用localhost/cgi-bin/test时。它的工作原理是这样的。
(Apache2 on Ubuntu 9.10.)
在Ubuntu 9.10(输入)。
Any idea?
任何想法?
4 个解决方案
#1
7
Few things to check:
检查一些东西:
- is your file executable? You can make it executable by going
chmod +x /path/to/file
- 你的文件可执行吗?您可以通过使用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?
- 设置完配置后,您是否重新启动了Apache ?
If you did all that, it should work fine. I have this as my test.rb file:
如果你做了所有这些,它应该没问题。这是我的测试。rb文件:
#!/usr/bin/env ruby
puts <<EOS
Content-type: text/html
<html><body>hi</body></html>
EOS
#2
1
I ran in to the same situation and was able to fix it by adding the following line after AddHandler
:
我在相同的情况下运行,并通过在AddHandler后添加以下行来修复它:
Require all granted
#3
1
Double check that mod_cgi is enabled; the default Yosemite http.conf has it disabled.
再次检查是否启用了mod_cgi;默认的约塞米蒂http。conf有残疾。
#4
0
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
+ExecCGI
option - + ExecCGI选项
- Access control that allows the visitors you want (
Require all granted
, for example) - 访问控制,允许您想要的访问者(例如,要求所有的权限)
- Set a handler for CGI scripts with
AddHandler
orSetHandler
(see note below) - 使用AddHandler或SetHandler为CGI脚本设置处理程序(参见下面的说明)
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:
注意:要使用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_cgi
is 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解释器来运行脚本。
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!):
确保该文件是可执行的(由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
https://httpd.apache.org/docs/current/mod/mod_cgi.html
https://httpd.apache.org/docs/current/mod/mod_cgi.html
#1
7
Few things to check:
检查一些东西:
- is your file executable? You can make it executable by going
chmod +x /path/to/file
- 你的文件可执行吗?您可以通过使用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?
- 设置完配置后,您是否重新启动了Apache ?
If you did all that, it should work fine. I have this as my test.rb file:
如果你做了所有这些,它应该没问题。这是我的测试。rb文件:
#!/usr/bin/env ruby
puts <<EOS
Content-type: text/html
<html><body>hi</body></html>
EOS
#2
1
I ran in to the same situation and was able to fix it by adding the following line after AddHandler
:
我在相同的情况下运行,并通过在AddHandler后添加以下行来修复它:
Require all granted
#3
1
Double check that mod_cgi is enabled; the default Yosemite http.conf has it disabled.
再次检查是否启用了mod_cgi;默认的约塞米蒂http。conf有残疾。
#4
0
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
+ExecCGI
option - + ExecCGI选项
- Access control that allows the visitors you want (
Require all granted
, for example) - 访问控制,允许您想要的访问者(例如,要求所有的权限)
- Set a handler for CGI scripts with
AddHandler
orSetHandler
(see note below) - 使用AddHandler或SetHandler为CGI脚本设置处理程序(参见下面的说明)
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:
注意:要使用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_cgi
is 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解释器来运行脚本。
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!):
确保该文件是可执行的(由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
https://httpd.apache.org/docs/current/mod/mod_cgi.html
https://httpd.apache.org/docs/current/mod/mod_cgi.html