1 package Catalyst::Engine::FastCGI;
4 use base 'Catalyst::Engine::CGI';
6 die "Please install FCGI\n" if $@;
10 Catalyst::Engine::FastCGI - FastCGI Engine
14 This is the FastCGI engine.
16 =head1 OVERLOADED METHODS
18 This class overloads some methods from C<Catalyst::Engine::CGI>.
20 =head2 $self->run($c, $listen, { option => value, ... })
22 Starts the FastCGI server. If C<$listen> is set, then it specifies a
23 location to listen for FastCGI requests;
26 /path listen via Unix sockets on /path
27 :port listen via TCP on port on all interfaces
28 hostname:port listen via TCP on port bound to hostname
30 Options may also be specified;
33 leave_umask Set to 1 to disable setting umask to 0
35 nointr Do not allow the listener to be
37 nproc Specify a number of processes for
39 pidfile Specify a filename for the pid file
40 manager Specify a FCGI::ProcManager sub-class
41 detach Detach from console
46 my ( $self, $class, $listen, $options ) = @_;
50 my $old_umask = umask;
51 unless ( $options->{leave_umask} ) {
54 $sock = FCGI::OpenSocket( $listen, 100 )
55 or die "failed to open FastCGI socket; $!";
56 unless ( $options->{leave_umask} ) {
60 elsif ( $^O ne 'MSWin32' ) {
62 or die "STDIN is not a socket; specify a listen location";
70 FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%env, $sock,
71 ( $options->{nointr} ? 0 : &FCGI::FAIL_ACCEPT_ON_INTR ),
77 $options->{manager} ||= "FCGI::ProcManager";
78 $options->{nproc} ||= 1;
80 $self->daemon_fork() if $options->{detach};
82 if ( $options->{manager} ) {
83 eval "use $options->{manager}; 1" or die $@;
85 $proc_manager = $options->{manager}->new(
87 n_processes => $options->{nproc},
88 pid_fname => $options->{pidfile},
92 # detach *before* the ProcManager inits
93 $self->daemon_detach() if $options->{detach};
95 $proc_manager->pm_manage();
97 elsif ( $options->{detach} ) {
98 $self->daemon_detach();
102 while ( $request->Accept >= 0 ) {
103 $proc_manager && $proc_manager->pm_pre_dispatch();
105 # If we're running under Lighttpd, swap PATH_INFO and SCRIPT_NAME
106 # http://lists.rawmode.org/pipermail/catalyst/2006-June/008361.html
107 # Thanks to Mark Blythe for this fix
108 if ( $env{SERVER_SOFTWARE} && $env{SERVER_SOFTWARE} =~ /lighttpd/ ) {
109 $env{PATH_INFO} = delete $env{SCRIPT_NAME};
112 $class->handle_request( env => \%env );
114 $proc_manager && $proc_manager->pm_post_dispatch();
118 =head2 $self->write($c, $buffer)
123 my ( $self, $c, $buffer ) = @_;
125 unless ( $self->{_prepared_write} ) {
126 $self->prepare_write($c);
127 $self->{_prepared_write} = 1;
130 # FastCGI does not stream data properly if using 'print $handle',
131 # but a syswrite appears to work properly.
132 *STDOUT->syswrite($buffer);
135 =head2 $self->daemon_fork()
137 Performs the first part of daemon initialisation. Specifically,
138 forking. STDERR, etc are still connected to a terminal.
147 =head2 $self->daemon_detach( )
149 Performs the second part of daemon initialisation. Specifically,
150 disassociates from the terminal.
152 However, this does B<not> change the current working directory to "/",
153 as normal daemons do. It also does not close all open file
154 descriptors (except STDIN, STDOUT and STDERR, which are re-opened from
161 print "FastCGI daemon started (pid $$)\n";
162 open STDIN, "+</dev/null" or die $!;
163 open STDOUT, ">&STDIN" or die $!;
164 open STDERR, ">&STDIN" or die $!;
171 =head1 WEB SERVER CONFIGURATIONS
173 =head2 Standalone FastCGI Server
175 In server mode the application runs as a standalone server and accepts
176 connections from a web server. The application can be on the same machine as
177 the web server, on a remote machine, or even on multiple remote machines.
178 Advantages of this method include running the Catalyst application as a
179 different user than the web server, and the ability to set up a scalable
182 To start your application in server mode, install the FCGI::ProcManager
183 module and then use the included fastcgi.pl script.
185 $ script/myapp_fastcgi.pl -l /tmp/myapp.socket -n 5
187 Command line options for fastcgi.pl include:
189 -d -daemon Daemonize the server.
190 -p -pidfile Write a pidfile with the pid of the process manager.
191 -l -listen Listen on a socket path, hostname:port, or :port.
192 -n -nproc The number of processes started to handle requests.
194 See below for the specific web server configurations for using the external
197 =head2 Apache 1.x, 2.x
199 Apache requires the mod_fastcgi module. The same module supports both
202 There are three ways to run your application under FastCGI on Apache: server,
205 =head3 Standalone server mode
207 FastCgiExternalServer /tmp/myapp -socket /tmp/myapp.socket
208 Alias /myapp/ /tmp/myapp/
210 # Or, run at the root
213 # Optionally, rewrite the path when accessed without a trailing slash
214 RewriteRule ^/myapp$ myapp/ [R]
216 The FastCgiExternalServer directive tells Apache that when serving /tmp/myapp
217 to use the FastCGI application listenting on the socket /tmp/mapp.socket.
218 Note that /tmp/myapp does not need to exist -- it's a virtual file name.
220 It's likely that Apache is not configured to serve files in /tmp, so the
221 Alias directive maps the url path /myapp/ to the (virtual) file that runs the
222 FastCGI application. The trailing slashes are important as their use will
223 correctly set the PATH_INFO environment variable used by Catalyst to
224 determine the request path. If you would like to be able to access your app
225 without a trailing slash (http://server/myapp), you can use the above
226 RewriteRule directive.
230 The term 'static' is misleading, but in static mode Apache uses its own
231 FastCGI Process Manager to start the application processes. This happens at
232 Apache startup time. In this case you do not run your application's
233 fastcgi.pl script -- that is done by Apache. Apache then maps URIs to the
234 FastCGI script to run your application.
236 FastCgiServer /path/to/myapp/script/myapp_fastcgi.pl -processes 3
237 Alias /myapp/ /path/to/myapp/script/myapp_fastcgi.pl/
239 FastCgiServer tells Apache to start three processes of your application at
240 startup. The Alias command maps a path to the FastCGI application. Again,
241 the trailing slashes are important.
245 In FastCGI dynamic mode, Apache will run your application on demand,
246 typically by requesting a file with a specific extension (e.g. .fcgi). ISPs
247 often use this type of setup to provide FastCGI support to many customers.
249 In this mode it is often enough to place or link your *_fastcgi.pl script in
250 your cgi-bin directory with the extension of .fcgi. In dynamic mode Apache
251 must be able to run your application as a CGI script so ExecCGI must be
252 enabled for the directory.
254 AddHandler fastcgi-script .fcgi
256 The above tells Apache to run any .fcgi file as a FastCGI application.
258 Here is a complete example:
261 ServerName www.myapp.com
262 DocumentRoot /path/to/MyApp
264 # Allow CGI script to run
265 <Directory /path/to/MyApp>
269 # Tell Apache this is a FastCGI application
270 <Files myapp_fastcgi.pl>
271 SetHandler fastcgi-script
275 Then a request for /script/myapp_fastcgi.pl will run the
278 For more information on using FastCGI under Apache, visit
279 L<http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html>
283 These configurations were tested with Lighttpd 1.4.7.
285 =head3 Standalone server mode
287 server.document-root = "/var/www/MyApp/root"
292 "socket" => "/tmp/myapp.socket",
293 "check-local" => "disable"
300 server.document-root = "/var/www/MyApp/root"
305 "socket" => "/tmp/myapp.socket",
306 "check-local" => "disable",
307 "bin-path" => "/var/www/MyApp/script/myapp_fastcgi.pl",
315 Note that in newer versions of lighttpd, the min-procs and idle-timeout
316 values are disabled. The above example would start 5 processes.
318 =head3 Non-root configuration
320 You can also run your application at any non-root location with either of the
331 For more information on using FastCGI under Lighttpd, visit
332 L<http://www.lighttpd.net/documentation/fastcgi.html>
336 It is possible to run Catalyst under IIS with FastCGI, but we do not
337 yet have detailed instructions.
341 L<Catalyst>, L<FCGI>.
345 Sebastian Riedel, <sri@cpan.org>
347 Christian Hansen, <ch@ngmedia.com>
349 Andy Grundman, <andy@hybridized.org>
353 Bill Moseley, for documentation updates and testing.
357 This program is free software, you can redistribute it and/or modify it under
358 the same terms as Perl itself.