1 package Catalyst::Engine::FastCGI;
4 extends 'Catalyst::Engine::CGI';
6 # eval { Class::MOP::load_class("FCGI") };
8 die "Unable to load the FCGI module, you may need to install it:\n$@\n" if $@;
12 Catalyst::Engine::FastCGI - FastCGI Engine
16 This is the FastCGI engine.
18 =head1 OVERLOADED METHODS
20 This class overloads some methods from C<Catalyst::Engine::CGI>.
22 =head2 $self->run($c, $listen, { option => value, ... })
24 Starts the FastCGI server. If C<$listen> is set, then it specifies a
25 location to listen for FastCGI requests;
31 listen via Unix sockets on /path
35 listen via TCP on port on all interfaces
39 listen via TCP on port bound to hostname
43 Options may also be specified;
49 Set to 1 to disable setting umask to 0 for socket open =item nointr
51 Do not allow the listener to be interrupted by Ctrl+C
55 Specify a number of processes for FCGI::ProcManager
59 Specify a filename for the pid file
63 Specify a FCGI::ProcManager sub-class
71 Send STDERR to STDOUT instead of the webserver
78 my ( $self, $class, $listen, $options ) = @_;
82 my $old_umask = umask;
83 unless ( $options->{leave_umask} ) {
86 $sock = FCGI::OpenSocket( $listen, 100 )
87 or die "failed to open FastCGI socket; $!";
88 unless ( $options->{leave_umask} ) {
92 elsif ( $^O ne 'MSWin32' ) {
94 or die "STDIN is not a socket; specify a listen location";
100 my $error = \*STDERR; # send STDERR to the web server
101 $error = \*STDOUT # send STDERR to stdout (a logfile)
102 if $options->{keep_stderr}; # (if asked to)
105 FCGI::Request( \*STDIN, \*STDOUT, $error, \%env, $sock,
106 ( $options->{nointr} ? 0 : &FCGI::FAIL_ACCEPT_ON_INTR ),
112 $options->{manager} ||= "FCGI::ProcManager";
113 $options->{nproc} ||= 1;
115 $self->daemon_fork() if $options->{detach};
117 if ( $options->{manager} ) {
118 eval "use $options->{manager}; 1" or die $@;
120 $proc_manager = $options->{manager}->new(
122 n_processes => $options->{nproc},
123 pid_fname => $options->{pidfile},
127 # detach *before* the ProcManager inits
128 $self->daemon_detach() if $options->{detach};
130 $proc_manager->pm_manage();
132 elsif ( $options->{detach} ) {
133 $self->daemon_detach();
137 while ( $request->Accept >= 0 ) {
138 $proc_manager && $proc_manager->pm_pre_dispatch();
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/ ) {
144 $env{PATH_INFO} ||= delete $env{SCRIPT_NAME};
147 $class->handle_request( env => \%env );
149 $proc_manager && $proc_manager->pm_post_dispatch();
153 =head2 $self->write($c, $buffer)
158 my ( $self, $c, $buffer ) = @_;
160 unless ( $self->{_prepared_write} ) {
161 $self->prepare_write($c);
162 $self->{_prepared_write} = 1;
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
169 # Prepend the headers if they have not yet been sent
170 if ( my $headers = delete $self->{_header_buf} ) {
171 $buffer = $headers . $buffer;
174 # FastCGI does not stream data properly if using 'print $handle',
175 # but a syswrite appears to work properly.
176 *STDOUT->syswrite($buffer);
179 =head2 $self->daemon_fork()
181 Performs the first part of daemon initialisation. Specifically,
182 forking. STDERR, etc are still connected to a terminal.
191 =head2 $self->daemon_detach( )
193 Performs the second part of daemon initialisation. Specifically,
194 disassociates from the terminal.
196 However, this does B<not> change the current working directory to "/",
197 as normal daemons do. It also does not close all open file
198 descriptors (except STDIN, STDOUT and STDERR, which are re-opened from
205 print "FastCGI daemon started (pid $$)\n";
206 open STDIN, "+</dev/null" or die $!;
207 open STDOUT, ">&STDIN" or die $!;
208 open STDERR, ">&STDIN" or die $!;
215 =head1 WEB SERVER CONFIGURATIONS
217 =head2 Standalone FastCGI Server
219 In server mode the application runs as a standalone server and accepts
220 connections from a web server. The application can be on the same machine as
221 the web server, on a remote machine, or even on multiple remote machines.
222 Advantages of this method include running the Catalyst application as a
223 different user than the web server, and the ability to set up a scalable
226 To start your application in server mode, install the FCGI::ProcManager
227 module and then use the included fastcgi.pl script.
229 $ script/myapp_fastcgi.pl -l /tmp/myapp.socket -n 5
231 Command line options for fastcgi.pl include:
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.
238 See below for the specific web server configurations for using the external
241 =head2 Apache 1.x, 2.x
243 Apache requires the mod_fastcgi module. The same module supports both
246 There are three ways to run your application under FastCGI on Apache: server,
249 =head3 Standalone server mode
251 FastCgiExternalServer /tmp/myapp.fcgi -socket /tmp/myapp.socket
252 Alias /myapp/ /tmp/myapp/myapp.fcgi/
254 # Or, run at the root
255 Alias / /tmp/myapp.fcgi/
257 # Optionally, rewrite the path when accessed without a trailing slash
258 RewriteRule ^/myapp$ myapp/ [R]
261 The 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 --
264 it's a virtual file name. With some versions of C<mod_fastcgi> or
265 C<mod_fcgid>, you can use any name you like, but most require that the
266 virtual filename end in C<.fcgi>.
268 It's likely that Apache is not configured to serve files in /tmp, so the
269 Alias directive maps the url path /myapp/ to the (virtual) file that runs the
270 FastCGI application. The trailing slashes are important as their use will
271 correctly set the PATH_INFO environment variable used by Catalyst to
272 determine the request path. If you would like to be able to access your app
273 without a trailing slash (http://server/myapp), you can use the above
274 RewriteRule directive.
278 The term 'static' is misleading, but in static mode Apache uses its own
279 FastCGI Process Manager to start the application processes. This happens at
280 Apache startup time. In this case you do not run your application's
281 fastcgi.pl script -- that is done by Apache. Apache then maps URIs to the
282 FastCGI script to run your application.
284 FastCgiServer /path/to/myapp/script/myapp_fastcgi.pl -processes 3
285 Alias /myapp/ /path/to/myapp/script/myapp_fastcgi.pl/
287 FastCgiServer tells Apache to start three processes of your application at
288 startup. The Alias command maps a path to the FastCGI application. Again,
289 the trailing slashes are important.
293 In FastCGI dynamic mode, Apache will run your application on demand,
294 typically by requesting a file with a specific extension (e.g. .fcgi). ISPs
295 often use this type of setup to provide FastCGI support to many customers.
297 In this mode it is often enough to place or link your *_fastcgi.pl script in
298 your cgi-bin directory with the extension of .fcgi. In dynamic mode Apache
299 must be able to run your application as a CGI script so ExecCGI must be
300 enabled for the directory.
302 AddHandler fastcgi-script .fcgi
304 The above tells Apache to run any .fcgi file as a FastCGI application.
306 Here is a complete example:
309 ServerName www.myapp.com
310 DocumentRoot /path/to/MyApp
312 # Allow CGI script to run
313 <Directory /path/to/MyApp>
317 # Tell Apache this is a FastCGI application
318 <Files myapp_fastcgi.pl>
319 SetHandler fastcgi-script
323 Then a request for /script/myapp_fastcgi.pl will run the
326 For more information on using FastCGI under Apache, visit
327 L<http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html>
329 =head3 Authorization header with mod_fastcgi or mod_cgi
331 By default, mod_fastcgi/mod_cgi do not pass along the Authorization header,
332 so modules like C<Catalyst::Plugin::Authentication::Credential::HTTP> will
333 not work. To enable pass-through of this header, add the following
334 mod_rewrite directives:
336 RewriteCond %{HTTP:Authorization} ^(.+)
337 RewriteRule ^(.*)$ $1 [E=HTTP_AUTHORIZATION:%1,PT]
341 These configurations were tested with Lighttpd 1.4.7.
343 =head3 Standalone server mode
345 server.document-root = "/var/www/MyApp/root"
350 "socket" => "/tmp/myapp.socket",
351 "check-local" => "disable"
358 server.document-root = "/var/www/MyApp/root"
363 "socket" => "/tmp/myapp.socket",
364 "check-local" => "disable",
365 "bin-path" => "/var/www/MyApp/script/myapp_fastcgi.pl",
373 Note that in newer versions of lighttpd, the min-procs and idle-timeout
374 values are disabled. The above example would start 5 processes.
376 =head3 Non-root configuration
378 You can also run your application at any non-root location with either of the
379 above modes. Note the required mod_rewrite rule.
381 url.rewrite = ( "myapp\$" => "myapp/" )
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 Catalyst Contributors, see Catalyst.pm
408 Bill Moseley, for documentation updates and testing.
412 This program is free software, you can redistribute it and/or modify it under
413 the same terms as Perl itself.