Commit | Line | Data |
d1d9793f |
1 | package Catalyst::Engine::FastCGI; |
ffb41d94 |
2 | |
3 | use strict; |
fbcc39ad |
4 | use base 'Catalyst::Engine::CGI'; |
5 | use FCGI; |
ffb41d94 |
6 | |
7 | =head1 NAME |
8 | |
fbcc39ad |
9 | Catalyst::Engine::FastCGI - FastCGI Engine |
ffb41d94 |
10 | |
11 | =head1 DESCRIPTION |
12 | |
fbcc39ad |
13 | This is the FastCGI engine. |
ffb41d94 |
14 | |
e2fd5b5f |
15 | =head1 OVERLOADED METHODS |
16 | |
fbcc39ad |
17 | This class overloads some methods from C<Catalyst::Engine::CGI>. |
e2fd5b5f |
18 | |
19 | =over 4 |
20 | |
5898abae |
21 | =item $self->run($c, $listen, { option => value, ... }) |
22 | |
23 | Starts the FastCGI server. If C<$listen> is set, then it specifies a |
24 | location to listen for FastCGI requests; |
25 | |
26 | Form Meaning |
27 | /path listen via Unix sockets on /path |
28 | :port listen via TCP on port on all interfaces |
29 | hostname:port listen via TCP on port bound to hostname |
30 | |
31 | Options may also be specified; |
32 | |
33 | Option Meaning |
34 | leave_umask Set to 1 to disable setting umask to 0 |
35 | for socket open |
36 | nointr Do not allow the listener to be |
37 | interrupted by Ctrl+C |
38 | nproc Specify a number of processes for |
39 | FCGI::ProcManager |
e2fd5b5f |
40 | |
41 | =cut |
42 | |
fbcc39ad |
43 | sub run { |
5898abae |
44 | my ( $self, $class, $listen, $options ) = @_; |
45 | |
46 | my $sock; |
47 | if ($listen) { |
48 | my $old_umask = umask; |
49 | unless ( $options->{leave_umask} ) { |
50 | umask(0); |
51 | } |
52 | $sock = FCGI::OpenSocket( $listen, 100 ) |
53 | or die "failed to open FastCGI socket; $!"; |
54 | unless ( $options->{leave_umask} ) { |
55 | umask($old_umask); |
56 | } |
57 | } |
58 | else { |
59 | -S STDIN |
60 | or die "STDIN is not a socket; specify a listen location"; |
61 | } |
62 | |
63 | $options ||= {}; |
e2fd5b5f |
64 | |
5898abae |
65 | my $request = |
66 | FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%ENV, $sock, |
67 | ( $options->{nointr} ? 0 : &FCGI::FAIL_ACCEPT_ON_INTR ), |
68 | ); |
69 | |
70 | my $proc_manager; |
71 | |
72 | if ( $listen and ( $options->{nproc} || 1 ) > 1 ) { |
73 | require FCGI::ProcManager; |
74 | $proc_manager = |
75 | FCGI::ProcManager->new( { n_processes => $options->{nproc} } ); |
76 | $proc_manager->pm_manage(); |
77 | } |
e2fd5b5f |
78 | |
fbcc39ad |
79 | while ( $request->Accept >= 0 ) { |
5898abae |
80 | $proc_manager && $proc_manager->pm_pre_dispatch(); |
fbcc39ad |
81 | $class->handle_request; |
5898abae |
82 | $proc_manager && $proc_manager->pm_pre_dispatch(); |
fbcc39ad |
83 | } |
e2fd5b5f |
84 | } |
85 | |
fbcc39ad |
86 | =item $self->write($c, $buffer) |
e2fd5b5f |
87 | |
88 | =cut |
89 | |
fbcc39ad |
90 | sub write { |
91 | my ( $self, $c, $buffer ) = @_; |
4f5ebacd |
92 | |
fbcc39ad |
93 | unless ( $self->{_prepared_write} ) { |
4f5ebacd |
94 | $self->prepare_write($c); |
fbcc39ad |
95 | $self->{_prepared_write} = 1; |
96 | } |
4f5ebacd |
97 | |
fbcc39ad |
98 | # FastCGI does not stream data properly if using 'print $handle', |
99 | # but a syswrite appears to work properly. |
4f5ebacd |
100 | *STDOUT->syswrite($buffer); |
e2fd5b5f |
101 | } |
102 | |
198b0efa |
103 | 1; |
104 | __END__ |
105 | |
fbcc39ad |
106 | =back |
e2fd5b5f |
107 | |
198b0efa |
108 | =head1 WEB SERVER CONFIGURATIONS |
109 | |
110 | =head2 Apache 1.x, 2.x |
111 | |
112 | Apache requires the mod_fastcgi module. The following config will let Apache |
113 | control the running of your FastCGI processes. |
114 | |
115 | # Launch the FastCGI processes |
116 | FastCgiIpcDir /tmp |
117 | FastCgiServer /var/www/MyApp/script/myapp_fastcgi.pl -idle_timeout 300 -processes 5 |
118 | |
119 | <VirtualHost *> |
120 | ScriptAlias / /var/www/MyApp/script/myapp_fastcgi.pl/ |
121 | </VirtualHost> |
122 | |
123 | You can also tell Apache to connect to an external FastCGI server: |
124 | |
125 | # Start the external server (requires FCGI::ProcManager) |
126 | $ script/myapp_fastcgi.pl -l /tmp/myapp.socket -n 5 |
127 | |
128 | # Note that the path used in FastCgiExternalServer can be any path |
129 | FastCgiIpcDir /tmp |
130 | FastCgiExternalServer /tmp/myapp_fastcgi.pl -socket /tmp/myapp.socket |
131 | |
132 | <VirtualHost *> |
133 | ScriptAlias / /tmp/myapp_fastcgi.pl/ |
134 | </VirtualHost> |
135 | |
136 | For more information on using FastCGI under Apache, visit |
137 | L<http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html> |
138 | |
139 | =head2 Lighttpd |
140 | |
141 | This configuration was tested with Lighttpd 1.4.7. |
142 | |
143 | server.document-root = "/var/www/MyApp/root" |
144 | |
145 | fastcgi.server = ( |
146 | "" => ( |
147 | "MyApp" => ( |
148 | "socket" => "/tmp/myapp.socket", |
149 | "check-local" => "disable", |
150 | "bin-path" => "/var/www/MyApp/script/myapp_fastcgi.pl", |
151 | "min-procs" => 2, |
152 | "max-procs" => 5, |
153 | "idle-timeout" => 20 |
154 | ) |
155 | ) |
156 | ) |
157 | |
25810c41 |
158 | You can also run your application at any non-root location. |
159 | |
160 | fastcgi.server = ( |
161 | "/myapp" => ( |
162 | "MyApp" => ( |
163 | # same as above |
164 | ) |
165 | ) |
166 | ) |
167 | |
168 | You can also use an external server: |
198b0efa |
169 | |
170 | # Start the external server (requires FCGI::ProcManager) |
171 | $ script/myapp_fastcgi.pl -l /tmp/myapp.socket -n 5 |
172 | |
173 | server.document-root = "/var/www/MyApp/root" |
174 | |
175 | fastcgi.server = ( |
176 | "" => ( |
177 | "MyApp" => ( |
178 | "socket" => "/tmp/myapp.socket", |
179 | "check-local" => "disable" |
180 | ) |
181 | ) |
182 | ) |
183 | |
184 | For more information on using FastCGI under Lighttpd, visit |
185 | L<http://www.lighttpd.net/documentation/fastcgi.html> |
186 | |
187 | =head2 IIS |
188 | |
189 | It is possible to run Catalyst under IIS with FastCGI, but we do not |
190 | yet have detailed instructions. |
191 | |
fbcc39ad |
192 | =head1 SEE ALSO |
e2fd5b5f |
193 | |
fbcc39ad |
194 | L<Catalyst>, L<FCGI>. |
cd3bb248 |
195 | |
fbcc39ad |
196 | =head1 AUTHORS |
ffb41d94 |
197 | |
fbcc39ad |
198 | Sebastian Riedel, <sri@cpan.org> |
ffb41d94 |
199 | |
fbcc39ad |
200 | Christian Hansen, <ch@ngmedia.com> |
ffb41d94 |
201 | |
fbcc39ad |
202 | Andy Grundman, <andy@hybridized.org> |
ffb41d94 |
203 | |
204 | =head1 COPYRIGHT |
205 | |
206 | This program is free software, you can redistribute it and/or modify it under |
207 | the same terms as Perl itself. |
208 | |
209 | =cut |