Fixed typos
[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
20=over 4
21
5898abae 22=item $self->run($c, $listen, { option => value, ... })
23
24Starts the FastCGI server. If C<$listen> is set, then it specifies a
25location to listen for FastCGI requests;
26
27 Form Meaning
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
31
32Options may also be specified;
33
34 Option Meaning
35 leave_umask Set to 1 to disable setting umask to 0
36 for socket open
37 nointr Do not allow the listener to be
38 interrupted by Ctrl+C
39 nproc Specify a number of processes for
40 FCGI::ProcManager
dc2fc680 41 pidfile Specify a filename for the pid file
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) {
5898abae 77 require FCGI::ProcManager;
dc2fc680 78 $options->{nproc} ||= 1;
6f409682 79
80 $proc_manager =
81 FCGI::ProcManager->new( { n_processes => $options->{nproc} } );
82
dc2fc680 83 if ( $options->{pidfile} ) {
84 $proc_manager->pm_write_pid_file( $options->{pidfile} );
85 }
6f409682 86
5898abae 87 $proc_manager->pm_manage();
88 }
e2fd5b5f 89
fbcc39ad 90 while ( $request->Accept >= 0 ) {
5898abae 91 $proc_manager && $proc_manager->pm_pre_dispatch();
84528885 92 $class->handle_request( env => \%env );
5898abae 93 $proc_manager && $proc_manager->pm_pre_dispatch();
fbcc39ad 94 }
e2fd5b5f 95}
96
fbcc39ad 97=item $self->write($c, $buffer)
e2fd5b5f 98
99=cut
100
fbcc39ad 101sub write {
102 my ( $self, $c, $buffer ) = @_;
4f5ebacd 103
fbcc39ad 104 unless ( $self->{_prepared_write} ) {
4f5ebacd 105 $self->prepare_write($c);
fbcc39ad 106 $self->{_prepared_write} = 1;
107 }
4f5ebacd 108
fbcc39ad 109 # FastCGI does not stream data properly if using 'print $handle',
110 # but a syswrite appears to work properly.
4f5ebacd 111 *STDOUT->syswrite($buffer);
e2fd5b5f 112}
113
198b0efa 1141;
115__END__
116
fbcc39ad 117=back
e2fd5b5f 118
198b0efa 119=head1 WEB SERVER CONFIGURATIONS
120
121=head2 Apache 1.x, 2.x
122
123Apache requires the mod_fastcgi module. The following config will let Apache
124control the running of your FastCGI processes.
125
126 # Launch the FastCGI processes
127 FastCgiIpcDir /tmp
8f753f10 128 FastCgiServer /var/www/MyApp/script/myapp_fastcgi.pl -idle-timeout 300 -processes 5
198b0efa 129
130 <VirtualHost *>
131 ScriptAlias / /var/www/MyApp/script/myapp_fastcgi.pl/
132 </VirtualHost>
133
134You can also tell Apache to connect to an external FastCGI server:
135
136 # Start the external server (requires FCGI::ProcManager)
137 $ script/myapp_fastcgi.pl -l /tmp/myapp.socket -n 5
138
139 # Note that the path used in FastCgiExternalServer can be any path
140 FastCgiIpcDir /tmp
141 FastCgiExternalServer /tmp/myapp_fastcgi.pl -socket /tmp/myapp.socket
142
143 <VirtualHost *>
144 ScriptAlias / /tmp/myapp_fastcgi.pl/
145 </VirtualHost>
146
147For more information on using FastCGI under Apache, visit
148L<http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html>
149
150=head2 Lighttpd
151
152This configuration was tested with Lighttpd 1.4.7.
153
154 server.document-root = "/var/www/MyApp/root"
155
156 fastcgi.server = (
157 "" => (
158 "MyApp" => (
159 "socket" => "/tmp/myapp.socket",
160 "check-local" => "disable",
161 "bin-path" => "/var/www/MyApp/script/myapp_fastcgi.pl",
162 "min-procs" => 2,
163 "max-procs" => 5,
164 "idle-timeout" => 20
165 )
166 )
167 )
168
25810c41 169You can also run your application at any non-root location.
170
171 fastcgi.server = (
172 "/myapp" => (
173 "MyApp" => (
174 # same as above
175 )
176 )
177 )
178
179You can also use an external server:
198b0efa 180
181 # Start the external server (requires FCGI::ProcManager)
182 $ script/myapp_fastcgi.pl -l /tmp/myapp.socket -n 5
183
184 server.document-root = "/var/www/MyApp/root"
185
186 fastcgi.server = (
187 "" => (
188 "MyApp" => (
189 "socket" => "/tmp/myapp.socket",
190 "check-local" => "disable"
191 )
192 )
193 )
194
195For more information on using FastCGI under Lighttpd, visit
196L<http://www.lighttpd.net/documentation/fastcgi.html>
197
198=head2 IIS
199
200It is possible to run Catalyst under IIS with FastCGI, but we do not
201yet have detailed instructions.
202
fbcc39ad 203=head1 SEE ALSO
e2fd5b5f 204
fbcc39ad 205L<Catalyst>, L<FCGI>.
cd3bb248 206
fbcc39ad 207=head1 AUTHORS
ffb41d94 208
fbcc39ad 209Sebastian Riedel, <sri@cpan.org>
ffb41d94 210
fbcc39ad 211Christian Hansen, <ch@ngmedia.com>
ffb41d94 212
fbcc39ad 213Andy Grundman, <andy@hybridized.org>
ffb41d94 214
215=head1 COPYRIGHT
216
217This program is free software, you can redistribute it and/or modify it under
218the same terms as Perl itself.
219
220=cut