| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | |||
| 5 | package Jifty::Script::FastCGI; | ||
| 6 | use base qw/Jifty::Script/; | ||
| 7 | |||
| 8 | use Plack::Handler::FCGI; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Jifty::Script::FastCGI - A FastCGI server for your Jifty application | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | AddHandler fastcgi-script fcgi | ||
| 17 | FastCgiServer /path/to/your/jifty/app/bin/jifty -initial-env JIFTY_COMMAND=fastcgi | ||
| 18 | |||
| 19 | Options: | ||
| 20 | --maxrequests maximum number of requests per process | ||
| 21 | |||
| 22 | --help brief help message | ||
| 23 | --man full documentation | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | FastCGI entry point for your Jifty application | ||
| 28 | |||
| 29 | =head2 options | ||
| 30 | |||
| 31 | =over 8 | ||
| 32 | |||
| 33 | =item B<--maxrequests> | ||
| 34 | |||
| 35 | Set maximum number of requests per process. Read also --man. | ||
| 36 | |||
| 37 | =item B<--help> | ||
| 38 | |||
| 39 | Print a brief help message and exits. | ||
| 40 | |||
| 41 | =item B<--man> | ||
| 42 | |||
| 43 | Prints the manual page and exits. | ||
| 44 | |||
| 45 | =back | ||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | sub options { | ||
| 50 | my $self = shift; | ||
| 51 | return ( | ||
| 52 | $self->SUPER::options, | ||
| 53 | 'maxrequests=i' => 'maxrequests', | ||
| 54 | ); | ||
| 55 | } | ||
| 56 | |||
| 57 | =head1 DESCRIPTION | ||
| 58 | |||
| 59 | When you're ready to move up to something that can handle the increasing load your | ||
| 60 | new world-changing application is generating, you'll need something a bit heavier-duty | ||
| 61 | than the pure-perl Jifty standalone server. C<FastCGI> is what you're looking for. | ||
| 62 | |||
| 63 | If you have MaxRequests options under FastCGI in your config.yml, or | ||
| 64 | command-line option C<--maxrequests=N> assigned, the FastCGI process | ||
| 65 | will exit after serving N requests. | ||
| 66 | |||
| 67 | =head1 SERVER CONFIGURATIONS | ||
| 68 | |||
| 69 | =head2 Apache + mod_fastcgi | ||
| 70 | |||
| 71 | # These two lines are FastCGI-specific; skip them to run in vanilla CGI mode | ||
| 72 | AddHandler fastcgi-script fcgi | ||
| 73 | FastCgiServer /path/to/your/jifty/app/bin/jifty -initial-env JIFTY_COMMAND=fastcgi | ||
| 74 | |||
| 75 | DocumentRoot /path/to/your/jifty/app/share/web/templates | ||
| 76 | ScriptAlias / /path/to/your/jifty/app/bin/jifty/ | ||
| 77 | |||
| 78 | =head2 Apache + mod_fcgid + mod_rewrite | ||
| 79 | |||
| 80 | An alternative to Apache mod_fastcgi is to use mod_fcgid with mod_rewrite. | ||
| 81 | If you use mod_fcgid and mod_rewrite, you can use this in your Apache | ||
| 82 | configuration instead: | ||
| 83 | |||
| 84 | DocumentRoot /path/to/your/jifty/app/share/web/templates | ||
| 85 | ScriptAlias /cgi-bin /path/to/your/jifty/app/bin | ||
| 86 | DefaultInitEnv JIFTY_COMMAND fastcgi | ||
| 87 | <Directory /path/to/your/jifty/app/bin> | ||
| 88 | Options ExecCGI | ||
| 89 | SetHandler fcgid-script | ||
| 90 | </Directory> | ||
| 91 | <Directory /path/to/your/jifty/app/share/web/templates> | ||
| 92 | RewriteEngine on | ||
| 93 | RewriteRule ^$ index.html [QSA] | ||
| 94 | RewriteRule ^(.*)$ /cgi-bin/jifty/$1 [QSA,L] | ||
| 95 | </Directory> | ||
| 96 | |||
| 97 | It may be possible to do this without using mod_rewrite. | ||
| 98 | |||
| 99 | =head2 Lighttpd (L<http://www.lighttpd.net/>) | ||
| 100 | |||
| 101 | Version 1.4.23 or newer is recommended, and you may adapt this config: | ||
| 102 | |||
| 103 | server.modules = ( "mod_fastcgi" ) | ||
| 104 | server.document-root = "/path/to/your/jifty/app/share/web/templates" | ||
| 105 | fastcgi.server = ( | ||
| 106 | "/" => ( | ||
| 107 | "your_jifty_app" => ( | ||
| 108 | "socket" => "/tmp/your_jifty_app.socket", | ||
| 109 | "check-local" => "disable", | ||
| 110 | "fix-root-scriptname" => "enable", | ||
| 111 | "bin-path" => "/path/to/your/jifty/app/bin/jifty", | ||
| 112 | "bin-environment" => ( "JIFTY_COMMAND" => "fastcgi" ), | ||
| 113 | "min-procs" => 1, | ||
| 114 | "max-procs" => 5, | ||
| 115 | "max-load-per-proc" => 1, | ||
| 116 | "idle-timeout" => 20, | ||
| 117 | ) | ||
| 118 | ) | ||
| 119 | ) | ||
| 120 | |||
| 121 | Versions before 1.4.23 will work, but you should read L<Plack::Handler::FCGI's lighttpd | ||
| 122 | documentation|http://search.cpan.org/dist/Plack/lib/Plack/Handler/FCGI.pm#lighttpd> | ||
| 123 | for how to configure your server. | ||
| 124 | |||
| 125 | =head2 More information | ||
| 126 | |||
| 127 | Since this uses L<Plack::Handler::FCGI>, you might also want to read | ||
| 128 | L<its documentation on webserver | ||
| 129 | configurations|http://search.cpan.org/dist/Plack/lib/Plack/Handler/FCGI.pm#WEB_SERVER_CONFIGURATIONS>. | ||
| 130 | |||
| 131 | =head1 METHODS | ||
| 132 | |||
| 133 | =head2 run | ||
| 134 | |||
| 135 | Creates a new FastCGI process. | ||
| 136 | |||
| 137 | =cut | ||
| 138 | |||
| 139 | sub run { | ||
| 140 | my $self = shift; | ||
| 141 | |||
| 142 | $self->print_help; | ||
| 143 | |||
| 144 | Jifty->new(); | ||
| 145 | my $conf = Jifty->config->framework('Web')->{'FastCGI'} || {}; | ||
| 146 | $self->{maxrequests} ||= $conf->{MaxRequests}; # XXX: make it work | ||
| 147 | |||
| 148 | my $server = Plack::Handler::FCGI->new( | ||
| 149 | nproc => $conf->{NProc} || 1, | ||
| 150 | detach => 1, | ||
| 151 | ); | ||
| 152 | |||
| 153 | $server->run(Jifty->handler->psgi_app); | ||
| 154 | } | ||
| 155 | |||
| 156 | 1; |