fastcgi.txt 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. =====================
  2. the FastCGI Interface
  3. =====================
  4. -------------------
  5. Module: mod_fastcgi
  6. -------------------
  7. :Author: Jan Kneschke
  8. :Date: $Date: 2004/11/03 22:26:05 $
  9. :Revision: $Revision: 1.3 $
  10. :abstract:
  11. The FastCGI interface is the fastest and most secure way
  12. to interface external process-handlers like Perl, PHP and
  13. your self-written applications.
  14. .. meta::
  15. :keywords: lighttpd, FastCGI
  16. .. contents:: Table of Contents
  17. Description
  18. ===========
  19. lighttpd provides an interface to a external programs that
  20. support the FastCGI interface. The FastCGI Interface is
  21. defined by http://www.fastcgi.com/ and is a
  22. platform-independent and server independent interface between
  23. a web-application and a webserver.
  24. This means that FastCGI programs that run with the Apache
  25. Webserver will run seamlessly with lighttpd and vice versa.
  26. FastCGI
  27. -------
  28. FastCGI is removes a lot of the limitations of CGI programs.
  29. CGI programs have the problem that they have to be restarted
  30. by the webserver for every request which leads to really bad
  31. performance values.
  32. FastCGI removes this limitation by keeping the process running
  33. and handling the requests by this always running process. This
  34. removes the time used for the fork() and the overall startup
  35. and cleanup time which is necessary to create and destroy a
  36. process.
  37. While CGI programs communicate to the server over pipes,
  38. FastCGI processes use Unix-Domain-Sockets or TCP/IP to talk
  39. with the webserver. This gives you the second advantage over
  40. simple CGI programs: FastCGI don't have to run on the Webserver
  41. itself but everywhere in the network.
  42. lighttpd takes it a little bit further by providing a internal
  43. FastCGI load-balancer which can be used to balance the load
  44. over multiple FastCGI Servers. In contrast to other solutions
  45. only the FastCGI process has to be on the cluster and not the
  46. whole webserver. That gives the FastCGI process more resources
  47. than a e.g. load-balancer+apache+mod_php solution.
  48. If you compare FastCGI against a apache+mod_php solution you
  49. should note that FastCGI provides additional security as the
  50. FastCGI process can be run under different permissions that
  51. the webserver and can also live a chroot which might be
  52. different than the one the webserver is running in.
  53. Options
  54. =======
  55. lighttpd provides the FastCGI support via the fastcgi-module
  56. (mod_fastcgi) which provides 2 options in the config-file:
  57. fastcgi.debug
  58. a value between 0 and 65535 to set the debug-level in the
  59. FastCGI module. Currently only 0 and 1 are used. Use 1 to
  60. enable some debug output, 0 to disable it.
  61. fastcgi.map-extensions
  62. map multiple extensions to the same fastcgi server
  63. Example: ::
  64. fastcgi.map-extensions = ( ".php3" => ".php" )
  65. fastcgi.server
  66. tell the module where to send FastCGI requests to. Every
  67. file-extension can have it own handler. Load-Balancing is
  68. done by specifying multiple handles for the same extension.
  69. structure of fastcgi.server section: ::
  70. ( <extension> =>
  71. (
  72. ( "host" => <string> ,
  73. "port" => <integer> ,
  74. "socket" => <string>, # either socket
  75. # or host+port
  76. "bin-path" => <string>, # OPTIONAL
  77. "bin-environment" => <array>, # OPTIONAL
  78. "bin-copy-environment" => <array>, # OPTIONAL
  79. "mode" => <string>, # OPTIONAL
  80. "docroot" => <string> , # OPTIONAL if "mode"
  81. # is not "authorizer"
  82. "check-local" => <string>, # OPTIONAL
  83. "max-procs" => <integer>, # OPTIONAL
  84. "broken-scriptfilename" => <boolean>, # OPTIONAL
  85. "disable-time" => <integer>, # optional
  86. "x-sendfile" => <boolean>, # optional (replaces "allow-x-send-file")
  87. "x-sendfile-docroot" => <boolean>, # optional
  88. "kill-signal" => <integer>, # OPTIONAL
  89. "fix-root-scriptname" => <boolean>,
  90. # OPTIONAL
  91. ( "host" => ...
  92. )
  93. )
  94. )
  95. :<extension>: is the file-extension or prefix
  96. (if started with "/")
  97. :"host": is hostname/ip of the FastCGI process
  98. :"port": is tcp-port on the "host" used by the FastCGI
  99. process
  100. :"bin-path": path to the local FastCGI binary which should be
  101. started if no local FastCGI is running
  102. :"socket": path to the unix-domain socket
  103. :"mode": is the FastCGI protocol mode.
  104. Default is "responder", also "authorizer"
  105. mode is implemented.
  106. :"docroot": is optional and is the docroot on the remote
  107. host for default "responder" mode. For
  108. "authorizer" mode it is MANDATORY and it points
  109. to docroot for authorized requests. For security
  110. reasons it is recommended to keep this docroot
  111. outside of server.document-root tree.
  112. :"check-local": is optional and may be "enable" (default) or
  113. "disable". If enabled the server first check
  114. for a file in local server.document-root tree
  115. and return 404 (Not Found) if no such file.
  116. If disabled, the server forward request to
  117. FastCGI interface without this check.
  118. :"broken-scriptfilename": breaks SCRIPT_FILENAME in a wat that
  119. PHP can extract PATH_INFO from it (default: disabled)
  120. :"disable-time": time to wait before a disabled backend is checked
  121. again
  122. :"x-sendfile": controls if X-Sendfile backend response header is allowed
  123. (deprecated headers: X-Sendfile2 and X-LIGHTTPD-send-file)
  124. ("x-sendfile" replaces "allow-x-sendfile")
  125. :"x-sendfile-docroot": list of directory trees permitted with X-Sendfile
  126. :"fix-root-scriptname": fix broken path-info split for "/" extension ("prefix")
  127. If bin-path is set:
  128. :"max-procs": the upper limit of the processes to start
  129. :"bin-environment": put an entry into the environment of
  130. the started process
  131. :"bin-copy-environement": clean up the environment and copy
  132. only the specified entries into the fresh
  133. environment of the spawn process
  134. :"kill-signal": signal to terminate the FastCGI process with,
  135. defaults to SIGTERM
  136. Examples
  137. --------
  138. Multiple extensions for the same host ::
  139. fastcgi.server = ( ".php" =>
  140. (( "host" => "127.0.0.1",
  141. "port" => 1026,
  142. "bin-path" => "/usr/local/bin/php"
  143. )),
  144. ".php4" =>
  145. (( "host" => "127.0.0.1",
  146. "port" => 1026
  147. ))
  148. )
  149. Example with prefix: ::
  150. fastcgi.server = ( "/remote_scripts/" =>
  151. (( "host" => "192.168.0.3",
  152. "port" => 9000,
  153. "check-local" => "disable",
  154. "docroot" => "/" # remote server may use
  155. # it's own docroot
  156. ))
  157. )
  158. The request `http://my.host.com/remote_scripts/test.cgi` will
  159. be forwarded to fastcgi server at 192.168.0.3 and the value
  160. "/remote_scripts/test.cgi" will be used for the SCRIPT_NAME
  161. variable. Remote server may prepend it with its own
  162. document root. The handling of index files is also the
  163. responsibility of remote server for this case.
  164. In the case that the prefix is not terminated with a slash
  165. the prefix will be handled as file and /test.cgi would become
  166. a PATH_INFO instead of part of SCRIPT_NAME.
  167. Example for "authorizer" mode: ::
  168. fastcgi.server = ( "/remote_scripts/" =>
  169. (( "host" => "10.0.0.2",
  170. "port" => 9000,
  171. "docroot" => "/path_to_private_docs",
  172. "mode" => "authorizer"
  173. ))
  174. )
  175. Note that if "docroot" is specified then its value will be
  176. used in DOCUMENT_ROOT and SCRIPT_FILENAME variables passed
  177. to FastCGI server.
  178. Load-Balancing
  179. ==============
  180. The FastCGI plugin provides automatically a load-balancing between
  181. multiple FastCGI servers. ::
  182. fastcgi.server = ( ".php" =>
  183. (( "host" => "10.0.0.2", "port" => 1030 ),
  184. ( "host" => "10.0.0.3", "port" => 1030 ))
  185. )
  186. To understand how the load-balancing works you can enable the
  187. fastcgi.debug option and will get a similar output as here: ::
  188. proc: 127.0.0.1 1031 1 1 1 31454
  189. proc: 127.0.0.1 1028 1 1 1 31442
  190. proc: 127.0.0.1 1030 1 1 1 31449
  191. proc: 127.0.0.1 1029 1 1 2 31447
  192. proc: 127.0.0.1 1026 1 1 2 31438
  193. got proc: 34 31454
  194. release proc: 40 31438
  195. proc: 127.0.0.1 1026 1 1 1 31438
  196. proc: 127.0.0.1 1028 1 1 1 31442
  197. proc: 127.0.0.1 1030 1 1 1 31449
  198. proc: 127.0.0.1 1031 1 1 2 31454
  199. proc: 127.0.0.1 1029 1 1 2 31447
  200. Even if this for multiple FastCGI children on the local machine
  201. the following explanation is valid for remote connections too.
  202. The output shows:
  203. - IP, port, unix-socket (is empty here)
  204. - is-local, state (0 - unset, 1 - running, ... )
  205. - active connections (load)
  206. - PID
  207. As you can see the list is always sorted by the load field.
  208. Whenever a new connection is requested, the first entry (the one
  209. with the lowest load) is selected, the load is increased (got proc: ...)
  210. and the list is sorted again.
  211. If a FastCGI request is done or the connection is dropped, the load on the
  212. FastCGI proc decreases and the list is sorted again (release proc: ...)
  213. This behaviour is very light-weight in code and still very efficient
  214. as it keeps the fastcgi-servers equally loaded even if they have different
  215. CPUs.
  216. Adaptive Process Spawning
  217. =========================
  218. .. note:: This feature is disabled in 1.3.14 again. min-procs is
  219. ignored in that release
  220. Starting with 1.3.8 lighttpd can spawn processes on demand if
  221. a bin-path is specified and the FastCGI process runs locally.
  222. If you want to have a least one FastCGI process running and
  223. more of the number of requests increases you can use min-procs
  224. and max-procs.
  225. A new process is spawned as soon as the average number of
  226. requests waiting to be handle by a single process increases the
  227. max-load-per-proc setting.
  228. The idle-timeout specifies how long a fastcgi-process should wait
  229. for a new request before it kills itself.
  230. Example
  231. -------
  232. ::
  233. fastcgi.server = ( ".php" =>
  234. (( "socket" => "/tmp/php.socket",
  235. "bin-path" => "/usr/local/bin/php",
  236. "min-procs" => 1,
  237. "max-procs" => 32,
  238. "max-load-per-proc" => 4,
  239. "idle-timeout" => 20
  240. ))
  241. )
  242. Disabling Adaptive Spawning
  243. ---------------------------
  244. Adaptive Spawning is a quite new feature and it might misbehave
  245. for your setup. There are several ways to control how the spawning
  246. is done:
  247. 1. ``"max-load-per-proc" => 1``
  248. if that works for you, great.
  249. 2. If not set ``min-procs == max-procs``.
  250. 3. For PHP you can also use: ::
  251. $ PHP_FCGI_CHILDREN=384 ./lighttpd -f ./lighttpd.conf
  252. fastcgi.server = ( ".php" =>
  253. (( "socket" => "/tmp/php.socket",
  254. "bin-path" => "/usr/local/bin/php",
  255. "min-procs" => 1,
  256. "max-procs" => 1,
  257. "max-load-per-proc" => 4,
  258. "idle-timeout" => 20
  259. ))
  260. )
  261. It will create one socket and let's PHP create the 384 processes itself.
  262. 4. If you don't want lighttpd to manage the fastcgi processes, remove the
  263. bin-path and use spawn-fcgi to spawn them itself.
  264. FastCGI and Programming Languages
  265. =================================
  266. Preparing PHP as a FastCGI program
  267. ----------------------------------
  268. One of the most important application that has a FastCGI
  269. interface is php which can be downloaded from
  270. http://www.php.net/ . You have to recompile the php from
  271. source to enable the FastCGI interface as it is normally
  272. not enabled by default in the distributions.
  273. If you already have a working installation of PHP on a
  274. webserver execute a small script which just contains ::
  275. <?php phpinfo(); ?>
  276. and search for the line in that contains the configure call.
  277. You can use it as the base for the compilation.
  278. You have to remove all occurrences of `--with-apxs`, `--with-apxs2`
  279. and the like which would build PHP with Apache support. Add the
  280. next three switches to compile PHP with FastCGI support::
  281. $ ./configure \
  282. --enable-fastcgi \
  283. --enable-force-cgi-redirect \
  284. ...
  285. After compilation and installation check that your PHP
  286. binary contains FastCGI support by calling: ::
  287. $ php -v
  288. PHP 4.3.3RC2-dev (cgi-fcgi) (built: Oct 19 2003 23:19:17)
  289. The important part is the (cgi-fcgi).
  290. Starting a FastCGI-PHP
  291. ----------------------
  292. Starting with version 1.3.6 lighttpd can spawn the FastCGI
  293. processes locally itself if necessary: ::
  294. fastcgi.server = ( ".php" =>
  295. (( "socket" => "/tmp/php-fastcgi.socket",
  296. "bin-path" => "/usr/local/bin/php"
  297. ))
  298. )
  299. PHP provides 2 special environment variables which control the number of
  300. spawned works under the control of a single watching process
  301. (PHP_FCGI_CHILDREN) and the number of requests what a single worker
  302. handles before it kills itself. ::
  303. fastcgi.server = ( ".php" =>
  304. (( "socket" => "/tmp/php-fastcgi.socket",
  305. "bin-path" => "/usr/local/bin/php",
  306. "bin-environment" => (
  307. "PHP_FCGI_CHILDREN" => "16",
  308. "PHP_FCGI_MAX_REQUESTS" => "10000"
  309. )
  310. ))
  311. )
  312. To increase the security of the started process you should only pass
  313. the necessary environment variables to the FastCGI process. ::
  314. fastcgi.server = ( ".php" =>
  315. (( "socket" => "/tmp/php-fastcgi.socket",
  316. "bin-path" => "/usr/local/bin/php",
  317. "bin-environment" => (
  318. "PHP_FCGI_CHILDREN" => "16",
  319. "PHP_FCGI_MAX_REQUESTS" => "10000" ),
  320. "bin-copy-environment" => (
  321. "PATH", "SHELL", "USER" )
  322. ))
  323. )
  324. Configuring PHP
  325. ---------------
  326. If you want to use PATH_INFO and PHP_SELF in you PHP scripts you have to
  327. configure php and lighttpd. The php.ini needs the option: ::
  328. cgi.fix_pathinfo = 1
  329. and the option ``broken-scriptfilename`` in your fastcgi.server config: ::
  330. fastcgi.server = ( ".php" =>
  331. (( "socket" => "/tmp/php-fastcgi.socket",
  332. "bin-path" => "/usr/local/bin/php",
  333. "bin-environment" => (
  334. "PHP_FCGI_CHILDREN" => "16",
  335. "PHP_FCGI_MAX_REQUESTS" => "10000" ),
  336. "bin-copy-environment" => (
  337. "PATH", "SHELL", "USER" ),
  338. "broken-scriptfilename" => "enable"
  339. ))
  340. )
  341. Why this ? the ``cgi.fix_pathinfo = 0`` would give you a working ``PATH_INFO``
  342. but no ``PHP_SELF``. If you enable it, it turns around. To fix the
  343. ``PATH_INFO`` `--enable-discard-path` needs a SCRIPT_FILENAME which is against the CGI spec, a
  344. broken-scriptfilename. With ``cgi.fix_pathinfo = 1`` in php.ini and
  345. ``broken-scriptfilename => "enable"`` you get both.
  346. External Spawning
  347. -----------------
  348. Spawning FastCGI processes directly in the webserver has some
  349. disadvantages like
  350. - FastCGI process can only run locally
  351. - has the same permissions as the webserver
  352. - has the same base-dir as the webserver
  353. As soon as you are using a separate FastCGI Server to
  354. take off some load from the webserver you have to control
  355. the FastCGI process by a external program like spawn-fcgi.
  356. spawn-fcgi is used to start a FastCGI process in its own
  357. environment and set the user-id, group-id and change to
  358. another root-directory (chroot).
  359. For convenience a wrapper script should be used which takes
  360. care of all the necessary option. Such a script in included
  361. in the lighttpd distribution and is call spawn-php.sh.
  362. The script has a set of config variables you should take
  363. a look at: ::
  364. ## ABSOLUTE path to the spawn-fcgi binary
  365. SPAWNFCGI="/usr/local/sbin/spawn-fcgi"
  366. ## ABSOLUTE path to the PHP binary
  367. FCGIPROGRAM="/usr/local/bin/php"
  368. ## bind to tcp-port on localhost
  369. FCGIPORT="1026"
  370. ## bind to unix domain socket
  371. # FCGISOCKET="/tmp/php.sock"
  372. ## number of PHP children to spawn
  373. PHP_FCGI_CHILDREN=10
  374. ## number of request server by a single php-process until
  375. ## is will be restarted
  376. PHP_FCGI_MAX_REQUESTS=1000
  377. ## IP addresses where PHP should access server connections
  378. ## from
  379. FCGI_WEB_SERVER_ADDRS="127.0.0.1,192.168.0.1"
  380. # allowed environment variables sperated by spaces
  381. ALLOWED_ENV="ORACLE_HOME PATH USER"
  382. ## if this script is run as root switch to the following user
  383. USERID=wwwrun
  384. GROUPID=wwwrun
  385. If you have set the variables to values that fit to your
  386. setup you can start it by calling: ::
  387. $ spawn-php.sh
  388. spawn-fcgi.c.136: child spawned successfully: PID: 6925
  389. If you get "child spawned successfully: PID:" the php
  390. processes could be started successfully. You should see them
  391. in your processlist: ::
  392. $ ps ax | grep php
  393. 6925 ? S 0:00 /usr/local/bin/php
  394. 6928 ? S 0:00 /usr/local/bin/php
  395. ...
  396. The number of processes should be PHP_FCGI_CHILDREN + 1.
  397. Here the process 6925 is the master of the slaves which
  398. handle the work in parallel. Number of parallel workers can
  399. be set by PHP_FCGI_CHILDREN. A worker dies automatically of
  400. handling PHP_FCGI_MAX_REQUESTS requests as PHP might have
  401. memory leaks.
  402. If you start the script as user root php processes will be
  403. running as the user USERID and group GROUPID to drop the
  404. root permissions. Otherwise the php processes will run as
  405. the user you started script as.
  406. As the script might be started from a unknown stage or even
  407. directly from the command-line it cleans the environment
  408. before starting the processes. ALLOWED_ENV contains all
  409. the external environment variables that should be available
  410. to the php-process.
  411. Perl
  412. ----
  413. For Perl you have to install the FCGI module from CPAN.
  414. Skeleton for remote authorizer
  415. ==============================
  416. The basic functionality of authorizer is as follows (see
  417. http://www.fastcgi.com/devkit/doc/fcgi-spec.html, 6.3 for
  418. details). ::
  419. #include <fcgi_stdio.h>
  420. #include <stdlib.h>
  421. #include <unistd.h>
  422. int main () {
  423. char* p;
  424. while (FCGI_Accept() >= 0) {
  425. /* wait for fastcgi authorizer request */
  426. printf("Content-type: text/html\r\n");
  427. if ((p = getenv("QUERY_STRING")) == NULL) ||
  428. <QUERY_STRING is unauthorized>)
  429. printf("Status: 403 Forbidden\r\n\r\n");
  430. else printf("\r\n");
  431. /* default Status is 200 - allow access */
  432. }
  433. return 0;
  434. }
  435. It is possible to use any other variables provided by
  436. FastCGI interface for authorization check. Here is only an
  437. example.
  438. Troubleshooting
  439. ===============
  440. fastcgi.debug should be enabled for troubleshooting.
  441. If you get: ::
  442. (fcgi.c.274) connect delayed: 8
  443. (fcgi.c.289) connect succeeded: 8
  444. (fcgi.c.745) unexpected end-of-file (perhaps the fastcgi
  445. process died): 8
  446. the fastcgi process accepted the connection but closed it
  447. right away. This happens if FCGI_WEB_SERVER_ADDRS doesn't
  448. include the host where you are connection from.
  449. If you get ::
  450. (fcgi.c.274) connect delayed: 7
  451. (fcgi.c.1107) error: unexpected close of fastcgi connection
  452. for /peterp/seite1.php (no fastcgi process on host/port ?)
  453. (fcgi.c.1015) emergency exit: fastcgi: connection-fd: 5
  454. fcgi-fd: 7
  455. the fastcgi process is not running on the host/port you are
  456. connection to. Check your configuration.
  457. If you get ::
  458. (fcgi.c.274) connect delayed: 7
  459. (fcgi.c.289) connect succeeded: 7
  460. everything is fine. The connect() call just was delayed a
  461. little bit and is completely normal.