$ sudo apt-get install php5-cgi php5-cli libapache2-mod-php5
1. Let's write a simple PHP script and put this file (sapi.php) in the document root (/var/www). Content as shown below.
$ cat /var/www/sapi.php
2. Let's run this script from the console.
a) Plain old PHP binary, not through the web server.
$ php -f /var/www/sapi.php cli
b) Similarly but using the php5-cgi binary instead.
$ php5-cgi /var/www/sapi.php X-Powered-By: PHP/5.4.6-1ubuntu1.1 Content-type: text/html cgi-fcgi
c) Again, but using curl program, a HTTP client making request to the HTTP server (Apache).
$ curl http://localhost/sapi.php apache2handler
3. What does the result in step 2 tell us?
First, the method php_sapi_name() which will give us the type of the Server API (hence the name SAPI) used by the PHP binary. Think SAPI like a plugin module with different abstraction layers for different type of APIs. Second, an explanation of each SAPI layers in step 2.
2a - PHP standalone interpreter itself.
2b - Common Gateway Interface (CGI). A standard way of a HTTP server to delegate content to a program. In this case the PHP interpreter. Compare to 2a, this interface do pass extra environment variables to the PHP program.
2c - mod_php where the PHP interpreter is embedded as module in Apache web server. Similarly to CGI, both are the only two common communications between Apache and PHP. As mentioned, the main different is CGI delegate PHP script to an external binary where for mod_php, it was passed to Apache itself.
Another way to test this out is to get the phpinfo() result by repeating the step 2. Result obtained will be quite different.
4. Why I am writing about this?
I was trying to write a console script to extract Zend Optimizer+ statistics result but the result will not be the same running through plain interpreter and mod_php. Because my lack of fundamental understanding of this concept made me wasted 3 hours. Well, you have to learn the lesson the hard way somehow.