1 package Catalyst::Engine::FastCGI;
5 extends 'Catalyst::Engine::CGI';
7 # eval { Class::MOP::load_class("FCGI") };
9 die "Unable to load the FCGI module, you may need to install it:\n$@\n" if $@;
13 Catalyst::Engine::FastCGI - FastCGI Engine
17 This is the FastCGI engine.
19 =head1 OVERLOADED METHODS
21 This class overloads some methods from C<Catalyst::Engine::CGI>.
23 =head2 $self->run($c, $listen, { option => value, ... })
25 Starts the FastCGI server. If C<$listen> is set, then it specifies a
26 location to listen for FastCGI requests;
32 listen via Unix sockets on /path
36 listen via TCP on port on all interfaces
40 listen via TCP on port bound to hostname
44 Options may also be specified;
50 Set to 1 to disable setting umask to 0 for socket open =item nointr
52 Do not allow the listener to be interrupted by Ctrl+C
56 Specify a number of processes for FCGI::ProcManager
60 Specify a filename for the pid file
64 Specify a FCGI::ProcManager sub-class
72 Send STDERR to STDOUT instead of the webserver
79 my ( $self, $class, $listen, $options ) = @_;
83 my $old_umask = umask;
84 unless ( $options->{leave_umask} ) {
87 $sock = FCGI::OpenSocket( $listen, 100 )
88 or die "failed to open FastCGI socket; $!";
89 unless ( $options->{leave_umask} ) {
93 elsif ( $^O ne 'MSWin32' ) {
95 or die "STDIN is not a socket; specify a listen location";
101 my $error = \*STDERR; # send STDERR to the web server
102 $error = \*STDOUT # send STDERR to stdout (a logfile)
103 if $options->{keep_stderr}; # (if asked to)
106 FCGI::Request( \*STDIN, \*STDOUT, $error, \%env, $sock,
107 ( $options->{nointr} ? 0 : &FCGI::FAIL_ACCEPT_ON_INTR ),
113 $options->{manager} ||= "FCGI::ProcManager";
114 $options->{nproc} ||= 1;
116 $self->daemon_fork() if $options->{detach};
118 if ( $options->{manager} ) {
119 eval "use $options->{manager}; 1" or die $@;
121 $proc_manager = $options->{manager}->new(
123 n_processes => $options->{nproc},
124 pid_fname => $options->{pidfile},
128 # detach *before* the ProcManager inits
129 $self->daemon_detach() if $options->{detach};
131 $proc_manager->pm_manage();
133 elsif ( $options->{detach} ) {
134 $self->daemon_detach();
138 while ( $request->Accept >= 0 ) {
139 $proc_manager && $proc_manager->pm_pre_dispatch();
141 # If we're running under Lighttpd, swap PATH_INFO and SCRIPT_NAME
142 # http://lists.rawmode.org/pipermail/catalyst/2006-June/008361.html
143 # Thanks to Mark Blythe for this fix
144 if ( $env{SERVER_SOFTWARE} && $env{SERVER_SOFTWARE} =~ /lighttpd/ ) {
145 $env{PATH_INFO} ||= delete $env{SCRIPT_NAME};
148 $class->handle_request( env => \%env );
150 $proc_manager && $proc_manager->pm_post_dispatch();
154 =head2 $self->write($c, $buffer)
159 my ( $self, $c, $buffer ) = @_;
161 unless ( $self->{_prepared_write} ) {
162 $self->prepare_write($c);
163 $self->{_prepared_write} = 1;
166 # XXX: We can't use Engine's write() method because syswrite
167 # appears to return bogus values instead of the number of bytes
168 # written: http://www.fastcgi.com/om_archive/mail-archive/0128.html
170 # Prepend the headers if they have not yet been sent
171 if ( my $headers = delete $self->{_header_buf} ) {
172 $buffer = $headers . $buffer;
175 # FastCGI does not stream data properly if using 'print $handle',
176 # but a syswrite appears to work properly.
177 *STDOUT->syswrite($buffer);
180 =head2 $self->daemon_fork()
182 Performs the first part of daemon initialisation. Specifically,
183 forking. STDERR, etc are still connected to a terminal.
192 =head2 $self->daemon_detach( )
194 Performs the second part of daemon initialisation. Specifically,
195 disassociates from the terminal.
197 However, this does B<not> change the current working directory to "/",
198 as normal daemons do. It also does not close all open file
199 descriptors (except STDIN, STDOUT and STDERR, which are re-opened from
206 print "FastCGI daemon started (pid $$)\n";
207 open STDIN, "+</dev/null" or die $!;
208 open STDOUT, ">&STDIN" or die $!;
209 open STDERR, ">&STDIN" or die $!;
216 =head1 WEB SERVER CONFIGURATIONS
218 =head2 Standalone FastCGI Server
220 In server mode the application runs as a standalone server and accepts
221 connections from a web server. The application can be on the same machine as
222 the web server, on a remote machine, or even on multiple remote machines.
223 Advantages of this method include running the Catalyst application as a
224 different user than the web server, and the ability to set up a scalable
227 To start your application in server mode, install the FCGI::ProcManager
228 module and then use the included fastcgi.pl script.
230 $ script/myapp_fastcgi.pl -l /tmp/myapp.socket -n 5
232 Command line options for fastcgi.pl include:
234 -d -daemon Daemonize the server.
235 -p -pidfile Write a pidfile with the pid of the process manager.
236 -l -listen Listen on a socket path, hostname:port, or :port.
237 -n -nproc The number of processes started to handle requests.
239 See below for the specific web server configurations for using the external
242 =head2 Apache 1.x, 2.x
244 Apache requires the mod_fastcgi module. The same module supports both
247 There are three ways to run your application under FastCGI on Apache: server,
250 =head3 Standalone server mode
252 FastCgiExternalServer /tmp/myapp.fcgi -socket /tmp/myapp.socket
253 Alias /myapp/ /tmp/myapp/myapp.fcgi/
255 # Or, run at the root
256 Alias / /tmp/myapp.fcgi/
258 # Optionally, rewrite the path when accessed without a trailing slash
259 RewriteRule ^/myapp$ myapp/ [R]
262 The FastCgiExternalServer directive tells Apache that when serving
263 /tmp/myapp to use the FastCGI application listenting on the socket
264 /tmp/mapp.socket. Note that /tmp/myapp.fcgi does not need to exist --
265 it's a virtual file name. With some versions of C<mod_fastcgi> or
266 C<mod_fcgid>, you can use any name you like, but most require that the
267 virtual filename end in C<.fcgi>.
269 It's likely that Apache is not configured to serve files in /tmp, so the
270 Alias directive maps the url path /myapp/ to the (virtual) file that runs the
271 FastCGI application. The trailing slashes are important as their use will
272 correctly set the PATH_INFO environment variable used by Catalyst to
273 determine the request path. If you would like to be able to access your app
274 without a trailing slash (http://server/myapp), you can use the above
275 RewriteRule directive.
279 The term 'static' is misleading, but in static mode Apache uses its own
280 FastCGI Process Manager to start the application processes. This happens at
281 Apache startup time. In this case you do not run your application's
282 fastcgi.pl script -- that is done by Apache. Apache then maps URIs to the
283 FastCGI script to run your application.
285 FastCgiServer /path/to/myapp/script/myapp_fastcgi.pl -processes 3
286 Alias /myapp/ /path/to/myapp/script/myapp_fastcgi.pl/
288 FastCgiServer tells Apache to start three processes of your application at
289 startup. The Alias command maps a path to the FastCGI application. Again,
290 the trailing slashes are important.
294 In FastCGI dynamic mode, Apache will run your application on demand,
295 typically by requesting a file with a specific extension (e.g. .fcgi). ISPs
296 often use this type of setup to provide FastCGI support to many customers.
298 In this mode it is often enough to place or link your *_fastcgi.pl script in
299 your cgi-bin directory with the extension of .fcgi. In dynamic mode Apache
300 must be able to run your application as a CGI script so ExecCGI must be
301 enabled for the directory.
303 AddHandler fastcgi-script .fcgi
305 The above tells Apache to run any .fcgi file as a FastCGI application.
307 Here is a complete example:
310 ServerName www.myapp.com
311 DocumentRoot /path/to/MyApp
313 # Allow CGI script to run
314 <Directory /path/to/MyApp>
318 # Tell Apache this is a FastCGI application
319 <Files myapp_fastcgi.pl>
320 SetHandler fastcgi-script
324 Then a request for /script/myapp_fastcgi.pl will run the
327 For more information on using FastCGI under Apache, visit
328 L<http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html>
330 =head3 Authorization header with mod_fastcgi or mod_cgi
332 By default, mod_fastcgi/mod_cgi do not pass along the Authorization header,
333 so modules like C<Catalyst::Plugin::Authentication::Credential::HTTP> will
334 not work. To enable pass-through of this header, add the following
335 mod_rewrite directives:
337 RewriteCond %{HTTP:Authorization} ^(.+)
338 RewriteRule ^(.*)$ $1 [E=HTTP_AUTHORIZATION:%1,PT]
342 These configurations were tested with Lighttpd 1.4.7.
344 =head3 Standalone server mode
346 server.document-root = "/var/www/MyApp/root"
351 "socket" => "/tmp/myapp.socket",
352 "check-local" => "disable"
359 server.document-root = "/var/www/MyApp/root"
364 "socket" => "/tmp/myapp.socket",
365 "check-local" => "disable",
366 "bin-path" => "/var/www/MyApp/script/myapp_fastcgi.pl",
374 Note that in newer versions of lighttpd, the min-procs and idle-timeout
375 values are disabled. The above example would start 5 processes.
377 =head3 Non-root configuration
379 You can also run your application at any non-root location with either of the
390 For more information on using FastCGI under Lighttpd, visit
391 L<http://www.lighttpd.net/documentation/fastcgi.html>
395 It is possible to run Catalyst under IIS with FastCGI, but we do not
396 yet have detailed instructions.
400 L<Catalyst>, L<FCGI>.
404 Sebastian Riedel, <sri@cpan.org>
406 Christian Hansen, <ch@ngmedia.com>
408 Andy Grundman, <andy@hybridized.org>
412 Bill Moseley, for documentation updates and testing.
416 This program is free software, you can redistribute it and/or modify it under
417 the same terms as Perl itself.