README.FastCGI 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. Credits:
  2. Ben Mansell, Stephen Landamore, Daniel Silverstone, Shane Caraveo
  3. Building PHP
  4. ------------
  5. You must add '--enable-fastcgi' to the configure command on Linux or
  6. OSX based systems to get fastcgi support in the php-cgi binary. You
  7. also must not use '--enable-discard-path'.
  8. Running the FastCGI PHP module
  9. ------------------------------
  10. There are two ways to run the resulting 'php' binary after the fastcgi
  11. version has been built:
  12. 1) Configure your web server to run the PHP binary itself.
  13. This is the simplest method, obviously you will have to configure your
  14. web server appropriately. Some web servers may also not support this method,
  15. or may not be as efficient.
  16. 2) Run PHP separately from the web server.
  17. In this setup, PHP is started as a separate process entirely from the web
  18. server. It will listen on a socket for new FastCGI requests, and deliver
  19. PHP pages as appropriate. This is the recommended way of running PHP-FastCGI.
  20. To run this way, you must start the PHP binary running by giving it an IP
  21. and a port number to listen to on the command line, e.g.:
  22. ./php -b 127.0.0.1:8002
  23. The above line is the recommended way of running FastCGI. You usually
  24. want the FastCGI server to provide services to the localhost, not
  25. everyone on the Internet.
  26. If your web server sits on a remote host, you can make FastCGI listen
  27. on all interfaces:
  28. ./php -b :8002
  29. ./php -b "*:8002"
  30. Note that hostnames are not supported.
  31. You must also configure your web server to connect to the appropriate port
  32. in order to talk to the PHP FastCGI process.
  33. The advantage of running PHP in this way is that it entirely separates the
  34. web server and PHP process, so that one cannot disrupt the other. It also
  35. allows PHP to be on an entirely separate machine from the web server if need
  36. be, you could even have several web servers utilising the same running PHP
  37. process if required!
  38. Using FastCGI PHP with Apache
  39. =============================
  40. First of all, you may well ask 'Why?'. After all, Apache already has mod_php.
  41. However, there are advantages to running PHP with FastCGI. Separating the
  42. PHP code from the web server removes 'bloat' from the main server, and should
  43. improve the performance of non-PHP requests. Secondly, having one permanent
  44. PHP process as opposed to one per apache process means that shared resources
  45. like persistent database connections are used more efficiently.
  46. First of all, make sure that the FastCGI module is enabled. You should have
  47. a line in your config like:
  48. LoadModule fastcgi_module /usr/lib/apache/2.0/mod_fastcgi.so
  49. Don't load mod_php, by the way. Make sure it is commented out!
  50. #LoadModule php5_module /usr/lib/apache/2.0/libphp5.so
  51. Now, we'll create a fcgi-bin directory, just like you would do with normal
  52. CGI scripts. You'll need to create a directory somewhere to store your
  53. FastCGI binaries. We'll use /space/fcgi-bin/ for this example. Remember to
  54. copy the FastCGI-PHP binary in there. (named 'php-cgi') This sets up
  55. php to run under mod_fastcgi as a dynamic server.
  56. ScriptAlias /fcgi-bin/ /space/fcgi-bin/
  57. <Location /fcgi-bin/>
  58. Options ExecCGI
  59. SetHandler fastcgi-script
  60. </Location>
  61. To setup a specific static configuration for php, you have to use
  62. the FastCgiServer configuration for mod_fastcgi. For this, do not
  63. use the above configuration, but rather the following.
  64. (see mod_fastcgi docs for more configuration information):
  65. Alias /fcgi-bin/ /space/fcgi-bin/
  66. FastCgiServer /path/to/php-cgi -processes 5
  67. For either of the above configurations, we need to tell Apache to
  68. use the FastCGI binary /fcgi-bin/php to deliver PHP pages.
  69. All that is needed is:
  70. AddType application/x-httpd-fastphp .php
  71. Action application/x-httpd-fastphp /fcgi-bin/php-cgi
  72. Now, if you restart Apache, php pages should now be delivered!
  73. Using FastCGI PHP with IIS or iPlanet
  74. =====================================
  75. FastCGI server plugins are available at www.caraveo.com/fastcgi/
  76. Documentation on these are sparse. iPlanet is not very tested,
  77. and no makefile exists yet for unix based iPlanet servers.
  78. Security
  79. --------
  80. Be sure to run the php binary as an appropriate userid. Also, firewall out
  81. the port that PHP is listening on. In addition, you can set the environment
  82. variable FCGI_WEB_SERVER_ADDRS to control who can connect to the FastCGI.
  83. Set it to a comma separated list of IP addresses, e.g.:
  84. export FCGI_WEB_SERVER_ADDRS=199.170.183.28,199.170.183.71
  85. Tuning
  86. ------
  87. There are a few tuning parameters that can be tweaked to control the
  88. performance of FastCGI PHP. The following are environment variables that can
  89. be set before running the PHP binary:
  90. PHP_FCGI_CHILDREN (default value: 0)
  91. This controls how many child processes the PHP process spawns. When the
  92. fastcgi starts, it creates a number of child processes which handle one
  93. page request at a time. Value 0 means that PHP willnot start additional
  94. processes and main process will handle FastCGI requests by itself. Note that
  95. this process may die (because of PHP_FCGI_MAX_REQUESTS) and it willnot
  96. respawned automatic. Values 1 and above force PHP start additioanl processes
  97. those will handle requests. The main process will restart children in case of
  98. their death. So by default, you will be able to handle 1 concurrent PHP page
  99. requests. Further requests will be queued. Increasing this number will allow
  100. for better concurrency, especially if you have pages that take a significant
  101. time to create, or supply a lot of data (e.g. downloading huge files via PHP).
  102. On the other hand, having more processes running will use more RAM, and letting
  103. too many PHP pages be generated concurrently will mean that each request will
  104. be slow.
  105. PHP_FCGI_MAX_REQUESTS (default value: 500)
  106. This controls how many requests each child process will handle before
  107. exitting. When one process exits, another will be created. This tuning is
  108. necessary because several PHP functions are known to have memory leaks. If the
  109. PHP processes were left around forever, they would be become very inefficient.