2005-02-20 14:27:00 +00:00
|
|
|
=====================
|
|
|
|
the FastCGI Interface
|
|
|
|
=====================
|
|
|
|
|
|
|
|
-------------------
|
|
|
|
Module: mod_fastcgi
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
:Author: Jan Kneschke
|
|
|
|
:Date: $Date: 2004/11/03 22:26:05 $
|
|
|
|
:Revision: $Revision: 1.3 $
|
|
|
|
|
|
|
|
:abstract:
|
|
|
|
The FastCGI interface is the fastest and most secure way
|
2006-10-04 13:26:23 +00:00
|
|
|
to interface external process-handlers like Perl, PHP and
|
|
|
|
your self-written applications.
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
.. meta::
|
|
|
|
:keywords: lighttpd, FastCGI
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
.. contents:: Table of Contents
|
|
|
|
|
|
|
|
Description
|
|
|
|
===========
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
lighttpd provides an interface to a external programs that
|
|
|
|
support the FastCGI interface. The FastCGI Interface is
|
|
|
|
defined by http://www.fastcgi.com/ and is a
|
2005-10-31 18:43:46 +00:00
|
|
|
platform-independent and server independent interface between
|
2005-02-20 14:27:00 +00:00
|
|
|
a web-application and a webserver.
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
This means that FastCGI programs that run with the Apache
|
2005-02-20 14:27:00 +00:00
|
|
|
Webserver will run seamlessly with lighttpd and vice versa.
|
|
|
|
|
|
|
|
|
|
|
|
FastCGI
|
|
|
|
-------
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
FastCGI is removes a lot of the limitations of CGI programs.
|
|
|
|
CGI programs have the problem that they have to be restarted
|
|
|
|
by the webserver for every request which leads to really bad
|
2005-02-20 14:27:00 +00:00
|
|
|
performance values.
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
FastCGI removes this limitation by keeping the process running
|
|
|
|
and handling the requests by this always running process. This
|
|
|
|
removes the time used for the fork() and the overall startup
|
|
|
|
and cleanup time which is necessary to create and destroy a
|
2005-02-20 14:27:00 +00:00
|
|
|
process.
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
While CGI programs communicate to the server over pipes,
|
|
|
|
FastCGI processes use Unix-Domain-Sockets or TCP/IP to talk
|
|
|
|
with the webserver. This gives you the second advantage over
|
2005-02-20 14:27:00 +00:00
|
|
|
simple CGI programs: FastCGI don't have to run on the Webserver
|
2006-10-04 13:26:23 +00:00
|
|
|
itself but everywhere in the network.
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
lighttpd takes it a little bit further by providing a internal
|
|
|
|
FastCGI load-balancer which can be used to balance the load
|
|
|
|
over multiple FastCGI Servers. In contrast to other solutions
|
|
|
|
only the FastCGI process has to be on the cluster and not the
|
2005-02-20 14:27:00 +00:00
|
|
|
whole webserver. That gives the FastCGI process more resources
|
|
|
|
than a e.g. load-balancer+apache+mod_php solution.
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
If you compare FastCGI against a apache+mod_php solution you
|
|
|
|
should note that FastCGI provides additional security as the
|
|
|
|
FastCGI process can be run under different permissions that
|
|
|
|
the webserver and can also live a chroot which might be
|
|
|
|
different than the one the webserver is running in.
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
Options
|
|
|
|
=======
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
lighttpd provides the FastCGI support via the fastcgi-module
|
2005-02-20 14:27:00 +00:00
|
|
|
(mod_fastcgi) which provides 2 options in the config-file:
|
|
|
|
|
|
|
|
fastcgi.debug
|
2006-10-04 13:26:23 +00:00
|
|
|
a value between 0 and 65535 to set the debug-level in the
|
|
|
|
FastCGI module. Currently only 0 and 1 are used. Use 1 to
|
2005-02-20 14:27:00 +00:00
|
|
|
enable some debug output, 0 to disable it.
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
fastcgi.map-extensions
|
2006-01-31 12:05:03 +00:00
|
|
|
map multiple extensions to the same fastcgi server
|
|
|
|
|
|
|
|
Example: ::
|
|
|
|
|
|
|
|
fastcgi.map-extensions = ( ".php3" => ".php" )
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
fastcgi.server
|
2006-10-04 13:26:23 +00:00
|
|
|
tell the module where to send FastCGI requests to. Every
|
|
|
|
file-extension can have it own handler. Load-Balancing is
|
2005-02-20 14:27:00 +00:00
|
|
|
done by specifying multiple handles for the same extension.
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
structure of fastcgi.server section: ::
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
( <extension> =>
|
|
|
|
(
|
2005-02-20 14:27:00 +00:00
|
|
|
( "host" => <string> ,
|
|
|
|
"port" => <integer> ,
|
2009-07-23 21:54:51 +00:00
|
|
|
"socket" => <string>, # either socket
|
|
|
|
# or host+port
|
|
|
|
"bin-path" => <string>, # OPTIONAL
|
|
|
|
"bin-environment" => <array>, # OPTIONAL
|
|
|
|
"bin-copy-environment" => <array>, # OPTIONAL
|
2005-02-20 14:27:00 +00:00
|
|
|
"mode" => <string>, # OPTIONAL
|
2006-10-04 13:26:23 +00:00
|
|
|
"docroot" => <string> , # OPTIONAL if "mode"
|
2009-07-23 21:54:51 +00:00
|
|
|
# is not "authorizer"
|
2005-02-20 14:27:00 +00:00
|
|
|
"check-local" => <string>, # OPTIONAL
|
2009-07-23 21:54:51 +00:00
|
|
|
"max-procs" => <integer>, # OPTIONAL
|
|
|
|
"broken-scriptfilename" => <boolean>, # OPTIONAL
|
2005-09-14 16:58:23 +00:00
|
|
|
"disable-time" => <integer>, # optional
|
[mod_fastcgi] use http_response_xsendfile() (fixes #799, fixes #851, fixes #2017, fixes #2076)
handle X-Sendfile and X-LIGHTTPD-send-file w/ http_response_xsendfile()
if host is configured ( "x-sendfile" = "enable" )
Note: X-Sendfile path is url-decoded for consistency, like X-Sendfile2
(response headers should be url-encoded to avoid tripping over
chars allowed in filesystem but which might change response
header parsing semantics)
Note: deprecated: "allow-x-send-file"; use "x-sendfile"
Note: deprecated: X-LIGHTTPD-send-file header; use X-Sendfile header
Note: deprecated: X-Sendfile2 header; use X-Sendfile header
For now, X-Sendfile2 is still handled internally by mod_fastcgi.
Since http_response_send_file() supports HTTP Range requests,
X-Sendfile2 is effectively obsolete. However, any code, e.g. PHP,
currently using X-Sendfile2 is probably manually generating 206 Partial
Content status and Range response headers. A future version of lighttpd
might *remove* X-Sendfile2. Existing code should be converted to use
X-Sendfile, which is easily done by removing all the special logic
around using X-Sendfile2, since the 206 Partial Content status and Range
response headers are handled in http_response_send_file().
x-ref:
"mod_fastcgi + X-Sendfile -> mod_staticfile"
https://redmine.lighttpd.net/issues/799
"Feature Request: New option "x-send-file-docroot""
https://redmine.lighttpd.net/issues/851
"X-Sendfile handoff to mod-static-file in 1.4.x"
https://redmine.lighttpd.net/issues/2017
"X-sendfile should be able to set content-type"
https://redmine.lighttpd.net/issues/2076
2016-04-22 01:01:30 +00:00
|
|
|
"x-sendfile" => <boolean>, # optional (replaces "allow-x-send-file")
|
|
|
|
"x-sendfile-docroot" => <boolean>, # optional
|
2009-07-23 21:54:51 +00:00
|
|
|
"kill-signal" => <integer>, # OPTIONAL
|
|
|
|
"fix-root-scriptname" => <boolean>,
|
|
|
|
# OPTIONAL
|
2006-10-04 13:26:23 +00:00
|
|
|
( "host" => ...
|
|
|
|
)
|
2005-02-20 14:27:00 +00:00
|
|
|
)
|
|
|
|
)
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
:<extension>: is the file-extension or prefix
|
2005-02-20 14:27:00 +00:00
|
|
|
(if started with "/")
|
|
|
|
:"host": is hostname/ip of the FastCGI process
|
|
|
|
:"port": is tcp-port on the "host" used by the FastCGI
|
|
|
|
process
|
2006-10-04 13:26:23 +00:00
|
|
|
:"bin-path": path to the local FastCGI binary which should be
|
2005-02-20 14:27:00 +00:00
|
|
|
started if no local FastCGI is running
|
|
|
|
:"socket": path to the unix-domain socket
|
2006-10-04 13:26:23 +00:00
|
|
|
:"mode": is the FastCGI protocol mode.
|
|
|
|
Default is "responder", also "authorizer"
|
2009-07-23 21:54:51 +00:00
|
|
|
mode is implemented.
|
2006-10-04 13:26:23 +00:00
|
|
|
:"docroot": is optional and is the docroot on the remote
|
|
|
|
host for default "responder" mode. For
|
2009-07-23 21:54:51 +00:00
|
|
|
"authorizer" mode it is MANDATORY and it points
|
|
|
|
to docroot for authorized requests. For security
|
|
|
|
reasons it is recommended to keep this docroot
|
2005-02-20 14:27:00 +00:00
|
|
|
outside of server.document-root tree.
|
2006-10-04 13:26:23 +00:00
|
|
|
:"check-local": is optional and may be "enable" (default) or
|
|
|
|
"disable". If enabled the server first check
|
2009-07-23 21:54:51 +00:00
|
|
|
for a file in local server.document-root tree
|
|
|
|
and return 404 (Not Found) if no such file.
|
2006-10-04 13:26:23 +00:00
|
|
|
If disabled, the server forward request to
|
2009-07-23 21:54:51 +00:00
|
|
|
FastCGI interface without this check.
|
2006-10-04 13:26:23 +00:00
|
|
|
:"broken-scriptfilename": breaks SCRIPT_FILENAME in a wat that
|
2005-07-29 09:12:40 +00:00
|
|
|
PHP can extract PATH_INFO from it (default: disabled)
|
2005-09-14 16:58:23 +00:00
|
|
|
:"disable-time": time to wait before a disabled backend is checked
|
|
|
|
again
|
[mod_fastcgi] use http_response_xsendfile() (fixes #799, fixes #851, fixes #2017, fixes #2076)
handle X-Sendfile and X-LIGHTTPD-send-file w/ http_response_xsendfile()
if host is configured ( "x-sendfile" = "enable" )
Note: X-Sendfile path is url-decoded for consistency, like X-Sendfile2
(response headers should be url-encoded to avoid tripping over
chars allowed in filesystem but which might change response
header parsing semantics)
Note: deprecated: "allow-x-send-file"; use "x-sendfile"
Note: deprecated: X-LIGHTTPD-send-file header; use X-Sendfile header
Note: deprecated: X-Sendfile2 header; use X-Sendfile header
For now, X-Sendfile2 is still handled internally by mod_fastcgi.
Since http_response_send_file() supports HTTP Range requests,
X-Sendfile2 is effectively obsolete. However, any code, e.g. PHP,
currently using X-Sendfile2 is probably manually generating 206 Partial
Content status and Range response headers. A future version of lighttpd
might *remove* X-Sendfile2. Existing code should be converted to use
X-Sendfile, which is easily done by removing all the special logic
around using X-Sendfile2, since the 206 Partial Content status and Range
response headers are handled in http_response_send_file().
x-ref:
"mod_fastcgi + X-Sendfile -> mod_staticfile"
https://redmine.lighttpd.net/issues/799
"Feature Request: New option "x-send-file-docroot""
https://redmine.lighttpd.net/issues/851
"X-Sendfile handoff to mod-static-file in 1.4.x"
https://redmine.lighttpd.net/issues/2017
"X-sendfile should be able to set content-type"
https://redmine.lighttpd.net/issues/2076
2016-04-22 01:01:30 +00:00
|
|
|
:"x-sendfile": controls if X-Sendfile backend response header is allowed
|
|
|
|
(deprecated headers: X-Sendfile2 and X-LIGHTTPD-send-file)
|
|
|
|
("x-sendfile" replaces "allow-x-sendfile")
|
|
|
|
:"x-sendfile-docroot": list of directory trees permitted with X-Sendfile
|
2009-07-23 21:54:51 +00:00
|
|
|
:"fix-root-scriptname": fix broken path-info split for "/" extension ("prefix")
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
If bin-path is set:
|
|
|
|
|
|
|
|
:"max-procs": the upper limit of the processess to start
|
2006-10-04 13:26:23 +00:00
|
|
|
:"bin-environment": put an entry into the environment of
|
2005-02-20 14:27:00 +00:00
|
|
|
the started process
|
|
|
|
:"bin-copy-environement": clean up the environment and copy
|
2006-10-04 13:26:23 +00:00
|
|
|
only the specified entries into the fresh
|
2005-02-20 14:27:00 +00:00
|
|
|
environment of the spawn process
|
2009-07-23 21:54:51 +00:00
|
|
|
:"kill-signal": signal to terminate the FastCGI process with,
|
|
|
|
defauls to SIGTERM
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
Examples
|
|
|
|
--------
|
|
|
|
|
|
|
|
Multiple extensions for the same host ::
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
fastcgi.server = ( ".php" =>
|
2006-10-04 13:26:23 +00:00
|
|
|
(( "host" => "127.0.0.1",
|
2005-12-13 11:36:15 +00:00
|
|
|
"port" => 1026,
|
2009-07-23 21:54:51 +00:00
|
|
|
"bin-path" => "/usr/local/bin/php"
|
2005-12-13 11:36:15 +00:00
|
|
|
)),
|
|
|
|
".php4" =>
|
|
|
|
(( "host" => "127.0.0.1",
|
2009-07-23 21:54:51 +00:00
|
|
|
"port" => 1026
|
2005-12-13 11:36:15 +00:00
|
|
|
))
|
|
|
|
)
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
Example with prefix: ::
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-09-14 16:58:23 +00:00
|
|
|
fastcgi.server = ( "/remote_scripts/" =>
|
2005-12-13 11:36:15 +00:00
|
|
|
(( "host" => "192.168.0.3",
|
2009-07-23 21:54:51 +00:00
|
|
|
"port" => 9000,
|
2005-12-13 11:36:15 +00:00
|
|
|
"check-local" => "disable",
|
2006-10-04 13:26:23 +00:00
|
|
|
"docroot" => "/" # remote server may use
|
2009-07-23 21:54:51 +00:00
|
|
|
# it's own docroot
|
2005-12-13 11:36:15 +00:00
|
|
|
))
|
|
|
|
)
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
The request `http://my.host.com/remote_scripts/test.cgi` will
|
|
|
|
be forwarded to fastcgi server at 192.168.0.3 and the value
|
|
|
|
"/remote_scripts/test.cgi" will be used for the SCRIPT_NAME
|
2006-10-04 13:26:23 +00:00
|
|
|
variable. Remote server may prepend it with its own
|
|
|
|
document root. The handling of index files is also the
|
2005-02-20 14:27:00 +00:00
|
|
|
resposibility of remote server for this case.
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
In the case that the prefix is not terminated with a slash
|
2005-09-14 16:58:23 +00:00
|
|
|
the prefix will be handled as file and /test.cgi would become
|
|
|
|
a PATH_INFO instead of part of SCRIPT_NAME.
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
Example for "authorizer" mode: ::
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-09-14 16:58:23 +00:00
|
|
|
fastcgi.server = ( "/remote_scripts/" =>
|
2005-12-13 11:36:15 +00:00
|
|
|
(( "host" => "10.0.0.2",
|
2009-07-23 21:54:51 +00:00
|
|
|
"port" => 9000,
|
2005-12-13 11:36:15 +00:00
|
|
|
"docroot" => "/path_to_private_docs",
|
|
|
|
"mode" => "authorizer"
|
|
|
|
))
|
|
|
|
)
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
Note that if "docroot" is specified then its value will be
|
2005-02-20 14:27:00 +00:00
|
|
|
used in DOCUMENT_ROOT and SCRIPT_FILENAME variables passed
|
|
|
|
to FastCGI server.
|
|
|
|
|
|
|
|
Load-Balancing
|
|
|
|
==============
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
The FastCGI plugin provides automaticly a load-balancing between
|
2005-02-20 14:27:00 +00:00
|
|
|
multiple FastCGI servers. ::
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
fastcgi.server = ( ".php" =>
|
2005-12-13 11:36:15 +00:00
|
|
|
(( "host" => "10.0.0.2", "port" => 1030 ),
|
|
|
|
( "host" => "10.0.0.3", "port" => 1030 ))
|
|
|
|
)
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
To understand how the load-balancing works you can enable the
|
2005-02-20 14:27:00 +00:00
|
|
|
fastcgi.debug option and will get a similar output as here: ::
|
|
|
|
|
|
|
|
proc: 127.0.0.1 1031 1 1 1 31454
|
|
|
|
proc: 127.0.0.1 1028 1 1 1 31442
|
|
|
|
proc: 127.0.0.1 1030 1 1 1 31449
|
|
|
|
proc: 127.0.0.1 1029 1 1 2 31447
|
|
|
|
proc: 127.0.0.1 1026 1 1 2 31438
|
|
|
|
got proc: 34 31454
|
|
|
|
release proc: 40 31438
|
|
|
|
proc: 127.0.0.1 1026 1 1 1 31438
|
|
|
|
proc: 127.0.0.1 1028 1 1 1 31442
|
|
|
|
proc: 127.0.0.1 1030 1 1 1 31449
|
|
|
|
proc: 127.0.0.1 1031 1 1 2 31454
|
|
|
|
proc: 127.0.0.1 1029 1 1 2 31447
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
Even if this for multiple FastCGI children on the local machine
|
2005-02-20 14:27:00 +00:00
|
|
|
the following explaination is valid for remote connections too.
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
The output shows:
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
- IP, port, unix-socket (is empty here)
|
|
|
|
- is-local, state (0 - unset, 1 - running, ... )
|
|
|
|
- active connections (load)
|
|
|
|
- PID
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
As you can see the list is always sorted by the load field.
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
Whenever a new connection is requested, the first entry (the one
|
|
|
|
with the lowest load) is selected, the load is increased (got proc: ...)
|
2005-02-20 14:27:00 +00:00
|
|
|
and the list is sorted again.
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
If a FastCGI request is done or the connection is dropped, the load on the
|
2005-02-20 14:27:00 +00:00
|
|
|
FastCGI proc decreases and the list is sorted again (release proc: ...)
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
This behaviour is very light-weight in code and still very efficient
|
|
|
|
as it keeps the fastcgi-servers equally loaded even if they have different
|
|
|
|
CPUs.
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
Adaptive Process Spawning
|
|
|
|
=========================
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
.. note:: This feature is disabled in 1.3.14 again. min-procs is
|
2005-06-15 10:59:50 +00:00
|
|
|
ignored in that release
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
Starting with 1.3.8 lighttpd can spawn processes on demand if
|
2005-02-20 14:27:00 +00:00
|
|
|
a bin-path is specified and the FastCGI process runs locally.
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
If you want to have a least one FastCGI process running and
|
|
|
|
more of the number of requests increases you can use min-procs
|
2005-02-20 14:27:00 +00:00
|
|
|
and max-procs.
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
A new process is spawned as soon as the average number of
|
2005-02-20 14:27:00 +00:00
|
|
|
requests waiting to be handle by a single process increases the
|
2006-10-04 13:26:23 +00:00
|
|
|
max-load-per-proc setting.
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
The idle-timeout specifies how long a fastcgi-process should wait
|
2006-10-04 13:26:23 +00:00
|
|
|
for a new request before it kills itself.
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
Example
|
|
|
|
-------
|
|
|
|
::
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
fastcgi.server = ( ".php" =>
|
2005-12-13 11:36:15 +00:00
|
|
|
(( "socket" => "/tmp/php.socket",
|
|
|
|
"bin-path" => "/usr/local/bin/php",
|
|
|
|
"min-procs" => 1,
|
|
|
|
"max-procs" => 32,
|
|
|
|
"max-load-per-proc" => 4,
|
2006-10-04 13:26:23 +00:00
|
|
|
"idle-timeout" => 20
|
2005-12-13 11:36:15 +00:00
|
|
|
))
|
|
|
|
)
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
Disabling Adaptive Spawning
|
|
|
|
---------------------------
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
Adaptive Spawning is a quite new feature and it might misbehave
|
|
|
|
for your setup. There are several ways to control how the spawing
|
2005-02-20 14:27:00 +00:00
|
|
|
is done:
|
|
|
|
|
|
|
|
1. ``"max-load-per-proc" => 1``
|
|
|
|
if that works for you, great.
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
2. If not set ``min-procs == max-procs``.
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
3. For PHP you can also use: ::
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
$ PHP_FCGI_CHILDREN=384 ./lighttpd -f ./lighttpd.conf
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
fastcgi.server = ( ".php" =>
|
2009-07-23 21:54:51 +00:00
|
|
|
(( "socket" => "/tmp/php.socket",
|
2005-12-13 11:36:15 +00:00
|
|
|
"bin-path" => "/usr/local/bin/php",
|
|
|
|
"min-procs" => 1,
|
2009-07-23 21:54:51 +00:00
|
|
|
"max-procs" => 1,
|
2005-12-13 11:36:15 +00:00
|
|
|
"max-load-per-proc" => 4,
|
2006-10-04 13:26:23 +00:00
|
|
|
"idle-timeout" => 20
|
2005-12-13 11:36:15 +00:00
|
|
|
))
|
|
|
|
)
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
It will create one socket and let's PHP create the 384 processes itself.
|
|
|
|
|
|
|
|
4. If you don't want lighttpd to manage the fastcgi processes, remove the
|
|
|
|
bin-path and use spawn-fcgi to spawn them itself.
|
|
|
|
|
|
|
|
|
|
|
|
FastCGI and Programming Languages
|
2006-10-04 13:26:23 +00:00
|
|
|
=================================
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
Preparing PHP as a FastCGI program
|
|
|
|
----------------------------------
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
One of the most important application that has a FastCGI
|
|
|
|
interface is php which can be downloaded from
|
|
|
|
http://www.php.net/ . You have to recompile the php from
|
|
|
|
source to enable the FastCGI interface as it is normally
|
2005-02-20 14:27:00 +00:00
|
|
|
not enabled by default in the distributions.
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
If you already have a working installation of PHP on a
|
2005-02-20 14:27:00 +00:00
|
|
|
webserver execute a small script which just contains ::
|
|
|
|
|
|
|
|
<?php phpinfo(); ?>
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
and search for the line in that contains the configure call.
|
|
|
|
You can use it as the base for the compilation.
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
You have to remove all occurences of `--with-apxs`, `--with-apxs2`
|
2005-02-20 14:27:00 +00:00
|
|
|
and the like which would build PHP with Apache support. Add the
|
|
|
|
next three switches to compile PHP with FastCGI support::
|
|
|
|
|
|
|
|
$ ./configure \
|
|
|
|
--enable-fastcgi \
|
|
|
|
--enable-force-cgi-redirect \
|
|
|
|
...
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
After compilation and installation check that your PHP
|
2005-02-20 14:27:00 +00:00
|
|
|
binary contains FastCGI support by calling: ::
|
|
|
|
|
|
|
|
$ php -v
|
|
|
|
PHP 4.3.3RC2-dev (cgi-fcgi) (built: Oct 19 2003 23:19:17)
|
|
|
|
|
|
|
|
The important part is the (cgi-fcgi).
|
|
|
|
|
|
|
|
|
|
|
|
Starting a FastCGI-PHP
|
|
|
|
----------------------
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
Starting with version 1.3.6 lighttpd can spawn the FastCGI
|
2005-02-20 14:27:00 +00:00
|
|
|
processes locally itself if necessary: ::
|
|
|
|
|
|
|
|
fastcgi.server = ( ".php" =>
|
2005-12-13 11:36:15 +00:00
|
|
|
(( "socket" => "/tmp/php-fastcgi.socket",
|
|
|
|
"bin-path" => "/usr/local/bin/php"
|
|
|
|
))
|
|
|
|
)
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
PHP provides 2 special environment variables which control the number of
|
|
|
|
spawned workes under the control of a single watching process
|
|
|
|
(PHP_FCGI_CHILDREN) and the number of requests what a single worker
|
|
|
|
handles before it kills itself. ::
|
|
|
|
|
|
|
|
fastcgi.server = ( ".php" =>
|
2005-12-13 11:36:15 +00:00
|
|
|
(( "socket" => "/tmp/php-fastcgi.socket",
|
|
|
|
"bin-path" => "/usr/local/bin/php",
|
2006-10-04 13:26:23 +00:00
|
|
|
"bin-environment" => (
|
2005-12-13 11:36:15 +00:00
|
|
|
"PHP_FCGI_CHILDREN" => "16",
|
|
|
|
"PHP_FCGI_MAX_REQUESTS" => "10000"
|
|
|
|
)
|
|
|
|
))
|
|
|
|
)
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
To increase the security of the started process you should only pass
|
2005-02-20 14:27:00 +00:00
|
|
|
the necessary environment variables to the FastCGI process. ::
|
|
|
|
|
|
|
|
fastcgi.server = ( ".php" =>
|
2005-12-13 11:36:15 +00:00
|
|
|
(( "socket" => "/tmp/php-fastcgi.socket",
|
|
|
|
"bin-path" => "/usr/local/bin/php",
|
2006-10-04 13:26:23 +00:00
|
|
|
"bin-environment" => (
|
2005-12-13 11:36:15 +00:00
|
|
|
"PHP_FCGI_CHILDREN" => "16",
|
|
|
|
"PHP_FCGI_MAX_REQUESTS" => "10000" ),
|
|
|
|
"bin-copy-environment" => (
|
2009-07-23 21:54:51 +00:00
|
|
|
"PATH", "SHELL", "USER" )
|
2005-12-13 11:36:15 +00:00
|
|
|
))
|
|
|
|
)
|
2005-08-19 14:12:30 +00:00
|
|
|
|
2005-08-15 09:57:11 +00:00
|
|
|
Configuring PHP
|
|
|
|
---------------
|
|
|
|
|
2005-08-17 09:51:24 +00:00
|
|
|
If you want to use PATH_INFO and PHP_SELF in you PHP scripts you have to
|
|
|
|
configure php and lighttpd. The php.ini needs the option: ::
|
2005-08-15 09:57:11 +00:00
|
|
|
|
|
|
|
cgi.fix_pathinfo = 1
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2005-08-17 09:51:24 +00:00
|
|
|
and the option ``broken-scriptfilename`` in your fastcgi.server config: ::
|
2005-07-29 09:12:40 +00:00
|
|
|
|
|
|
|
fastcgi.server = ( ".php" =>
|
2005-12-13 11:36:15 +00:00
|
|
|
(( "socket" => "/tmp/php-fastcgi.socket",
|
|
|
|
"bin-path" => "/usr/local/bin/php",
|
2006-10-04 13:26:23 +00:00
|
|
|
"bin-environment" => (
|
2005-12-13 11:36:15 +00:00
|
|
|
"PHP_FCGI_CHILDREN" => "16",
|
|
|
|
"PHP_FCGI_MAX_REQUESTS" => "10000" ),
|
|
|
|
"bin-copy-environment" => (
|
|
|
|
"PATH", "SHELL", "USER" ),
|
|
|
|
"broken-scriptfilename" => "enable"
|
|
|
|
))
|
|
|
|
)
|
2005-07-29 09:12:40 +00:00
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
Why this ? the ``cgi.fix_pathinfo = 0`` would give you a working ``PATH_INFO``
|
2005-07-29 09:12:40 +00:00
|
|
|
but no ``PHP_SELF``. If you enable it, it turns around. To fix the
|
2005-08-15 09:57:11 +00:00
|
|
|
``PATH_INFO`` `--enable-discard-path` needs a SCRIPT_FILENAME which is against the CGI spec, a
|
2005-07-29 09:12:40 +00:00
|
|
|
broken-scriptfilename. With ``cgi.fix_pathinfo = 1`` in php.ini and
|
2006-10-04 13:26:23 +00:00
|
|
|
``broken-scriptfilename => "enable"`` you get both.
|
2005-07-29 09:12:40 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
External Spawning
|
|
|
|
-----------------
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
Spawning FastCGI processes directly in the webserver has some
|
2005-02-20 14:27:00 +00:00
|
|
|
disadvantages like
|
|
|
|
|
|
|
|
- FastCGI process can only run locally
|
|
|
|
- has the same permissions as the webserver
|
|
|
|
- has the same base-dir as the webserver
|
|
|
|
|
|
|
|
As soon as you are using a seperate FastCGI Server to
|
|
|
|
take off some load from the webserver you have to control
|
|
|
|
the FastCGI process by a external program like spawn-fcgi.
|
|
|
|
|
|
|
|
spawn-fcgi is used to start a FastCGI process in its own
|
2006-10-04 13:26:23 +00:00
|
|
|
environment and set the user-id, group-id and change to
|
2005-02-20 14:27:00 +00:00
|
|
|
another root-directory (chroot).
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
For convenience a wrapper script should be used which takes
|
|
|
|
care of all the necessary option. Such a script in included
|
2005-02-20 14:27:00 +00:00
|
|
|
in the lighttpd distribution and is call spawn-php.sh.
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
The script has a set of config variables you should take
|
2005-02-20 14:27:00 +00:00
|
|
|
a look at: ::
|
|
|
|
|
|
|
|
## ABSOLUTE path to the spawn-fcgi binary
|
|
|
|
SPAWNFCGI="/usr/local/sbin/spawn-fcgi"
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
## ABSOLUTE path to the PHP binary
|
|
|
|
FCGIPROGRAM="/usr/local/bin/php"
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
## bind to tcp-port on localhost
|
|
|
|
FCGIPORT="1026"
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
## bind to unix domain socket
|
|
|
|
# FCGISOCKET="/tmp/php.sock"
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
## number of PHP childs to spawn
|
|
|
|
PHP_FCGI_CHILDREN=10
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
## number of request server by a single php-process until
|
|
|
|
## is will be restarted
|
|
|
|
PHP_FCGI_MAX_REQUESTS=1000
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
## IP adresses where PHP should access server connections
|
|
|
|
## from
|
|
|
|
FCGI_WEB_SERVER_ADDRS="127.0.0.1,192.168.0.1"
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
# allowed environment variables sperated by spaces
|
|
|
|
ALLOWED_ENV="ORACLE_HOME PATH USER"
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
## if this script is run as root switch to the following user
|
|
|
|
USERID=wwwrun
|
|
|
|
GROUPID=wwwrun
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
If you have set the variables to values that fit to your
|
2005-02-20 14:27:00 +00:00
|
|
|
setup you can start it by calling: ::
|
|
|
|
|
|
|
|
$ spawn-php.sh
|
|
|
|
spawn-fcgi.c.136: child spawned successfully: PID: 6925
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
If you get "child spawned successfully: PID:" the php
|
|
|
|
processes could be started successfully. You should see them
|
2005-02-20 14:27:00 +00:00
|
|
|
in your processlist: ::
|
|
|
|
|
|
|
|
$ ps ax | grep php
|
|
|
|
6925 ? S 0:00 /usr/local/bin/php
|
|
|
|
6928 ? S 0:00 /usr/local/bin/php
|
|
|
|
...
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
The number of processes should be PHP_FCGI_CHILDREN + 1.
|
|
|
|
Here the process 6925 is the master of the slaves which
|
|
|
|
handle the work in parallel. Number of parallel workers can
|
|
|
|
be set by PHP_FCGI_CHILDREN. A worker dies automaticly of
|
|
|
|
handling PHP_FCGI_MAX_REQUESTS requests as PHP might have
|
2005-02-20 14:27:00 +00:00
|
|
|
memory leaks.
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
If you start the script as user root php processes will be
|
|
|
|
running as the user USERID and group GROUPID to drop the
|
|
|
|
root permissions. Otherwise the php processes will run as
|
2005-02-20 14:27:00 +00:00
|
|
|
the user you started script as.
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
As the script might be started from a unknown stage or even
|
|
|
|
directly from the command-line it cleans the environment
|
|
|
|
before starting the processes. ALLOWED_ENV contains all
|
|
|
|
the external environement variables that should be available
|
2005-02-20 14:27:00 +00:00
|
|
|
to the php-process.
|
|
|
|
|
|
|
|
|
|
|
|
Perl
|
|
|
|
----
|
|
|
|
|
|
|
|
For Perl you have to install the FCGI module from CPAN.
|
|
|
|
|
|
|
|
Skeleton for remote authorizer
|
|
|
|
==============================
|
|
|
|
|
|
|
|
The basic functionality of authorizer is as follows (see
|
2006-10-04 13:26:23 +00:00
|
|
|
http://www.fastcgi.com/devkit/doc/fcgi-spec.html, 6.3 for
|
2005-02-20 14:27:00 +00:00
|
|
|
details). ::
|
|
|
|
|
|
|
|
#include <fcgi_stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
int main () {
|
|
|
|
char* p;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
while (FCGI_Accept() >= 0) {
|
2005-02-20 14:27:00 +00:00
|
|
|
/* wait for fastcgi authorizer request */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
printf("Content-type: text/html\r\n");
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if ((p = getenv("QUERY_STRING")) == NULL) ||
|
|
|
|
<QUERY_STRING is unauthorized>)
|
|
|
|
printf("Status: 403 Forbidden\r\n\r\n");
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
else printf("\r\n");
|
2005-02-20 14:27:00 +00:00
|
|
|
/* default Status is 200 - allow access */
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
It is possible to use any other variables provided by
|
|
|
|
FastCGI interface for authorization check. Here is only an
|
2005-02-20 14:27:00 +00:00
|
|
|
example.
|
|
|
|
|
|
|
|
|
|
|
|
Troubleshooting
|
|
|
|
===============
|
|
|
|
|
|
|
|
fastcgi.debug should be enabled for troubleshooting.
|
|
|
|
|
|
|
|
If you get: ::
|
|
|
|
|
|
|
|
(fcgi.c.274) connect delayed: 8
|
|
|
|
(fcgi.c.289) connect succeeded: 8
|
2006-10-04 13:26:23 +00:00
|
|
|
(fcgi.c.745) unexpected end-of-file (perhaps the fastcgi
|
2005-02-20 14:27:00 +00:00
|
|
|
process died): 8
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
the fastcgi process accepted the connection but closed it
|
|
|
|
right away. This happens if FCGI_WEB_SERVER_ADDRS doesn't
|
2005-02-20 14:27:00 +00:00
|
|
|
include the host where you are connection from.
|
|
|
|
|
|
|
|
If you get ::
|
|
|
|
|
|
|
|
(fcgi.c.274) connect delayed: 7
|
2006-10-04 13:26:23 +00:00
|
|
|
(fcgi.c.1107) error: unexpected close of fastcgi connection
|
2005-02-20 14:27:00 +00:00
|
|
|
for /peterp/seite1.php (no fastcgi process on host/port ?)
|
2006-10-04 13:26:23 +00:00
|
|
|
(fcgi.c.1015) emergency exit: fastcgi: connection-fd: 5
|
2005-02-20 14:27:00 +00:00
|
|
|
fcgi-fd: 7
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
the fastcgi process is not running on the host/port you are
|
2005-02-20 14:27:00 +00:00
|
|
|
connection to. Check your configuration.
|
|
|
|
|
|
|
|
If you get ::
|
|
|
|
|
|
|
|
(fcgi.c.274) connect delayed: 7
|
|
|
|
(fcgi.c.289) connect succeeded: 7
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
everything is fine. The connect() call just was delayed a
|
2005-02-20 14:27:00 +00:00
|
|
|
little bit and is completly normal.
|
|
|
|
|