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