【问题标题】:cgi perl scripts not working, only cgi bash scripts are working?cgi perl 脚本不工作,只有 cgi bash 脚本工作?
【发布时间】:2012-10-15 07:41:57
【问题描述】:

我遇到了奇怪的问题。,

命令行 cgi bash 脚本和 cgi perl 脚本正在运行,但从浏览器中只有 cgi bash 脚本正在运行,而不是 cgi perl

从浏览器访问 cgi perl 脚本后,我得到 500 Internal server error

and apache error log says

[Thu Oct 25 01:58:59 2012] [error] [client x.x.x.x] (13)Permission denied: exec of '/home/x/x/x/x/public_html/cgi-bin/test.cgi' failed
[Thu Oct 25 01:58:59 2012] [error] [client x.x.x.x] Premature end of script headers: test.cgi

我想从浏览器运行 cgi perl 脚本。

我该怎么做。

[root@www ~]# which perl
/usr/bin/perl

[root@www cgi-bin]# perl test.cgi
Content-type: text/plain

testing...

test.cgi 的来源

#!/usr/bin/perl

print "Content-type: text/plain\n\n";
print "testing...\n";

感谢您的宝贵时间。

它是一个带有 apache 2 的专用服务器。

【问题讨论】:

  • 确保 Web 服务器的用户 ID 对 test.cgi 路径中的所有内容具有读取和执行权限。
  • 试试su www-data(或任何apache的用户),然后运行脚本,看看它是否有效。
  • su apache 无法正常工作,因此我将 httpd.conf 中的用户和组更改为 cgi 文件的所有者,瞧,一切正常。非常感谢,现在我如何为用户 apache 添加权限访问 .cgi 文件
  • 现在我修改了 passwd 并为用户 apache 提供了一个 shell,我尝试了 su apache 它工作,然后我尝试执行它说“bash:/usr/bin/perl:权限被拒绝”的脚本,谢谢跨度>

标签: perl apache cgi


【解决方案1】:

您是否为 perl 脚本正确设置了权限? 将它们与您为 bash 脚本设置的权限进行比较。

也许chmod 755 会按照here 的建议提供帮助(不幸的是,仅限德语)

【讨论】:

  • 是的,我已经仔细检查了权限、所有者和组以及 chmodded +x,cgi bash 脚本仍然在工作,而 cgi perl 在浏览器中没有工作。谢谢
【解决方案2】:

正如 Barmar 所说,您可能遇到了权限问题。另外,由于您刚刚开始,这里有一个改进的测试脚本:

#!/usr/bin/perl
use strict;
use warnings;

#Useful for testing: Perl error messages, plus your die statements, will get
#sent to the browser.  Otherwise you will just see "Internal Server Error".
use CGI::Carp qw/fatalsToBrowser/;

#Always use the CGI module for your scripts.
use CGI; 

#Create simple HTML output (taken directly from CGI documentation).
my $q = CGI->new;                    # create new CGI object
print $q->header,                    # create the HTTP header
      $q->start_html('hello world'), # start the HTML
      $q->h1('hello world'),         # level 1 header
      $q->end_html;                  # end the HTML

请参阅documentation on CGI 了解更多信息。

另外,澄清一下:“脚本头过早结束”错误意味着您的脚本没有向浏览器发送任何输出。在这种情况下,这是因为您的脚本没有运行。但是,如果您的脚本运行但实际上并未发送任何输出,也可能会发生这种情况。这是一个有用的错误信息。

【讨论】:

  • 我从您的数据和 chmod +x 以及 755 中创建了新的测试脚本,但仍然出现相同的错误。它从命令行运行而不是从浏览器运行,是运行 perl cgi 脚本所需的 mod_perl ?谢谢
  • 我刚刚检查了德语文章并使用 unix 格式创建了新文档,只是为了安全起见,得到了同样的错误。谢谢
  • @AmbroseBwangatto, mod_perl 不是必需的(如果您刚刚开始,不建议这样做)。查看此链接以了解更多可能的错误来源:kurinchilamp.kurinchilion.com/2009/08/…
  • 当我在尝试 su apache 时将 httpd.conf 文件中的 USER 和 GROUP 更改为 test.cgi 文件的所有者时,问题已解决。
【解决方案3】:

检查哪个用户正在运行httpd

[root@www ~]# top | grep httpd
15607 apache    16   0  210m  19m 4228 S 23.5  0.2   0:00.52 httpd

上面的apache用户正在运行。

试试 su apache,

使用命令 whoami 确认你是 apache,

如果您还没有切换到 apache,那么这意味着没有为用户 apache 分配 shell,

现在修改 /etc/passwd 文件

找到用户 apache 并更改结尾 来自

/bin/false

to 

/bin/bash

保存 passwd 文件,然后尝试 su apache,检查 whoami, 如果您成功切换到用户 apache 则转到脚本所在的目录并尝试从那里运行它。

你会得到类似的东西

bash: /usr/bin/perl: Permission denied

这意味着用户“apache”对用户 perl 没有权限。

您需要为用户“apache”添加执行 perl 脚本的权限。

***不要忘记像以前一样更改密码文件。

您可以通过将组添加到特定用户并更改组名称 httpd.conf 来做到这一点。

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    • 2012-12-28
    相关资源
    最近更新 更多