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>.
22 =item $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;
28 /path listen via Unix sockets on /path
29 :port listen via TCP on port on all interfaces
30 hostname:port listen via TCP on port bound to hostname
32 Options may also be specified;
35 leave_umask Set to 1 to disable setting umask to 0
37 nointr Do not allow the listener to be
39 nproc Specify a number of processes for
41 pidfile Specify a filename for the pid file
42 manager Specify a FCGI::ProcManager sub-class
43 detach Detach from console
48 my ( $self, $class, $listen, $options ) = @_;
52 my $old_umask = umask;
53 unless ( $options->{leave_umask} ) {
56 $sock = FCGI::OpenSocket( $listen, 100 )
57 or die "failed to open FastCGI socket; $!";
58 unless ( $options->{leave_umask} ) {
64 or die "STDIN is not a socket; specify a listen location";
72 FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%env, $sock,
73 ( $options->{nointr} ? 0 : &FCGI::FAIL_ACCEPT_ON_INTR ),
79 $options->{manager} ||= "FCGI::ProcManager";
80 $options->{nproc} ||= 1;
82 $self->daemon_fork() if $options->{detach};
84 if ( $options->{manager} ) {
85 eval "use $options->{manager}; 1" or die $@;
88 = $options->{manager}->new( {
89 n_processes => $options->{nproc},
90 pid_fname => $options->{pidfile},
93 # detach *before* the ProcManager inits
94 $self->daemon_detach() if $options->{detach};
96 $proc_manager->pm_manage();
98 elsif ( $options->{detach} ) {
99 $self->daemon_detach();
103 while ( $request->Accept >= 0 ) {
104 $proc_manager && $proc_manager->pm_pre_dispatch();
105 $class->handle_request( env => \%env );
106 $proc_manager && $proc_manager->pm_pre_dispatch();
110 =item $self->write($c, $buffer)
115 my ( $self, $c, $buffer ) = @_;
117 unless ( $self->{_prepared_write} ) {
118 $self->prepare_write($c);
119 $self->{_prepared_write} = 1;
122 # FastCGI does not stream data properly if using 'print $handle',
123 # but a syswrite appears to work properly.
124 *STDOUT->syswrite($buffer);
127 =item $self->daemon_fork()
129 Performs the first part of daemon initialisation. Specifically,
130 forking. STDERR, etc are still connected to a terminal.
139 =item $self->daemon_detach( )
141 Performs the second part of daemon initialisation. Specifically,
142 disassociates from the terminal.
144 However, this does B<not> change the current working directory to "/",
145 as normal daemons do. It also does not close all open file
146 descriptors (except STDIN, STDOUT and STDERR, which are re-opened from
153 print "FastCGI daemon started (pid $$)\n";
154 open STDIN, "+</dev/null" or die $!;
155 open STDOUT, ">&STDIN" or die $!;
156 open STDERR, ">&STDIN" or die $!;
165 =head1 WEB SERVER CONFIGURATIONS
167 =head2 Apache 1.x, 2.x
169 Apache requires the mod_fastcgi module. The following config will let Apache
170 control the running of your FastCGI processes.
172 # Launch the FastCGI processes
174 FastCgiServer /var/www/MyApp/script/myapp_fastcgi.pl -idle-timeout 300 -processes 5
177 ScriptAlias / /var/www/MyApp/script/myapp_fastcgi.pl/
180 You can also tell Apache to connect to an external FastCGI server:
182 # Start the external server (requires FCGI::ProcManager)
183 $ script/myapp_fastcgi.pl -l /tmp/myapp.socket -n 5
185 # Note that the path used in FastCgiExternalServer can be any path
187 FastCgiExternalServer /tmp/myapp_fastcgi.pl -socket /tmp/myapp.socket
190 ScriptAlias / /tmp/myapp_fastcgi.pl/
193 For more information on using FastCGI under Apache, visit
194 L<http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html>
198 This configuration was tested with Lighttpd 1.4.7.
200 server.document-root = "/var/www/MyApp/root"
205 "socket" => "/tmp/myapp.socket",
206 "check-local" => "disable",
207 "bin-path" => "/var/www/MyApp/script/myapp_fastcgi.pl",
215 You can also run your application at any non-root location.
225 You can also use an external server:
227 # Start the external server (requires FCGI::ProcManager)
228 $ script/myapp_fastcgi.pl -l /tmp/myapp.socket -n 5
230 server.document-root = "/var/www/MyApp/root"
235 "socket" => "/tmp/myapp.socket",
236 "check-local" => "disable"
241 For more information on using FastCGI under Lighttpd, visit
242 L<http://www.lighttpd.net/documentation/fastcgi.html>
246 It is possible to run Catalyst under IIS with FastCGI, but we do not
247 yet have detailed instructions.
251 L<Catalyst>, L<FCGI>.
255 Sebastian Riedel, <sri@cpan.org>
257 Christian Hansen, <ch@ngmedia.com>
259 Andy Grundman, <andy@hybridized.org>
263 This program is free software, you can redistribute it and/or modify it under
264 the same terms as Perl itself.