1 package Catalyst::Engine::FastCGI;
4 extends 'Catalyst::Engine::CGI';
7 die "Unable to load the FCGI module, you may need to install it:\n$@\n" if $@;
11 Catalyst::Engine::FastCGI - FastCGI Engine
15 This is the FastCGI engine.
17 =head1 OVERLOADED METHODS
19 This class overloads some methods from C<Catalyst::Engine::CGI>.
21 =head2 $self->run($c, $listen, { option => value, ... })
23 Starts the FastCGI server. If C<$listen> is set, then it specifies a
24 location to listen for FastCGI requests;
30 listen via Unix sockets on /path
34 listen via TCP on port on all interfaces
38 listen via TCP on port bound to hostname
42 Options may also be specified;
48 Set to 1 to disable setting umask to 0 for socket open =item nointr
50 Do not allow the listener to be interrupted by Ctrl+C
54 Specify a number of processes for FCGI::ProcManager
58 Specify a filename for the pid file
62 Specify a FCGI::ProcManager sub-class
70 Send STDERR to STDOUT instead of the webserver
77 my ( $self, $class, $listen, $options ) = @_;
81 my $old_umask = umask;
82 unless ( $options->{leave_umask} ) {
85 $sock = FCGI::OpenSocket( $listen, 100 )
86 or die "failed to open FastCGI socket; $!";
87 unless ( $options->{leave_umask} ) {
91 elsif ( $^O ne 'MSWin32' ) {
93 or die "STDIN is not a socket; specify a listen location";
99 my $error = \*STDERR; # send STDERR to the web server
100 $error = \*STDOUT # send STDERR to stdout (a logfile)
101 if $options->{keep_stderr}; # (if asked to)
104 FCGI::Request( \*STDIN, \*STDOUT, $error, \%env, $sock,
105 ( $options->{nointr} ? 0 : &FCGI::FAIL_ACCEPT_ON_INTR ),
111 $options->{manager} ||= "FCGI::ProcManager";
112 $options->{nproc} ||= 1;
114 $self->daemon_fork() if $options->{detach};
116 if ( $options->{manager} ) {
117 eval "use $options->{manager}; 1" or die $@;
119 $proc_manager = $options->{manager}->new(
121 n_processes => $options->{nproc},
122 pid_fname => $options->{pidfile},
126 # detach *before* the ProcManager inits
127 $self->daemon_detach() if $options->{detach};
129 $proc_manager->pm_manage();
131 elsif ( $options->{detach} ) {
132 $self->daemon_detach();
136 while ( $request->Accept >= 0 ) {
137 $proc_manager && $proc_manager->pm_pre_dispatch();
139 # If we're running under Lighttpd, swap PATH_INFO and SCRIPT_NAME
140 # http://lists.rawmode.org/pipermail/catalyst/2006-June/008361.html
141 # Thanks to Mark Blythe for this fix
142 if ( $env{SERVER_SOFTWARE} && $env{SERVER_SOFTWARE} =~ /lighttpd/ ) {
143 $env{PATH_INFO} ||= delete $env{SCRIPT_NAME};
146 $class->handle_request( env => \%env );
148 $proc_manager && $proc_manager->pm_post_dispatch();
152 =head2 $self->write($c, $buffer)
157 my ( $self, $c, $buffer ) = @_;
159 unless ( $self->{_prepared_write} ) {
160 $self->prepare_write($c);
161 $self->{_prepared_write} = 1;
164 # XXX: We can't use Engine's write() method because syswrite
165 # appears to return bogus values instead of the number of bytes
166 # written: http://www.fastcgi.com/om_archive/mail-archive/0128.html
168 # Prepend the headers if they have not yet been sent
169 if ( my $headers = delete $self->{_header_buf} ) {
170 $buffer = $headers . $buffer;
173 # FastCGI does not stream data properly if using 'print $handle',
174 # but a syswrite appears to work properly.
175 *STDOUT->syswrite($buffer);
178 =head2 $self->daemon_fork()
180 Performs the first part of daemon initialisation. Specifically,
181 forking. STDERR, etc are still connected to a terminal.
190 =head2 $self->daemon_detach( )
192 Performs the second part of daemon initialisation. Specifically,
193 disassociates from the terminal.
195 However, this does B<not> change the current working directory to "/",
196 as normal daemons do. It also does not close all open file
197 descriptors (except STDIN, STDOUT and STDERR, which are re-opened from
204 print "FastCGI daemon started (pid $$)\n";
205 open STDIN, "+</dev/null" or die $!;
206 open STDOUT, ">&STDIN" or die $!;
207 open STDERR, ">&STDIN" or die $!;
214 =head1 WEB SERVER CONFIGURATIONS
216 =head2 Standalone FastCGI Server
218 In server mode the application runs as a standalone server and accepts
219 connections from a web server. The application can be on the same machine as
220 the web server, on a remote machine, or even on multiple remote machines.
221 Advantages of this method include running the Catalyst application as a
222 different user than the web server, and the ability to set up a scalable
225 To start your application in server mode, install the FCGI::ProcManager
226 module and then use the included fastcgi.pl script.
228 $ script/myapp_fastcgi.pl -l /tmp/myapp.socket -n 5
230 Command line options for fastcgi.pl include:
232 -d -daemon Daemonize the server.
233 -p -pidfile Write a pidfile with the pid of the process manager.
234 -l -listen Listen on a socket path, hostname:port, or :port.
235 -n -nproc The number of processes started to handle requests.
237 See below for the specific web server configurations for using the external
240 =head2 Apache 1.x, 2.x
242 Apache requires the mod_fastcgi module. The same module supports both
245 There are three ways to run your application under FastCGI on Apache: server,
248 =head3 Standalone server mode
250 FastCgiExternalServer /tmp/myapp.fcgi -socket /tmp/myapp.socket
251 Alias /myapp/ /tmp/myapp/myapp.fcgi/
253 # Or, run at the root
254 Alias / /tmp/myapp.fcgi/
256 # Optionally, rewrite the path when accessed without a trailing slash
257 RewriteRule ^/myapp$ myapp/ [R]
260 The FastCgiExternalServer directive tells Apache that when serving
261 /tmp/myapp to use the FastCGI application listenting on the socket
262 /tmp/mapp.socket. Note that /tmp/myapp.fcgi does not need to exist --
263 it's a virtual file name. With some versions of C<mod_fastcgi> or
264 C<mod_fcgid>, you can use any name you like, but most require that the
265 virtual filename end in C<.fcgi>.
267 It's likely that Apache is not configured to serve files in /tmp, so the
268 Alias directive maps the url path /myapp/ to the (virtual) file that runs the
269 FastCGI application. The trailing slashes are important as their use will
270 correctly set the PATH_INFO environment variable used by Catalyst to
271 determine the request path. If you would like to be able to access your app
272 without a trailing slash (http://server/myapp), you can use the above
273 RewriteRule directive.
277 The term 'static' is misleading, but in static mode Apache uses its own
278 FastCGI Process Manager to start the application processes. This happens at
279 Apache startup time. In this case you do not run your application's
280 fastcgi.pl script -- that is done by Apache. Apache then maps URIs to the
281 FastCGI script to run your application.
283 FastCgiServer /path/to/myapp/script/myapp_fastcgi.pl -processes 3
284 Alias /myapp/ /path/to/myapp/script/myapp_fastcgi.pl/
286 FastCgiServer tells Apache to start three processes of your application at
287 startup. The Alias command maps a path to the FastCGI application. Again,
288 the trailing slashes are important.
292 In FastCGI dynamic mode, Apache will run your application on demand,
293 typically by requesting a file with a specific extension (e.g. .fcgi). ISPs
294 often use this type of setup to provide FastCGI support to many customers.
296 In this mode it is often enough to place or link your *_fastcgi.pl script in
297 your cgi-bin directory with the extension of .fcgi. In dynamic mode Apache
298 must be able to run your application as a CGI script so ExecCGI must be
299 enabled for the directory.
301 AddHandler fastcgi-script .fcgi
303 The above tells Apache to run any .fcgi file as a FastCGI application.
305 Here is a complete example:
308 ServerName www.myapp.com
309 DocumentRoot /path/to/MyApp
311 # Allow CGI script to run
312 <Directory /path/to/MyApp>
316 # Tell Apache this is a FastCGI application
317 <Files myapp_fastcgi.pl>
318 SetHandler fastcgi-script
322 Then a request for /script/myapp_fastcgi.pl will run the
325 For more information on using FastCGI under Apache, visit
326 L<http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html>
328 =head3 Authorization header with mod_fastcgi or mod_cgi
330 By default, mod_fastcgi/mod_cgi do not pass along the Authorization header,
331 so modules like C<Catalyst::Plugin::Authentication::Credential::HTTP> will
332 not work. To enable pass-through of this header, add the following
333 mod_rewrite directives:
335 RewriteCond %{HTTP:Authorization} ^(.+)
336 RewriteRule ^(.*)$ $1 [E=HTTP_AUTHORIZATION:%1,PT]
340 These configurations were tested with Lighttpd 1.4.7.
342 =head3 Standalone server mode
344 server.document-root = "/var/www/MyApp/root"
349 "socket" => "/tmp/myapp.socket",
350 "check-local" => "disable"
357 server.document-root = "/var/www/MyApp/root"
362 "socket" => "/tmp/myapp.socket",
363 "check-local" => "disable",
364 "bin-path" => "/var/www/MyApp/script/myapp_fastcgi.pl",
372 Note that in newer versions of lighttpd, the min-procs and idle-timeout
373 values are disabled. The above example would start 5 processes.
375 =head3 Non-root configuration
377 You can also run your application at any non-root location with either of the
388 For more information on using FastCGI under Lighttpd, visit
389 L<http://www.lighttpd.net/documentation/fastcgi.html>
393 It is possible to run Catalyst under IIS with FastCGI, but we do not
394 yet have detailed instructions.
398 L<Catalyst>, L<FCGI>.
402 Sebastian Riedel, <sri@cpan.org>
404 Christian Hansen, <ch@ngmedia.com>
406 Andy Grundman, <andy@hybridized.org>
410 Bill Moseley, for documentation updates and testing.
414 This program is free software, you can redistribute it and/or modify it under
415 the same terms as Perl itself.