为什么我的简单 fastCGI Perl 脚本会失败?

sst*_*eau 5 perl fastcgi mod-fcgid movabletype

我不是 Perl 世界的人,所以其中一些对我来说是新的。我正在运行 Ubuntu Hardy LTS,并安装了 apache2 和 mod_fcgid 软件包。我想让 MT4 在 fcgid 而不是 mod-cgi 下运行(它似乎可以在普通的 CGI 下运行)。

我似乎无法让一个简单的 Perl 脚本在 fcgid 下运行。我创建了一个简单的“Hello World”应用程序,并包含了上一个问题中的代码来测试 FCGI 是否正在运行。

我将脚本命名为 HelloWorld.fcgi(当前 fcgid 设置为仅处理 .fcgi 文件)。代码:

#!/usr/bin/perl

use FCGI;

print "Content-type: text/html\n\n";
print "Hello world.\n\n";
my $request = FCGI::Request();
if ( $request->IsFastCGI ) { 
    print "we're running under FastCGI!\n";
} else { 
    print "plain old boring CGI\n";
}
Run Code Online (Sandbox Code Playgroud)

当从命令行运行时,它会打印“plain old Boring...”当通过对 apache 的 http 请求调用时,我收到 500 内部服务器错误,并且脚本的输出被打印到 Apache 错误日志中:

Content-type: text/html

Hello world.

we're running under FastCGI!
[Wed Dec 03 22:26:19 2008] [warn] (104)Connection reset by peer: mod_fcgid: read data from fastcgi server error.
[Wed Dec 03 22:26:19 2008] [error] [client 70.23.221.171] Premature end of script headers: HelloWorld.fcgi
[Wed Dec 03 22:26:25 2008] [notice] mod_fcgid: process /www/mt/HelloWorld.fcgi(14189) exit(communication error), terminated by calling exit(), return code: 0
Run Code Online (Sandbox Code Playgroud)

当我运行相同代码的 .cgi 版本时,它工作正常。知道为什么脚本的输出会进入错误日志吗?Apache 配置是默认的 mod_fcgid 配置加上 VirtualHost 指令中的:

  ServerName test1.example.com
  DocumentRoot /www/example

  <Directory /www/example>
    AllowOverride None
    AddHandler cgi-script .cgi
    AddHandler fcgid-script .fcgi
    Options +ExecCGI +Includes +FollowSymLinks
  </Directory>
Run Code Online (Sandbox Code Playgroud)

mat*_*mat 3

我使用 CGI::Fast 比 FCGI 更多,但我认为想法是相同的。快速 cgi 的目标是加载程序一次,并针对每个请求循环迭代。

FCGI 的手册页显示:

use FCGI;

my $count = 0;
my $request = FCGI::Request();

while($request->Accept() >= 0) {
    print("Content-type: text/html\r\n\r\n", ++$count);
}
Run Code Online (Sandbox Code Playgroud)

这意味着,您必须先发出Accept请求,然后才能将任何内容打印回浏览器。