Credits:
Ben Mansell, Stephen Landamore, Daniel Silverstone, Shane Caraveo
Building PHP
------------
You must add '--enable-fastcgi' to the configure command on Linux or
OSX based systems to get fastcgi support in the php-cgi binary. You
also must not use '--enable-discard-path'.
Running the FastCGI PHP module
------------------------------
There are two ways to run the resulting 'php' binary after the fastcgi
version has been built:
1) Configure your web server to run the PHP binary itself.
This is the simplest method, obviously you will have to configure your
web server appropriately. Some web servers may also not support this method,
or may not be as efficient.
2) Run PHP separately from the web server.
In this setup, PHP is started as a separate process entirely from the web
server. It will listen on a socket for new FastCGI requests, and deliver
PHP pages as appropriate. This is the recommended way of running PHP-FastCGI.
To run this way, you must start the PHP binary running by giving it an IP
and a port number to listen to on the command line, e.g.:
./php -b 127.0.0.1:8002
The above line is the recommended way of running FastCGI. You usually
want the FastCGI server to provide services to the localhost, not
everyone on the Internet.
If your web server sits on a remote host, you can make FastCGI listen
on all interfaces:
./php -b :8002
./php -b "*:8002"
Note that hostnames are not supported.
You must also configure your web server to connect to the appropriate port
in order to talk to the PHP FastCGI process.
The advantage of running PHP in this way is that it entirely separates the
web server and PHP process, so that one cannot disrupt the other. It also
allows PHP to be on an entirely separate machine from the web server if need
be, you could even have several web servers utilising the same running PHP
process if required!
Using FastCGI PHP with Apache
=============================
First of all, you may well ask 'Why?'. After all, Apache already has mod_php.
However, there are advantages to running PHP with FastCGI. Separating the
PHP code from the web server removes 'bloat' from the main server, and should
improve the performance of non-PHP requests. Secondly, having one permanent
PHP process as opposed to one per apache process means that shared resources
like persistent database connections are used more efficiently.
First of all, make sure that the FastCGI module is enabled. You should have
a line in your config like:
LoadModule fastcgi_module /usr/lib/apache/2.0/mod_fastcgi.so
Don't load mod_php, by the way. Make sure it is commented out!
#LoadModule php5_module /usr/lib/apache/2.0/libphp5.so
Now, we'll create a fcgi-bin directory, just like you would do with normal
CGI scripts. You'll need to create a directory somewhere to store your
FastCGI binaries. We'll use /space/fcgi-bin/ for this example. Remember to
copy the FastCGI-PHP binary in there. (named 'php-cgi') This sets up
php to run under mod_fastcgi as a dynamic server.
ScriptAlias /fcgi-bin/ /space/fcgi-bin/