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

时间:2008-12-03 22:55:48

标签: perl fastcgi movabletype mod-fcgid

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

我似乎无法在fcgid下运行一个简单的Perl脚本。我创建了一个简单的“Hello World”应用程序,并包含this previous question中的代码来测试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";
}

从命令行运行时,它打印“plain old boring ...”当通过http请求调用apache时,我收到500 Internal Server错误,脚本输出打印到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

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

  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>

4 个答案:

答案 0 :(得分: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);
}

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

答案 1 :(得分:3)

问题是“Content-Type”标头是在请求循环之外发送的。您必须为每个请求打印“Content-Type”标头。如果你移动

  

print“Content-type:text / html \ n \ n”;

到请求循环的顶部它应该解决问题。

此外,您需要循环请求,否则您将无法获益,因此请按照第一张海报的示例:

my $request = FCGI::Request();
while($request->Accept() >= 0) {
  print("Content-type: text/html\n\n");
}

答案 2 :(得分:2)

Movable Type使用CGI :: Fast for FastCGI。典型的FastCGI脚本在循环中运行,如mat所述。使用CGI :: Fast的循环如下所示:

#!/usr/bin/perl

use strict;
use CGI::Fast;

my $count = 0;
while (my $q = CGI::Fast->new) {
    print("Content-Type: text/plain\n\n");
    print("Process ID: $$; Count is: " . ++$count);
}

我在安装了FCGI和CGI :: Fast模块的服务器上测试了这个脚本,并按照您的预期计算增量。如果进程id发生更改,count将返回1,然后在该进程内递增。当然,每个进程都有自己的可变空间。

对于MT,启用FastCGI的问题是将cgi脚本重命名(或符号链接)为'fcgi'(或者为'cgi'脚本设置处理程序fcgid,但这对mt-xmlrpc.cgi不起作用,这不是' t FastCGI友好了)。您还需要向mt-config.cgi文件添加一些指令,以便它知道新的脚本名称。像这样:

AdminScript mt.fcgi
CommentsScript mt-comments.fcgi

等等。有关FastCGI和Movable Type的更多文档可在movabletype.org上找到。

答案 3 :(得分:0)

无论如何,根据您的服务器的错误日志,它看起来像FCGI正在工作,并被正确调用,但您的脚本只是没有在循环中运行,等待下一个请求出现。因此,您的测试脚本确实完成了任务 - 报告是否配置了FastCGI。所以现在你应该能够重新配置MT以使用FastCGI。