Why does my simple fastCGI Perl script fail?

sstrudeau picture sstrudeau · Dec 3, 2008 · Viewed 12.5k times · Source

I'm not of the Perl world, so some of this is new to me. I'm running Ubuntu Hardy LTS with apache2 and mod_fcgid packages installed. I'd like to get MT4 running under fcgid rather than mod-cgi (it seems to run OK with plain-old CGI).

I can't seem to get even a simple Perl script to run under fcgid. I created a simple "Hello World" app and included the code from this previous question to test if FCGI is running.

I named my script HelloWorld.fcgi (currently fcgid is set to handle .fcgi files only). Code:

#!/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";
}

When run from the command line, it prints "plain old boring..." When invoked via an http request to apache, I get a 500 Internal Server error and the output of the script is printed to the Apache error log:

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

When I run the .cgi version of the same code, it works fine. Any idea why the output of the script is going to the error log? Apache config is the default mod_fcgid config plus, in a VirtualHost directive:

  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>

Answer

mat picture mat · Dec 4, 2008

I use CGI::Fast more than FCGI, but the idea is the same, I think. The goal of fast cgi is to load the program once, and iterate in a loop for every request.

FCGI's man page says :

use FCGI;

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

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

Which means, you have to Accept the request before being able to print anything back to the browser.