use CGI::Fast socket_path => '9000', socket_perm => 0777, listen_queue => 50; use CGI qw/ :standard /; $COUNTER = 0; # optional, will default to STDOUT, STDERR CGI::Fast->file_handles({ fcgi_output_file_handle => IO::Handle->new, fcgi_error_file_handle => IO::Handle->new, }); while ($q = CGI::Fast->new) { process_request($q); }
Note that as CGI::Fast is based on CGI.pm it is no longer advised as a way to write Perl web apps. See <https://metacpan.org/pod/CGI#CGI.pm-HAS-BEEN-REMOVED-FROM-THE-PERL-CORE> for more information about this
A typical FastCGI script will look like this:
#!perl use CGI::Fast; do_some_initialization(); while ($q = CGI::Fast->new) { process_request($q); }
Each time there's a new request, CGI::Fast returns a CGI object to your loop. The rest of the time your script waits in the call to new(). When the server requests that your script be terminated, new() will return undef. You can of course exit earlier if you choose. A new version of the script will be respawned to take its place (this may be necessary in order to avoid Perl memory leaks in long-running scripts).
CGI.pm's default CGI object mode also works. Just modify the loop this way:
while (CGI::Fast->new) { process_request(); }
Calls to header(), start_form(), etc. will all operate on the current request.
AddType application/x-httpd-fcgi .fcgi
FastCGI scripts must end in the extension .fcgi. For each script you install, you must add something like the following to srm.conf:
FastCgiServer /usr/etc/httpd/fcgi-bin/file_upload.fcgi -processes 2
This instructs Apache to launch two copies of file_upload.fcgi at startup time.
FastCgiExternalServer /usr/etc/httpd/fcgi-bin/file_upload.fcgi -host sputnik:8888
Two environment variables affect how the "CGI::Fast" object is created, allowing "CGI::Fast" to be used as an external FastCGI server. (See "FCGI" documentation for "FCGI::OpenSocket" for more information.)
You can set these as ENV variables or imports in the use CGI::Fast statement. If the ENV variables are set then these will be favoured so you can override the import statements on the command line, etc.
For example:
use CGI::Fast socket_path => "sputnik:8888", listen_queue => "50" ; use CGI qw/ :standard /; do_some_initialization(); while ($q = CGI::Fast->new) { process_request($q); }
Or:
use CGI::Fast; use CGI qw/ :standard /; do_some_initialization(); $ENV{FCGI_SOCKET_PATH} = "sputnik:8888"; $ENV{FCGI_LISTEN_QUEUE} = 50; while ($q = CGI::Fast->new) { process_request($q); }
Note the importance of having use CGI after use CGI::Fast as this will prevent any CGI import pragmas being overwritten by CGI::Fast. You can use CGI::Fast as a drop in replacement like so:
use CGI::Fast qw/ :standard /
CGI::Fast->file_handles({ fcgi_output_file_handle => IO::Handle->new, fcgi_error_file_handle => IO::Handle->new, }); while (CGI::Fast->new) { .. }
Overriding STDIN using the "fcgi_input_file_handle" key is also possible, however doing so is likely to break at least POST requests.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Address bug reports and comments to:
https://github.com/leejo/cgi-fast