Reformatted documentation
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / FastCGI.pm
CommitLineData
d1d9793f 1package Catalyst::Engine::FastCGI;
ffb41d94 2
3use strict;
fbcc39ad 4use base 'Catalyst::Engine::CGI';
6f409682 5eval "use FCGI";
6die "Please install FCGI\n" if $@;
ffb41d94 7
8=head1 NAME
9
fbcc39ad 10Catalyst::Engine::FastCGI - FastCGI Engine
ffb41d94 11
12=head1 DESCRIPTION
13
fbcc39ad 14This is the FastCGI engine.
ffb41d94 15
e2fd5b5f 16=head1 OVERLOADED METHODS
17
fbcc39ad 18This class overloads some methods from C<Catalyst::Engine::CGI>.
e2fd5b5f 19
b5ecfcf0 20=head2 $self->run($c, $listen, { option => value, ... })
5898abae 21
22Starts the FastCGI server. If C<$listen> is set, then it specifies a
23location to listen for FastCGI requests;
24
25 Form Meaning
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
29
30Options may also be specified;
31
32 Option Meaning
33 leave_umask Set to 1 to disable setting umask to 0
34 for socket open
35 nointr Do not allow the listener to be
36 interrupted by Ctrl+C
37 nproc Specify a number of processes for
38 FCGI::ProcManager
dc2fc680 39 pidfile Specify a filename for the pid file
526b698a 40 manager Specify a FCGI::ProcManager sub-class
41 detach Detach from console
e2fd5b5f 42
43=cut
44
fbcc39ad 45sub run {
5898abae 46 my ( $self, $class, $listen, $options ) = @_;
47
48 my $sock;
49 if ($listen) {
50 my $old_umask = umask;
51 unless ( $options->{leave_umask} ) {
52 umask(0);
53 }
54 $sock = FCGI::OpenSocket( $listen, 100 )
55 or die "failed to open FastCGI socket; $!";
56 unless ( $options->{leave_umask} ) {
57 umask($old_umask);
58 }
59 }
60 else {
61 -S STDIN
62 or die "STDIN is not a socket; specify a listen location";
63 }
64
65 $options ||= {};
6f409682 66
84528885 67 my %env;
e2fd5b5f 68
5898abae 69 my $request =
84528885 70 FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%env, $sock,
5898abae 71 ( $options->{nointr} ? 0 : &FCGI::FAIL_ACCEPT_ON_INTR ),
72 );
73
74 my $proc_manager;
6f409682 75
76 if ($listen) {
526b698a 77 $options->{manager} ||= "FCGI::ProcManager";
78 $options->{nproc} ||= 1;
b5ecfcf0 79
526b698a 80 $self->daemon_fork() if $options->{detach};
b5ecfcf0 81
526b698a 82 if ( $options->{manager} ) {
83 eval "use $options->{manager}; 1" or die $@;
84
b5ecfcf0 85 $proc_manager = $options->{manager}->new(
86 {
526b698a 87 n_processes => $options->{nproc},
88 pid_fname => $options->{pidfile},
b5ecfcf0 89 }
90 );
91
526b698a 92 # detach *before* the ProcManager inits
93 $self->daemon_detach() if $options->{detach};
94
95 $proc_manager->pm_manage();
dc2fc680 96 }
526b698a 97 elsif ( $options->{detach} ) {
98 $self->daemon_detach();
b5ecfcf0 99 }
5898abae 100 }
e2fd5b5f 101
fbcc39ad 102 while ( $request->Accept >= 0 ) {
5898abae 103 $proc_manager && $proc_manager->pm_pre_dispatch();
84528885 104 $class->handle_request( env => \%env );
5898abae 105 $proc_manager && $proc_manager->pm_pre_dispatch();
fbcc39ad 106 }
e2fd5b5f 107}
108
b5ecfcf0 109=head2 $self->write($c, $buffer)
e2fd5b5f 110
111=cut
112
fbcc39ad 113sub write {
114 my ( $self, $c, $buffer ) = @_;
4f5ebacd 115
fbcc39ad 116 unless ( $self->{_prepared_write} ) {
4f5ebacd 117 $self->prepare_write($c);
fbcc39ad 118 $self->{_prepared_write} = 1;
119 }
4f5ebacd 120
fbcc39ad 121 # FastCGI does not stream data properly if using 'print $handle',
122 # but a syswrite appears to work properly.
4f5ebacd 123 *STDOUT->syswrite($buffer);
e2fd5b5f 124}
125
b5ecfcf0 126=head2 $self->daemon_fork()
526b698a 127
128Performs the first part of daemon initialisation. Specifically,
129forking. STDERR, etc are still connected to a terminal.
130
131=cut
132
133sub daemon_fork {
134 require POSIX;
135 fork && exit;
136}
137
b5ecfcf0 138=head2 $self->daemon_detach( )
526b698a 139
140Performs the second part of daemon initialisation. Specifically,
141disassociates from the terminal.
142
143However, this does B<not> change the current working directory to "/",
144as normal daemons do. It also does not close all open file
145descriptors (except STDIN, STDOUT and STDERR, which are re-opened from
146F</dev/null>).
147
148=cut
149
150sub daemon_detach {
151 my $self = shift;
152 print "FastCGI daemon started (pid $$)\n";
b5ecfcf0 153 open STDIN, "+</dev/null" or die $!;
154 open STDOUT, ">&STDIN" or die $!;
155 open STDERR, ">&STDIN" or die $!;
526b698a 156 POSIX::setsid();
157}
158
198b0efa 1591;
160__END__
161
198b0efa 162=head1 WEB SERVER CONFIGURATIONS
163
164=head2 Apache 1.x, 2.x
165
166Apache requires the mod_fastcgi module. The following config will let Apache
167control the running of your FastCGI processes.
168
169 # Launch the FastCGI processes
170 FastCgiIpcDir /tmp
8f753f10 171 FastCgiServer /var/www/MyApp/script/myapp_fastcgi.pl -idle-timeout 300 -processes 5
198b0efa 172
173 <VirtualHost *>
174 ScriptAlias / /var/www/MyApp/script/myapp_fastcgi.pl/
175 </VirtualHost>
176
177You can also tell Apache to connect to an external FastCGI server:
178
179 # Start the external server (requires FCGI::ProcManager)
180 $ script/myapp_fastcgi.pl -l /tmp/myapp.socket -n 5
181
182 # Note that the path used in FastCgiExternalServer can be any path
183 FastCgiIpcDir /tmp
184 FastCgiExternalServer /tmp/myapp_fastcgi.pl -socket /tmp/myapp.socket
185
186 <VirtualHost *>
187 ScriptAlias / /tmp/myapp_fastcgi.pl/
188 </VirtualHost>
189
190For more information on using FastCGI under Apache, visit
191L<http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html>
192
193=head2 Lighttpd
194
195This configuration was tested with Lighttpd 1.4.7.
196
197 server.document-root = "/var/www/MyApp/root"
198
199 fastcgi.server = (
200 "" => (
201 "MyApp" => (
202 "socket" => "/tmp/myapp.socket",
203 "check-local" => "disable",
204 "bin-path" => "/var/www/MyApp/script/myapp_fastcgi.pl",
205 "min-procs" => 2,
206 "max-procs" => 5,
207 "idle-timeout" => 20
208 )
209 )
210 )
211
25810c41 212You can also run your application at any non-root location.
213
214 fastcgi.server = (
215 "/myapp" => (
216 "MyApp" => (
217 # same as above
218 )
219 )
220 )
221
222You can also use an external server:
198b0efa 223
224 # Start the external server (requires FCGI::ProcManager)
225 $ script/myapp_fastcgi.pl -l /tmp/myapp.socket -n 5
226
227 server.document-root = "/var/www/MyApp/root"
228
229 fastcgi.server = (
230 "" => (
231 "MyApp" => (
232 "socket" => "/tmp/myapp.socket",
233 "check-local" => "disable"
234 )
235 )
236 )
237
238For more information on using FastCGI under Lighttpd, visit
239L<http://www.lighttpd.net/documentation/fastcgi.html>
240
241=head2 IIS
242
243It is possible to run Catalyst under IIS with FastCGI, but we do not
244yet have detailed instructions.
245
fbcc39ad 246=head1 SEE ALSO
e2fd5b5f 247
fbcc39ad 248L<Catalyst>, L<FCGI>.
cd3bb248 249
fbcc39ad 250=head1 AUTHORS
ffb41d94 251
fbcc39ad 252Sebastian Riedel, <sri@cpan.org>
ffb41d94 253
fbcc39ad 254Christian Hansen, <ch@ngmedia.com>
ffb41d94 255
fbcc39ad 256Andy Grundman, <andy@hybridized.org>
ffb41d94 257
258=head1 COPYRIGHT
259
260This program is free software, you can redistribute it and/or modify it under
261the same terms as Perl itself.
262
263=cut