You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
5.6 KiB

  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 php4_module /usr/lib/apache/2.0/libphp4.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: 8)
  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. So by default, you will be able to handle 8
  94. concurrent PHP page requests. Further requests will be queued.
  95. Increasing this number will allow for better concurrency, especially if you
  96. have pages that take a significant time to create, or supply a lot of data
  97. (e.g. downloading huge files via PHP). On the other hand, having more
  98. processes running will use more RAM, and letting too many PHP pages be
  99. generated concurrently will mean that each request will be slow.
  100. PHP_FCGI_MAX_REQUESTS (default value: 500)
  101. This controls how many requests each child process will handle before
  102. exitting. When one process exits, another will be created. This tuning is
  103. necessary because several PHP functions are known to have memory leaks. If the
  104. PHP processes were left around forever, they would be become very inefficient.