Bump version requirement for MX::Emulate::CAF to the new release which fixes the...
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / FastCGI.pm
CommitLineData
d1d9793f 1package Catalyst::Engine::FastCGI;
ffb41d94 2
7fa2c9c1 3use Moose;
4extends 'Catalyst::Engine::CGI';
5
0fc2d522 6# eval { Class::MOP::load_class("FCGI") };
6f409682 7eval "use FCGI";
500a1679 8die "Unable to load the FCGI module, you may need to install it:\n$@\n" if $@;
ffb41d94 9
10=head1 NAME
11
fbcc39ad 12Catalyst::Engine::FastCGI - FastCGI Engine
ffb41d94 13
14=head1 DESCRIPTION
15
fbcc39ad 16This is the FastCGI engine.
ffb41d94 17
e2fd5b5f 18=head1 OVERLOADED METHODS
19
fbcc39ad 20This class overloads some methods from C<Catalyst::Engine::CGI>.
e2fd5b5f 21
b5ecfcf0 22=head2 $self->run($c, $listen, { option => value, ... })
ac5c933b 23
5898abae 24Starts the FastCGI server. If C<$listen> is set, then it specifies a
25location to listen for FastCGI requests;
26
b799b493 27=over 4
28
29=item /path
30
31listen via Unix sockets on /path
32
33=item :port
34
35listen via TCP on port on all interfaces
36
37=item hostname:port
38
39listen via TCP on port bound to hostname
40
41=back
5898abae 42
43Options may also be specified;
44
b799b493 45=over 4
46
47=item leave_umask
48
49Set to 1 to disable setting umask to 0 for socket open =item nointr
50
51Do not allow the listener to be interrupted by Ctrl+C
52
53=item nproc
54
55Specify a number of processes for FCGI::ProcManager
56
57=item pidfile
58
59Specify a filename for the pid file
60
61=item manager
62
63Specify a FCGI::ProcManager sub-class
64
ac5c933b 65=item detach
b799b493 66
67Detach from console
68
69=item keep_stderr
70
71Send STDERR to STDOUT instead of the webserver
72
73=back
e2fd5b5f 74
75=cut
76
fbcc39ad 77sub run {
5898abae 78 my ( $self, $class, $listen, $options ) = @_;
79
5db46287 80 my $sock = 0;
5898abae 81 if ($listen) {
82 my $old_umask = umask;
83 unless ( $options->{leave_umask} ) {
84 umask(0);
85 }
86 $sock = FCGI::OpenSocket( $listen, 100 )
87 or die "failed to open FastCGI socket; $!";
88 unless ( $options->{leave_umask} ) {
89 umask($old_umask);
90 }
91 }
6f2e0021 92 elsif ( $^O ne 'MSWin32' ) {
5898abae 93 -S STDIN
94 or die "STDIN is not a socket; specify a listen location";
95 }
96
97 $options ||= {};
6f409682 98
84528885 99 my %env;
6fc58422 100 my $error = \*STDERR; # send STDERR to the web server
101 $error = \*STDOUT # send STDERR to stdout (a logfile)
85d9fce6 102 if $options->{keep_stderr}; # (if asked to)
ac5c933b 103
5898abae 104 my $request =
6fc58422 105 FCGI::Request( \*STDIN, \*STDOUT, $error, \%env, $sock,
5898abae 106 ( $options->{nointr} ? 0 : &FCGI::FAIL_ACCEPT_ON_INTR ),
107 );
108
109 my $proc_manager;
6f409682 110
111 if ($listen) {
526b698a 112 $options->{manager} ||= "FCGI::ProcManager";
113 $options->{nproc} ||= 1;
b5ecfcf0 114
526b698a 115 $self->daemon_fork() if $options->{detach};
b5ecfcf0 116
526b698a 117 if ( $options->{manager} ) {
118 eval "use $options->{manager}; 1" or die $@;
119
b5ecfcf0 120 $proc_manager = $options->{manager}->new(
121 {
526b698a 122 n_processes => $options->{nproc},
123 pid_fname => $options->{pidfile},
b5ecfcf0 124 }
125 );
126
526b698a 127 # detach *before* the ProcManager inits
128 $self->daemon_detach() if $options->{detach};
129
130 $proc_manager->pm_manage();
dc2fc680 131 }
526b698a 132 elsif ( $options->{detach} ) {
133 $self->daemon_detach();
b5ecfcf0 134 }
5898abae 135 }
e2fd5b5f 136
fbcc39ad 137 while ( $request->Accept >= 0 ) {
5898abae 138 $proc_manager && $proc_manager->pm_pre_dispatch();
ac5c933b 139
d7334071 140 # If we're running under Lighttpd, swap PATH_INFO and SCRIPT_NAME
141 # http://lists.rawmode.org/pipermail/catalyst/2006-June/008361.html
142 # Thanks to Mark Blythe for this fix
143 if ( $env{SERVER_SOFTWARE} && $env{SERVER_SOFTWARE} =~ /lighttpd/ ) {
3b6a7b7f 144 $env{PATH_INFO} ||= delete $env{SCRIPT_NAME};
d7334071 145 }
ac5c933b 146
84528885 147 $class->handle_request( env => \%env );
ac5c933b 148
9f778270 149 $proc_manager && $proc_manager->pm_post_dispatch();
fbcc39ad 150 }
e2fd5b5f 151}
152
b5ecfcf0 153=head2 $self->write($c, $buffer)
e2fd5b5f 154
155=cut
156
fbcc39ad 157sub write {
158 my ( $self, $c, $buffer ) = @_;
4f5ebacd 159
fbcc39ad 160 unless ( $self->{_prepared_write} ) {
4f5ebacd 161 $self->prepare_write($c);
fbcc39ad 162 $self->{_prepared_write} = 1;
163 }
ac5c933b 164
e512dd24 165 # XXX: We can't use Engine's write() method because syswrite
166 # appears to return bogus values instead of the number of bytes
167 # written: http://www.fastcgi.com/om_archive/mail-archive/0128.html
ac5c933b 168
e512dd24 169 # Prepend the headers if they have not yet been sent
170 if ( my $headers = delete $self->{_header_buf} ) {
171 $buffer = $headers . $buffer;
172 }
4f5ebacd 173
fbcc39ad 174 # FastCGI does not stream data properly if using 'print $handle',
175 # but a syswrite appears to work properly.
4f5ebacd 176 *STDOUT->syswrite($buffer);
e2fd5b5f 177}
178
b5ecfcf0 179=head2 $self->daemon_fork()
526b698a 180
181Performs the first part of daemon initialisation. Specifically,
182forking. STDERR, etc are still connected to a terminal.
183
184=cut
185
186sub daemon_fork {
187 require POSIX;
188 fork && exit;
189}
190
b5ecfcf0 191=head2 $self->daemon_detach( )
526b698a 192
193Performs the second part of daemon initialisation. Specifically,
194disassociates from the terminal.
195
196However, this does B<not> change the current working directory to "/",
197as normal daemons do. It also does not close all open file
198descriptors (except STDIN, STDOUT and STDERR, which are re-opened from
199F</dev/null>).
200
201=cut
202
203sub daemon_detach {
204 my $self = shift;
205 print "FastCGI daemon started (pid $$)\n";
b5ecfcf0 206 open STDIN, "+</dev/null" or die $!;
207 open STDOUT, ">&STDIN" or die $!;
208 open STDERR, ">&STDIN" or die $!;
526b698a 209 POSIX::setsid();
210}
211
198b0efa 2121;
213__END__
214
198b0efa 215=head1 WEB SERVER CONFIGURATIONS
216
d7d72ad9 217=head2 Standalone FastCGI Server
218
ac5c933b 219In server mode the application runs as a standalone server and accepts
d7d72ad9 220connections from a web server. The application can be on the same machine as
221the web server, on a remote machine, or even on multiple remote machines.
222Advantages of this method include running the Catalyst application as a
223different user than the web server, and the ability to set up a scalable
224server farm.
198b0efa 225
d7d72ad9 226To start your application in server mode, install the FCGI::ProcManager
227module and then use the included fastcgi.pl script.
198b0efa 228
d7d72ad9 229 $ script/myapp_fastcgi.pl -l /tmp/myapp.socket -n 5
ac5c933b 230
d7d72ad9 231Command line options for fastcgi.pl include:
232
233 -d -daemon Daemonize the server.
234 -p -pidfile Write a pidfile with the pid of the process manager.
235 -l -listen Listen on a socket path, hostname:port, or :port.
236 -n -nproc The number of processes started to handle requests.
ac5c933b 237
d7d72ad9 238See below for the specific web server configurations for using the external
239server.
198b0efa 240
d7d72ad9 241=head2 Apache 1.x, 2.x
242
243Apache requires the mod_fastcgi module. The same module supports both
244Apache 1 and 2.
245
ac5c933b 246There are three ways to run your application under FastCGI on Apache: server,
d7d72ad9 247static, and dynamic.
248
249=head3 Standalone server mode
250
3f0911d3 251 FastCgiExternalServer /tmp/myapp.fcgi -socket /tmp/myapp.socket
252 Alias /myapp/ /tmp/myapp/myapp.fcgi/
ac5c933b 253
d7d72ad9 254 # Or, run at the root
3f0911d3 255 Alias / /tmp/myapp.fcgi/
ac5c933b 256
d7d72ad9 257 # Optionally, rewrite the path when accessed without a trailing slash
258 RewriteRule ^/myapp$ myapp/ [R]
ac5c933b 259
3f0911d3 260
261The FastCgiExternalServer directive tells Apache that when serving
262/tmp/myapp to use the FastCGI application listenting on the socket
263/tmp/mapp.socket. Note that /tmp/myapp.fcgi does not need to exist --
264it's a virtual file name. With some versions of C<mod_fastcgi> or
265C<mod_fcgid>, you can use any name you like, but most require that the
266virtual filename end in C<.fcgi>.
d7d72ad9 267
ac5c933b 268It's likely that Apache is not configured to serve files in /tmp, so the
d7d72ad9 269Alias directive maps the url path /myapp/ to the (virtual) file that runs the
270FastCGI application. The trailing slashes are important as their use will
271correctly set the PATH_INFO environment variable used by Catalyst to
272determine the request path. If you would like to be able to access your app
273without a trailing slash (http://server/myapp), you can use the above
274RewriteRule directive.
275
276=head3 Static mode
277
278The term 'static' is misleading, but in static mode Apache uses its own
279FastCGI Process Manager to start the application processes. This happens at
280Apache startup time. In this case you do not run your application's
281fastcgi.pl script -- that is done by Apache. Apache then maps URIs to the
282FastCGI script to run your application.
283
284 FastCgiServer /path/to/myapp/script/myapp_fastcgi.pl -processes 3
285 Alias /myapp/ /path/to/myapp/script/myapp_fastcgi.pl/
ac5c933b 286
d7d72ad9 287FastCgiServer tells Apache to start three processes of your application at
288startup. The Alias command maps a path to the FastCGI application. Again,
289the trailing slashes are important.
ac5c933b 290
d7d72ad9 291=head3 Dynamic mode
292
ac5c933b 293In FastCGI dynamic mode, Apache will run your application on demand,
d7d72ad9 294typically by requesting a file with a specific extension (e.g. .fcgi). ISPs
295often use this type of setup to provide FastCGI support to many customers.
296
297In this mode it is often enough to place or link your *_fastcgi.pl script in
298your cgi-bin directory with the extension of .fcgi. In dynamic mode Apache
299must be able to run your application as a CGI script so ExecCGI must be
300enabled for the directory.
301
302 AddHandler fastcgi-script .fcgi
303
304The above tells Apache to run any .fcgi file as a FastCGI application.
305
306Here is a complete example:
307
308 <VirtualHost *:80>
309 ServerName www.myapp.com
310 DocumentRoot /path/to/MyApp
311
312 # Allow CGI script to run
313 <Directory /path/to/MyApp>
314 Options +ExecCGI
315 </Directory>
316
317 # Tell Apache this is a FastCGI application
318 <Files myapp_fastcgi.pl>
319 SetHandler fastcgi-script
320 </Files>
198b0efa 321 </VirtualHost>
d7d72ad9 322
323Then a request for /script/myapp_fastcgi.pl will run the
324application.
ac5c933b 325
198b0efa 326For more information on using FastCGI under Apache, visit
327L<http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html>
328
25f55123 329=head3 Authorization header with mod_fastcgi or mod_cgi
330
331By default, mod_fastcgi/mod_cgi do not pass along the Authorization header,
332so modules like C<Catalyst::Plugin::Authentication::Credential::HTTP> will
333not work. To enable pass-through of this header, add the following
334mod_rewrite directives:
335
336 RewriteCond %{HTTP:Authorization} ^(.+)
337 RewriteRule ^(.*)$ $1 [E=HTTP_AUTHORIZATION:%1,PT]
338
198b0efa 339=head2 Lighttpd
340
d7d72ad9 341These configurations were tested with Lighttpd 1.4.7.
342
343=head3 Standalone server mode
344
345 server.document-root = "/var/www/MyApp/root"
346
347 fastcgi.server = (
348 "" => (
349 "MyApp" => (
350 "socket" => "/tmp/myapp.socket",
351 "check-local" => "disable"
352 )
353 )
354 )
355
356=head3 Static mode
198b0efa 357
358 server.document-root = "/var/www/MyApp/root"
ac5c933b 359
198b0efa 360 fastcgi.server = (
361 "" => (
362 "MyApp" => (
363 "socket" => "/tmp/myapp.socket",
364 "check-local" => "disable",
365 "bin-path" => "/var/www/MyApp/script/myapp_fastcgi.pl",
366 "min-procs" => 2,
367 "max-procs" => 5,
368 "idle-timeout" => 20
369 )
370 )
371 )
ac5c933b 372
d7d72ad9 373Note that in newer versions of lighttpd, the min-procs and idle-timeout
374values are disabled. The above example would start 5 processes.
25810c41 375
d7d72ad9 376=head3 Non-root configuration
ac5c933b 377
d7d72ad9 378You can also run your application at any non-root location with either of the
379above modes.
198b0efa 380
381 fastcgi.server = (
d7d72ad9 382 "/myapp" => (
198b0efa 383 "MyApp" => (
d7d72ad9 384 # same as above
198b0efa 385 )
386 )
387 )
388
389For more information on using FastCGI under Lighttpd, visit
390L<http://www.lighttpd.net/documentation/fastcgi.html>
391
392=head2 IIS
393
394It is possible to run Catalyst under IIS with FastCGI, but we do not
395yet have detailed instructions.
396
fbcc39ad 397=head1 SEE ALSO
e2fd5b5f 398
fbcc39ad 399L<Catalyst>, L<FCGI>.
cd3bb248 400
fbcc39ad 401=head1 AUTHORS
ffb41d94 402
fbcc39ad 403Sebastian Riedel, <sri@cpan.org>
ffb41d94 404
fbcc39ad 405Christian Hansen, <ch@ngmedia.com>
ffb41d94 406
fbcc39ad 407Andy Grundman, <andy@hybridized.org>
ffb41d94 408
d7d72ad9 409=head1 THANKS
410
411Bill Moseley, for documentation updates and testing.
412
ffb41d94 413=head1 COPYRIGHT
414
415This program is free software, you can redistribute it and/or modify it under
416the same terms as Perl itself.
417
418=cut