1 package Catalyst::Engine::FastCGI;
4 extends 'Catalyst::Engine::CGI';
6 # eval { Class::MOP::load_class("FCGI") };
8 die "Unable to load the FCGI module, you may need to install it:\n$@\n" if $@;
12 Catalyst::Engine::FastCGI - FastCGI Engine
16 This is the FastCGI engine.
18 =head1 OVERLOADED METHODS
20 This class overloads some methods from C<Catalyst::Engine::CGI>.
22 =head2 $self->run($c, $listen, { option => value, ... })
24 Starts the FastCGI server. If C<$listen> is set, then it specifies a
25 location to listen for FastCGI requests;
31 listen via Unix sockets on /path
35 listen via TCP on port on all interfaces
39 listen via TCP on port bound to hostname
43 Options may also be specified;
49 Set to 1 to disable setting umask to 0 for socket open
53 Do not allow the listener to be interrupted by Ctrl+C
57 Specify a number of processes for FCGI::ProcManager
61 Specify a filename for the pid file
65 Specify a FCGI::ProcManager sub-class
73 Send STDERR to STDOUT instead of the webserver
80 my ( $self, $class, $listen, $options ) = @_;
84 my $old_umask = umask;
85 unless ( $options->{leave_umask} ) {
88 $sock = FCGI::OpenSocket( $listen, 100 )
89 or die "failed to open FastCGI socket; $!";
90 unless ( $options->{leave_umask} ) {
94 elsif ( $^O ne 'MSWin32' ) {
96 or die "STDIN is not a socket; specify a listen location";
102 my $error = \*STDERR; # send STDERR to the web server
103 $error = \*STDOUT # send STDERR to stdout (a logfile)
104 if $options->{keep_stderr}; # (if asked to)
107 FCGI::Request( \*STDIN, \*STDOUT, $error, \%env, $sock,
108 ( $options->{nointr} ? 0 : &FCGI::FAIL_ACCEPT_ON_INTR ),
114 $options->{manager} ||= "FCGI::ProcManager";
115 $options->{nproc} ||= 1;
116 $options->{proc_title} ||= "perl-fcgi-pm [$class]";
118 $self->daemon_fork() if $options->{detach};
120 if ( $options->{manager} ) {
121 eval "use $options->{manager}; 1" or die $@;
123 $proc_manager = $options->{manager}->new(
125 n_processes => $options->{nproc},
126 pid_fname => $options->{pidfile},
127 pm_title => $options->{proc_title},
131 # detach *before* the ProcManager inits
132 $self->daemon_detach() if $options->{detach};
134 $proc_manager->pm_manage();
136 # Give each child its own RNG state.
139 elsif ( $options->{detach} ) {
140 $self->daemon_detach();
144 while ( $request->Accept >= 0 ) {
145 $proc_manager && $proc_manager->pm_pre_dispatch();
147 $self->_fix_env( \%env );
149 $class->handle_request( env => \%env );
151 $proc_manager && $proc_manager->pm_post_dispatch();
155 =head2 $self->write($c, $buffer)
160 my ( $self, $c, $buffer ) = @_;
162 # ->write will be called once with the body, even in a redirect (and
163 # in that case, the body is undef)
164 $buffer = '' if !defined $buffer;
166 unless ( $self->_prepared_write ) {
167 $self->prepare_write($c);
168 $self->_prepared_write(1);
171 # XXX: We can't use Engine's write() method because syswrite
172 # appears to return bogus values instead of the number of bytes
173 # written: http://www.fastcgi.com/om_archive/mail-archive/0128.html
175 # Prepend the headers if they have not yet been sent
176 if ( $self->_has_header_buf ) {
177 $buffer = $self->_clear_header_buf . $buffer;
180 # FastCGI does not stream data properly if using 'print $handle',
181 # but a syswrite appears to work properly.
182 *STDOUT->syswrite($buffer);
185 =head2 $self->daemon_fork()
187 Performs the first part of daemon initialisation. Specifically,
188 forking. STDERR, etc are still connected to a terminal.
197 =head2 $self->daemon_detach( )
199 Performs the second part of daemon initialisation. Specifically,
200 disassociates from the terminal.
202 However, this does B<not> change the current working directory to "/",
203 as normal daemons do. It also does not close all open file
204 descriptors (except STDIN, STDOUT and STDERR, which are re-opened from
211 print "FastCGI daemon started (pid $$)\n";
212 open STDIN, "+</dev/null" or die $!;
213 open STDOUT, ">&STDIN" or die $!;
214 open STDERR, ">&STDIN" or die $!;
218 =head2 $self->_fix_env( $env )
220 Adjusts the environment variables when necessary.
229 # we are gonna add variables from current system environment %ENV to %env
230 # that contains at this moment just variables taken from FastCGI request
231 foreach my $k (keys(%ENV)) {
232 $env->{$k} = $ENV{$k} unless defined($env->{$k});
235 return unless ( $env->{SERVER_SOFTWARE} );
237 # If we're running under Lighttpd, swap PATH_INFO and SCRIPT_NAME
238 # http://lists.scsys.co.uk/pipermail/catalyst/2006-June/008361.html
239 # Thanks to Mark Blythe for this fix
240 if ( $env->{SERVER_SOFTWARE} =~ /lighttpd/ ) {
241 $env->{PATH_INFO} ||= delete $env->{SCRIPT_NAME};
243 elsif ( $env->{SERVER_SOFTWARE} =~ /^nginx/ ) {
244 my $script_name = $env->{SCRIPT_NAME};
245 $env->{PATH_INFO} =~ s/^$script_name//g;
247 # Fix the environment variables PATH_INFO and SCRIPT_NAME when running
249 elsif ( $env->{SERVER_SOFTWARE} =~ /IIS\/[6-9]\.[0-9]/ ) {
250 my @script_name = split(m!/!, $env->{PATH_INFO});
251 my @path_translated = split(m!/|\\\\?!, $env->{PATH_TRANSLATED});
254 while ($script_name[$#script_name] eq $path_translated[$#path_translated]) {
255 pop(@path_translated);
256 unshift(@path_info, pop(@script_name));
259 unshift(@path_info, '', '');
261 $env->{PATH_INFO} = join('/', @path_info);
262 $env->{SCRIPT_NAME} = join('/', @script_name);
269 =head1 WEB SERVER CONFIGURATIONS
271 =head2 Standalone FastCGI Server
273 In server mode the application runs as a standalone server and accepts
274 connections from a web server. The application can be on the same machine as
275 the web server, on a remote machine, or even on multiple remote machines.
276 Advantages of this method include running the Catalyst application as a
277 different user than the web server, and the ability to set up a scalable
280 To start your application in server mode, install the FCGI::ProcManager
281 module and then use the included fastcgi.pl script.
283 $ script/myapp_fastcgi.pl -l /tmp/myapp.socket -n 5
285 Command line options for fastcgi.pl include:
287 -d -daemon Daemonize the server.
288 -p -pidfile Write a pidfile with the pid of the process manager.
289 -l -listen Listen on a socket path, hostname:port, or :port.
290 -n -nproc The number of processes started to handle requests.
292 See below for the specific web server configurations for using the external
295 =head2 Apache 1.x, 2.x
297 Apache requires the mod_fastcgi module. The same module supports both
300 There are three ways to run your application under FastCGI on Apache: server,
303 =head3 Standalone server mode
305 FastCgiExternalServer /tmp/myapp.fcgi -socket /tmp/myapp.socket
306 Alias /myapp/ /tmp/myapp.fcgi/
308 # Or, run at the root
309 Alias / /tmp/myapp.fcgi/
311 # Optionally, rewrite the path when accessed without a trailing slash
312 RewriteRule ^/myapp$ myapp/ [R]
315 The FastCgiExternalServer directive tells Apache that when serving
316 /tmp/myapp to use the FastCGI application listenting on the socket
317 /tmp/mapp.socket. Note that /tmp/myapp.fcgi B<MUST NOT> exist --
318 it's a virtual file name. With some versions of C<mod_fastcgi> or
319 C<mod_fcgid>, you can use any name you like, but some require that the
320 virtual filename end in C<.fcgi>.
322 It's likely that Apache is not configured to serve files in /tmp, so the
323 Alias directive maps the url path /myapp/ to the (virtual) file that runs the
324 FastCGI application. The trailing slashes are important as their use will
325 correctly set the PATH_INFO environment variable used by Catalyst to
326 determine the request path. If you would like to be able to access your app
327 without a trailing slash (http://server/myapp), you can use the above
328 RewriteRule directive.
332 The term 'static' is misleading, but in static mode Apache uses its own
333 FastCGI Process Manager to start the application processes. This happens at
334 Apache startup time. In this case you do not run your application's
335 fastcgi.pl script -- that is done by Apache. Apache then maps URIs to the
336 FastCGI script to run your application.
338 FastCgiServer /path/to/myapp/script/myapp_fastcgi.pl -processes 3
339 Alias /myapp/ /path/to/myapp/script/myapp_fastcgi.pl/
341 FastCgiServer tells Apache to start three processes of your application at
342 startup. The Alias command maps a path to the FastCGI application. Again,
343 the trailing slashes are important.
347 In FastCGI dynamic mode, Apache will run your application on demand,
348 typically by requesting a file with a specific extension (e.g. .fcgi). ISPs
349 often use this type of setup to provide FastCGI support to many customers.
351 In this mode it is often enough to place or link your *_fastcgi.pl script in
352 your cgi-bin directory with the extension of .fcgi. In dynamic mode Apache
353 must be able to run your application as a CGI script so ExecCGI must be
354 enabled for the directory.
356 AddHandler fastcgi-script .fcgi
358 The above tells Apache to run any .fcgi file as a FastCGI application.
360 Here is a complete example:
363 ServerName www.myapp.com
364 DocumentRoot /path/to/MyApp
366 # Allow CGI script to run
367 <Directory /path/to/MyApp>
371 # Tell Apache this is a FastCGI application
372 <Files myapp_fastcgi.pl>
373 SetHandler fastcgi-script
377 Then a request for /script/myapp_fastcgi.pl will run the
380 For more information on using FastCGI under Apache, visit
381 L<http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html>
383 =head3 Authorization header with mod_fastcgi or mod_cgi
385 By default, mod_fastcgi/mod_cgi do not pass along the Authorization header,
386 so modules like C<Catalyst::Plugin::Authentication::Credential::HTTP> will
387 not work. To enable pass-through of this header, add the following
388 mod_rewrite directives:
390 RewriteCond %{HTTP:Authorization} ^(.+)
391 RewriteRule ^(.*)$ $1 [E=HTTP_AUTHORIZATION:%1,PT]
395 These configurations were tested with Lighttpd 1.4.7.
397 =head3 Standalone server mode
399 server.document-root = "/var/www/MyApp/root"
404 "socket" => "/tmp/myapp.socket",
405 "check-local" => "disable"
412 server.document-root = "/var/www/MyApp/root"
417 "socket" => "/tmp/myapp.socket",
418 "check-local" => "disable",
419 "bin-path" => "/var/www/MyApp/script/myapp_fastcgi.pl",
427 Note that in newer versions of lighttpd, the min-procs and idle-timeout
428 values are disabled. The above example would start 5 processes.
430 =head3 Non-root configuration
432 You can also run your application at any non-root location with either of the
433 above modes. Note the required mod_rewrite rule.
435 url.rewrite = ( "myapp\$" => "myapp/" )
444 For more information on using FastCGI under Lighttpd, visit
445 L<http://www.lighttpd.net/documentation/fastcgi.html>
449 Catalyst runs under nginx via FastCGI in a similar fashion as the lighttpd
450 standalone server as described above.
452 nginx does not have its own internal FastCGI process manager, so you must run
453 the FastCGI service separately.
457 To configure nginx, you must configure the FastCGI parameters and also the
458 socket your FastCGI daemon is listening on. It can be either a TCP socket
459 or a Unix file socket.
461 The server configuration block should look roughly like:
467 fastcgi_param QUERY_STRING $query_string;
468 fastcgi_param REQUEST_METHOD $request_method;
469 fastcgi_param CONTENT_TYPE $content_type;
470 fastcgi_param CONTENT_LENGTH $content_length;
472 fastcgi_param SCRIPT_NAME /;
473 fastcgi_param PATH_INFO $fastcgi_script_name;
474 fastcgi_param REQUEST_URI $request_uri;
475 fastcgi_param DOCUMENT_URI $document_uri;
476 fastcgi_param DOCUMENT_ROOT $document_root;
477 fastcgi_param SERVER_PROTOCOL $server_protocol;
479 fastcgi_param GATEWAY_INTERFACE CGI/1.1;
480 fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
482 fastcgi_param REMOTE_ADDR $remote_addr;
483 fastcgi_param REMOTE_PORT $remote_port;
484 fastcgi_param SERVER_ADDR $server_addr;
485 fastcgi_param SERVER_PORT $server_port;
486 fastcgi_param SERVER_NAME $server_name;
488 # Adjust the socket for your applications!
489 fastcgi_pass unix:$docroot/myapp.socket;
493 It is the standard convention of nginx to include the fastcgi_params in a
494 separate file (usually something like C</etc/nginx/fastcgi_params>) and
495 simply include that file.
497 =head3 Non-root configuration
499 If you properly specify the PATH_INFO and SCRIPT_NAME parameters your
500 application will be accessible at any path. The SCRIPT_NAME variable is the
501 prefix of your application, and PATH_INFO would be everything in addition.
503 As an example, if your application is rooted at /myapp, you would configure:
505 fastcgi_param SCRIPT_NAME /myapp/;
506 fastcgi_param PATH_INFO $fastcgi_script_name;
508 C<$fastcgi_script_name> would be "/myapp/path/of/the/action". Catalyst will
509 process this accordingly and setup the application base as expected.
511 This behavior is somewhat different than Apache and Lighttpd, but is still
514 For more information on nginx, visit:
519 It is possible to run Catalyst under IIS with FastCGI, but only on IIS 6.0
520 (Microsoft Windows 2003), IIS 7.0 (Microsoft Windows 2008 and Vista) and
521 hopefully its successors.
523 Even if it is declared that FastCGI is supported on IIS 5.1 (Windows XP) it
524 does not support some features (specifically: wildcard mappings) that prevents
525 running Catalyst application.
527 Let us assume that our server has the following layout:
529 d:\WWW\WebApp\ path to our Catalyst application
530 d:\strawberry\perl\bin\perl.exe path to perl interpreter (with Catalyst installed)
531 c:\windows Windows directory
533 =head3 Setup IIS 6.0 (Windows 2003)
537 =item Install FastCGI extension for IIS 6.0
539 FastCGI is not a standard part of IIS 6 - you have to install it separately. For
540 more info and download go to L<http://www.iis.net/extensions/FastCGI>. Choose
541 approptiate version (32-bit/64-bit), installation is quite simple
542 (in fact no questions, no options).
544 =item Create a new website
546 Open "Control Panel" > "Administrative Tools" > "Internet Information Services Manager".
547 Click "Action" > "New" > "Web Site". After you finish the installation wizard
548 you need to go to the new website's properties.
550 =item Set website properties
552 On tab "Web site" set proper values for:
553 Site Description, IP Address, TCP Port, SSL Port etc.
555 On tab "Home Directory" set the following:
557 Local path: "d:\WWW\WebApp\root"
558 Local path permission flags: check only "Read" + "Log visits"
559 Execute permitions: "Scripts only"
561 Click "Configuration" button (still on Home Directory tab) then click "Insert"
562 the wildcard application mapping and in the next dialog set:
564 Executable: "c:\windows\system32\inetsrv\fcgiext.dll"
565 Uncheck: "Verify that file exists"
567 Close all dialogs with "OK".
569 =item Edit fcgiext.ini
571 Put the following lines into c:\windows\system32\inetsrv\fcgiext.ini (on 64-bit
572 system c:\windows\syswow64\inetsrv\fcgiext.ini):
576 ;replace 8 with the identification number of the newly created website
577 ;it is not so easy to get this number:
578 ; - you can use utility "c:\inetpub\adminscripts\adsutil.vbs"
579 ; to list websites: "cscript adsutil.vbs ENUM /P /W3SVC"
580 ; to get site name: "cscript adsutil.vbs GET /W3SVC/<number>/ServerComment"
581 ; to get all details: "cscript adsutil.vbs GET /W3SVC/<number>"
582 ; - or look where are the logs located:
583 ; c:\WINDOWS\SYSTEM32\Logfiles\W3SVC7\whatever.log
584 ; means that the corresponding number is "7"
585 ;if you are running just one website using FastCGI you can use '*=CatalystApp'
588 ExePath=d:\strawberry\perl\bin\perl.exe
589 Arguments="d:\WWW\WebApp\script\webapp_fastcgi.pl -e"
591 ;by setting this you can instruct IIS to serve Catalyst static files
592 ;directly not via FastCGI (in case of any problems try 1)
593 IgnoreExistingFiles=0
595 ;do not be fooled by Microsoft doc talking about "IgnoreExistingDirectories"
596 ;that does not work and use "IgnoreDirectories" instead
601 =head3 Setup IIS 7.0 (Windows 2008 and Vista)
603 Microsoft IIS 7.0 has built-in support for FastCGI so you do not have to install
608 =item Necessary steps during IIS7 installation
610 During IIS7 installation after you have added role "Web Server (IIS)"
611 you need to check to install role feature "CGI" (do not be nervous that it is
612 not FastCGI). If you already have IIS7 installed you can add "CGI" role feature
613 through "Control panel" > "Programs and Features".
615 =item Create a new website
617 Open "Control Panel" > "Administrative Tools" > "Internet Information Services Manager"
620 site name: "CatalystSite"
621 content directory: "d:\WWW\WebApp\root"
622 binding: set proper IP address, port etc.
624 =item Configure FastCGI
626 You can configure FastCGI extension using commandline utility
627 "c:\windows\system32\inetsrv\appcmd.exe"
631 =item Configuring section "fastCgi" (it is a global setting)
633 appcmd.exe set config -section:system.webServer/fastCgi /+"[fullPath='d:\strawberry\perl\bin\perl.exe',arguments='d:\www\WebApp\script\webapp_fastcgi.pl -e',maxInstances='4',idleTimeout='300',activityTimeout='30',requestTimeout='90',instanceMaxRequests='1000',protocol='NamedPipe',flushNamedPipe='False']" /commit:apphost
635 =item Configuring proper handler (it is a site related setting)
637 appcmd.exe set config "CatalystSite" -section:system.webServer/handlers /+"[name='CatalystFastCGI',path='*',verb='GET,HEAD,POST',modules='FastCgiModule',scriptProcessor='d:\strawberry\perl\bin\perl.exe|d:\www\WebApp\script\webapp_fastcgi.pl -e',resourceType='Unspecified',requireAccess='Script']" /commit:apphost
639 Note: before launching the commands above do not forget to change site
640 name and paths to values relevant for your server setup.
648 L<Catalyst>, L<FCGI>.
652 Catalyst Contributors, see Catalyst.pm
656 Bill Moseley, for documentation updates and testing.
660 This library is free software. You can redistribute it and/or modify it under
661 the same terms as Perl itself.