Search code examples
apacheperlapache2cgifastcgi

CGI script (Perl) running forever under Apache with fastcgi_module


I'm trying to run cgi scripts in Apache2 (Apache/2.4.6 (Ubuntu)) using fastcgi_module. This is the virtual host I set up

<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>

And this is the Perl script (properly 775'ed) I created to run the tests (/home/noobeana/CGI/test.pl):

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

The path to the Perl executable is indeed /usr/bin/perl and everything else looks fine, but when I open http://cgi.local:8080/test.pl in a browser the script is run forever - I have to stop Apache to force an exit. Also, the print is being output to Apache's error log (not the browser), which displays a multitude of the following lines for as long as the script is running:

[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'

I'm not sure whether the two problems (the print not being output in the browser and the script not terminating) are related or not.


Solution

  • What you're trying to do isn't possible. fastcgi_module can only run scripts that implement the FastCGI interface, which is not supported by the script you've written. Instead, fastcgi_module is repeatedly trying to start your "FastCGI" script, seeing it print some stuff and exit immediately - which FastCGI scripts shouldn't do - and scratching its head wondering what it's doing wrong.

    A simple script that does implement the correct interface can be implemented using the CGI::Fast module:

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

    (The FastCGI protocol is somewhat complex, so there's no reasonable way to implement it without using a module.)