【问题标题】:CGI script (Perl) running forever under Apache with fastcgi_moduleCGI 脚本 (Perl) 在 Apache 下使用 fastcgi_module 永久运行
【发布时间】:2014-03-05 03:58:22
【问题描述】:

我正在尝试使用fastcgi_module 在 Apache2 (Apache/2.4.6 (Ubuntu)) 中运行 cgi 脚本。这是我设置的虚拟主机

<VirtualHost *:8080>
        ServerName cgi.local
        DocumentRoot /home/noobeana/CGI
        <Directory /home/noobeana/CGI>
                AllowOverride All
                Order allow,deny
                Allow from all
                Require all granted
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                SetHandler fastcgi-script
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

这是我为运行测试而创建的 Perl 脚本(正确地 775'ed)(/home/noobeana/CGI/test.pl):

#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Hello there!<br />\n";

Perl 可执行文件的路径确实是/usr/bin/perl,其他一切看起来都很好,但是当我在浏览器中打开http://cgi.local:8080/test.pl 时,脚本会永远运行——我必须停止Apache 才能强制退出。此外,print 正在输出到 Apache 的错误日志(而不是浏览器),只要脚本正在运行,它就会显示大量以下行:

[Fri Feb 07 10:24:54.059738 2014] [:warn] [pid 4708:tid 140365322880896] FastCGI: (dynamic) server "/home/noobeana/CGI/test.pl" started (pid 4771)
Content-type: text/html

Hello there!<br />
[Fri Feb 07 10:24:54.078938 2014] [:warn] [pid 4708:tid 140365322880896] FastCGI: (dynamic) server "/home/noobeana/CGI/test.pl" (pid 4771) terminated by calling exit with status '0'
[Fri Feb 07 10:24:59.663494 2014] [:warn] [pid 4708:tid 140365322880896] FastCGI: (dynamic) server "/home/noobeana/CGI/test.pl" restarted (pid 4773)
Content-type: text/html

Hello there!<br />
[Fri Feb 07 10:24:59.665855 2014] [:warn] [pid 4708:tid 140365322880896] FastCGI: (dynamic) server "/home/noobeana/CGI/test.pl" (pid 4773) terminated by calling exit with status '0'

我不确定这两个问题(print 未在浏览器中输出和脚本未终止)是否相关。

【问题讨论】:

  • CGI 和 FastCGI 真的很老派。您应该寻找现代 Perl 框架,例如 DancerMojolicious
  • @dolmen:错误的二分法。 CGI 和 FastCGI 是接口,而不是框架。

标签: apache perl apache2 cgi fastcgi


【解决方案1】:

您尝试做的事情是不可能的。 fastcgi_module 只能运行实现 FastCGI 接口的脚本,你写的脚本不支持。相反,fastcgi_module 反复尝试启动您的“FastCGI”脚本,看到它打印一些内容并立即退出 - FastCGI 脚本不应该这样做 - 并且挠头想知道它做错了什么。

确实实现正确接口的简单脚本可以使用CGI::Fast模块实现:

#!/usr/bin/perl
use strict;
use CGI::Fast;
while (my $CGI = CGI::Fast->new) {
    print $CGI->header();
    print "Hello there\n";
}

(FastCGI 协议有些复杂,所以不使用模块就没有合理的方法来实现它。)

【讨论】:

  • 谢谢,@duskwuff。其实我只是用 Perl 做一个简单的测试,不需要复杂的东西。我只需要 CGI 脚本在 Apache 下运行。也许我应该从 FastCGI 切换到普通 CGI?如果我这样做了,你认为我的虚拟 Perl 脚本会按照我的预期运行吗?
  • 是的,如果是这种情况,你应该切换到CGI模块。
猜你喜欢
  • 2015-05-27
  • 1970-01-01
  • 2011-12-23
  • 2010-10-08
  • 1970-01-01
  • 2013-03-11
  • 1970-01-01
相关资源
最近更新 更多