1 package Catalyst::Engine::FastCGI;
6 extends 'Catalyst::Engine::CGI';
8 # eval { Class::MOP::load_class("FCGI") };
10 die "Unable to load the FCGI module, you may need to install it:\n$@\n" if $@;
14 Catalyst::Engine::FastCGI - FastCGI Engine
18 This is the FastCGI engine.
20 =head1 OVERLOADED METHODS
22 This class overloads some methods from C<Catalyst::Engine::CGI>.
24 =head2 $self->run($c, $listen, { option => value, ... })
26 Starts the FastCGI server. If C<$listen> is set, then it specifies a
27 location to listen for FastCGI requests;
33 listen via Unix sockets on /path
37 listen via TCP on port on all interfaces
41 listen via TCP on port bound to hostname
45 Options may also be specified;
51 Set to 1 to disable setting umask to 0 for socket open =item nointr
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;
117 $self->daemon_fork() if $options->{detach};
119 if ( $options->{manager} ) {
120 eval "use $options->{manager}; 1" or die $@;
122 $proc_manager = $options->{manager}->new(
124 n_processes => $options->{nproc},
125 pid_fname => $options->{pidfile},
129 # detach *before* the ProcManager inits
130 $self->daemon_detach() if $options->{detach};
132 $proc_manager->pm_manage();
134 elsif ( $options->{detach} ) {
135 $self->daemon_detach();
139 while ( $request->Accept >= 0 ) {
140 $proc_manager && $proc_manager->pm_pre_dispatch();
142 # If we're running under Lighttpd, swap PATH_INFO and SCRIPT_NAME
143 # http://lists.rawmode.org/pipermail/catalyst/2006-June/008361.html
144 # Thanks to Mark Blythe for this fix
145 if ( $env{SERVER_SOFTWARE} && $env{SERVER_SOFTWARE} =~ /lighttpd/ ) {
146 $env{PATH_INFO} ||= delete $env{SCRIPT_NAME};
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 unless ( $self->{_prepared_write} ) {
163 $self->prepare_write($c);
164 $self->{_prepared_write} = 1;
167 # XXX: We can't use Engine's write() method because syswrite
168 # appears to return bogus values instead of the number of bytes
169 # written: http://www.fastcgi.com/om_archive/mail-archive/0128.html
171 # Prepend the headers if they have not yet been sent
172 if ( my $headers = delete $self->{_header_buf} ) {
173 $buffer = $headers . $buffer;
176 # FastCGI does not stream data properly if using 'print $handle',
177 # but a syswrite appears to work properly.
178 *STDOUT->syswrite($buffer);
181 =head2 $self->daemon_fork()
183 Performs the first part of daemon initialisation. Specifically,
184 forking. STDERR, etc are still connected to a terminal.
193 =head2 $self->daemon_detach( )
195 Performs the second part of daemon initialisation. Specifically,
196 disassociates from the terminal.
198 However, this does B<not> change the current working directory to "/",
199 as normal daemons do. It also does not close all open file
200 descriptors (except STDIN, STDOUT and STDERR, which are re-opened from
207 print "FastCGI daemon started (pid $$)\n";
208 open STDIN, "+</dev/null" or die $!;
209 open STDOUT, ">&STDIN" or die $!;
210 open STDERR, ">&STDIN" or die $!;
217 =head1 WEB SERVER CONFIGURATIONS
219 =head2 Standalone FastCGI Server
221 In server mode the application runs as a standalone server and accepts
222 connections from a web server. The application can be on the same machine as
223 the web server, on a remote machine, or even on multiple remote machines.
224 Advantages of this method include running the Catalyst application as a
225 different user than the web server, and the ability to set up a scalable
228 To start your application in server mode, install the FCGI::ProcManager
229 module and then use the included fastcgi.pl script.
231 $ script/myapp_fastcgi.pl -l /tmp/myapp.socket -n 5
233 Command line options for fastcgi.pl include:
235 -d -daemon Daemonize the server.
236 -p -pidfile Write a pidfile with the pid of the process manager.
237 -l -listen Listen on a socket path, hostname:port, or :port.
238 -n -nproc The number of processes started to handle requests.
240 See below for the specific web server configurations for using the external
243 =head2 Apache 1.x, 2.x
245 Apache requires the mod_fastcgi module. The same module supports both
248 There are three ways to run your application under FastCGI on Apache: server,
251 =head3 Standalone server mode
253 FastCgiExternalServer /tmp/myapp.fcgi -socket /tmp/myapp.socket
254 Alias /myapp/ /tmp/myapp/myapp.fcgi/
256 # Or, run at the root
257 Alias / /tmp/myapp.fcgi/
259 # Optionally, rewrite the path when accessed without a trailing slash
260 RewriteRule ^/myapp$ myapp/ [R]
263 The FastCgiExternalServer directive tells Apache that when serving
264 /tmp/myapp to use the FastCGI application listenting on the socket
265 /tmp/mapp.socket. Note that /tmp/myapp.fcgi does not need to exist --
266 it's a virtual file name. With some versions of C<mod_fastcgi> or
267 C<mod_fcgid>, you can use any name you like, but most require that the
268 virtual filename end in C<.fcgi>.
270 It's likely that Apache is not configured to serve files in /tmp, so the
271 Alias directive maps the url path /myapp/ to the (virtual) file that runs the
272 FastCGI application. The trailing slashes are important as their use will
273 correctly set the PATH_INFO environment variable used by Catalyst to
274 determine the request path. If you would like to be able to access your app
275 without a trailing slash (http://server/myapp), you can use the above
276 RewriteRule directive.
280 The term 'static' is misleading, but in static mode Apache uses its own
281 FastCGI Process Manager to start the application processes. This happens at
282 Apache startup time. In this case you do not run your application's
283 fastcgi.pl script -- that is done by Apache. Apache then maps URIs to the
284 FastCGI script to run your application.
286 FastCgiServer /path/to/myapp/script/myapp_fastcgi.pl -processes 3
287 Alias /myapp/ /path/to/myapp/script/myapp_fastcgi.pl/
289 FastCgiServer tells Apache to start three processes of your application at
290 startup. The Alias command maps a path to the FastCGI application. Again,
291 the trailing slashes are important.
295 In FastCGI dynamic mode, Apache will run your application on demand,
296 typically by requesting a file with a specific extension (e.g. .fcgi). ISPs
297 often use this type of setup to provide FastCGI support to many customers.
299 In this mode it is often enough to place or link your *_fastcgi.pl script in
300 your cgi-bin directory with the extension of .fcgi. In dynamic mode Apache
301 must be able to run your application as a CGI script so ExecCGI must be
302 enabled for the directory.
304 AddHandler fastcgi-script .fcgi
306 The above tells Apache to run any .fcgi file as a FastCGI application.
308 Here is a complete example:
311 ServerName www.myapp.com
312 DocumentRoot /path/to/MyApp
314 # Allow CGI script to run
315 <Directory /path/to/MyApp>
319 # Tell Apache this is a FastCGI application
320 <Files myapp_fastcgi.pl>
321 SetHandler fastcgi-script
325 Then a request for /script/myapp_fastcgi.pl will run the
328 For more information on using FastCGI under Apache, visit
329 L<http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html>
331 =head3 Authorization header with mod_fastcgi or mod_cgi
333 By default, mod_fastcgi/mod_cgi do not pass along the Authorization header,
334 so modules like C<Catalyst::Plugin::Authentication::Credential::HTTP> will
335 not work. To enable pass-through of this header, add the following
336 mod_rewrite directives:
338 RewriteCond %{HTTP:Authorization} ^(.+)
339 RewriteRule ^(.*)$ $1 [E=HTTP_AUTHORIZATION:%1,PT]
343 These configurations were tested with Lighttpd 1.4.7.
345 =head3 Standalone server mode
347 server.document-root = "/var/www/MyApp/root"
352 "socket" => "/tmp/myapp.socket",
353 "check-local" => "disable"
360 server.document-root = "/var/www/MyApp/root"
365 "socket" => "/tmp/myapp.socket",
366 "check-local" => "disable",
367 "bin-path" => "/var/www/MyApp/script/myapp_fastcgi.pl",
375 Note that in newer versions of lighttpd, the min-procs and idle-timeout
376 values are disabled. The above example would start 5 processes.
378 =head3 Non-root configuration
380 You can also run your application at any non-root location with either of the
391 For more information on using FastCGI under Lighttpd, visit
392 L<http://www.lighttpd.net/documentation/fastcgi.html>
396 It is possible to run Catalyst under IIS with FastCGI, but we do not
397 yet have detailed instructions.
401 L<Catalyst>, L<FCGI>.
405 Sebastian Riedel, <sri@cpan.org>
407 Christian Hansen, <ch@ngmedia.com>
409 Andy Grundman, <andy@hybridized.org>
413 Bill Moseley, for documentation updates and testing.
417 This program is free software, you can redistribute it and/or modify it under
418 the same terms as Perl itself.