1. There are two ways to configure Apache to use PHP:

  1. Configure Apache to load the PHP interpreter as an Apache module

  2. Configure Apache to run the PHP interpreter as a CGI binary

(PS: Windows IIS normaly configures as CGI by the way)


2. What is the difference between CGI and apache Module Mode?

An Apache module is compiled into the Apache binary, so the  PHP interpreter runs in the Apache process, meaning that when Apache  spawns a child, each process already contains a binary p_w_picpath of PHP. A  CGI is executed as a single process for each request, and must make an  exec() or fork() call to the PHP executable, meaning that each request  will create a new process of the PHP interpreter. Apache is much more  efficient in it's ability to handle requests, and managing resources,  making the Apache module slightly faster than the CGI (as well as more  stable under load).

CGI Mode on the other hand, is more secure because the server  now manages and controls access to the binaries. PHP can now run as your  own user rather than the generic Apache user. This means you can put  your database passwords in a file readable only by you and your php  scripts can still access it! The "Group" and "Other" permissions (refer Where can you learn more about file permissions?)  can now be more restrictive. CGI mode is also claimed to be more  flexible in many respects as you should now not see, with phpSuExec  (refer Permissions under phpSuExec)  issues with file ownership being taken over by the Apache user,  therefore you should no longer have problems under FTP when trying to  access or modify files that have been uploaded through a PHP interface,  such as Joomla! upload options.

If your server is configured to run PHP as an Apache module, then you  will have the choice of using either php.ini or Apache .htaccess files,  however, if your server runs PHP in CGI mode then you will only have  the choice of using php.ini files locally to change settings, as Apache  is no longer in complete control of PHP.



3. Running PHP as an Apache module

To configure Apache to load PHP as a module to parse your PHP  scripts, the httpd.conf needs to be modified, typically found in  "c:\Program Files\Apache Group\Apache\conf\" or "/etc/httpd/conf/".

Search for the section of the file that has a series of commented out  "LoadModule" statements. (Statements prefixed by the hash "#" sign are  regarded as having been commented out.) If PHP is running in "Apache  Module" Mode you should see something very similar to the following;

LoadModule php4_module "c:/php/php4apache.dll"

Apache 2.x

For PHP5
LoadModule php5_module C:/php/php5apache2.dll 

or (platform dependent)

LoadModule php5_module /usr/lib/apache/libphp5.so 

and

AddModule mod_php5.c

4. Running PHP as a CGI binary

To configure PHP to run as a CGI, again you will need to configure  the httpd.conf, but confirm that the above settings are not also  configured, unless you know what you are doing you can generate yourself  "HTTP 500" errors. Search your Apache configuration file for the  "ScriptAlias" section.

Add the following line below after the ScriptAlias for "cgi-bin". Note:  The location will depend on where PHP is installed on your system, you  should substitute the appropriate path in place of "c:/php/" (for  example, "c:/Program Files/php/").
ScriptAlias /php/ "c:/php/"
Apache again needs to be configured for the PHP MIME type. Search for  the "AddType" section, and add the following line after it:
AddType application/x-httpd-php .php
As in the case of running PHP as an Apache module, you can add  whatever extensions you want Apache to recognise as PHP scripts, such  as:
AddType application/x-httpd-php .php3
AddType application/x-httpd-php .phtml
Next, you will need to tell the server to execute the PHP executable  each time it encounters a PHP script. Add the following below any  existing entries in the "Action" section.
Action application/x-httpd-php "/php/php.exe"
If you notice, we have used the "ScriptAlias" reference, "/php/"  portion will be recognised as the scriptAlias configured above, this is  sort a path alias which will correlate to your PHP installation path  configured previously. In other words, don't put "c:/php/php.exe" or  "c:/Program Files/php/php.exe" in that directive, put "/php/php.exe",  Apache WILL work it out if correctly configured.


PHP as a CGI or a module?

 When running PHP through your web server, there are two distinct options: running it using PHP's CGI SAPI, or running it as a module for the web server. Each have their own benefits, but, overall, the module is generally preferred.

 Running PHP as a CGI means that you basically tell your web server the location of the PHP executable file, and the server runs that executable, giving it the script you called, each time you visit a page. That means each time you load a page, PHP needs to read php.ini and set its settings, it needs to load all its extensions, and then it needs to start work parsing the script - there is a lot of repeated work.

 When you run PHP as a module, PHP literally sits inside your web server - it starts only once, loads its settings and extensions only once, and can also store information across sessions. For example, PHP accelerators rely on PHP being able to save cached data across requests, which is impossible using the CGI version.

 The obvious advantage of using PHP as a module is speed - you will see a big speed boost if you convert from CGI to a module. Many people, particularly Windows users, do not realise this, and carry on using the php.exe CGI SAPI, which is a shame - the module is usually three to five times faster.

 There is one key advantage to using the CGI version, though, and that is that PHP reads its settings every time you load a page. With PHP running as a module, any changes you make in the php.ini file do not kick in until you restart your web server, which makes the CGI version preferable if you are testing a lot of new settings and want to see instant responses.