Setting PHP CGI on Mac

If you wish to use PHP CGI on Mac OS X you will need PHP build with the CGI enabled. By default, PHP version 4.3 and higher is built with "-enable-cli" option so default interface is CLI (Command Line Interface). You can check this by running "php -v" from the shell. Output will probably be something like this:

PHP 4.4.4 (cli) ....

CLI does not output any headers and output is plain text by default. More details about this is available here.

In order to use PHP with the wxWebServer (or any other CGI application), you will need to build your own PHP binaries with CGI SAPI. It's simple, it takes only 15 minutes and you will be proud of yourself for having a "personal" build of the PHP. Here is the procedure, step by step:

  1. Download the complete source code for the latest PHP version from php.net. Instructions here are for PHP 5.2.3 but should work for PHP 4 as well. I tested with 4.4.7 too.

  2. Unpack downloaded archive somewhere, for example ~/Temp/php-5.2.3

  3. Open terminal window (shell) and go to folder where you unpacked files:

    cd ~/Temp/php-5.2.3
      

  4. Decide where you want to install this custom PHP build and create folder. This document will assume it's custom folder under your home folder, for example if user name is webdev it's /Users/webdev/custom/php5

  5. Create that folder (in Finder or in Terminal with "mkdir ~/custom; mkdir ~/custom/php5")
  6. Go back to that Terminal window, you should still be in ~/Temp/php-5.2.3.
    Run ./configure with CLI disabled if you are planning to use this private PHP build only for CGI. Modify paths and options to reflect your preferences.
    You don't need option "--disable-cli", you can replace it with "--enable-cli" if you plan to use this PHP for something more. You will get both binaries but you will need to use php-cgi in wxWebServer.
    Here is a full command line for .configure that should produce PHP with all common options and CLI disabled:

    ./configure --prefix=/Users/webdev/custom/php5 --disable-cli --enable-cgi --enable-trans-sid
      --enable-mbstring --with-xml --enable-exif --enable-mbregex --enable-dbx --enable-sockets
      --with-iodbc=/usr --with-curl=/usr --with-config-file-path=/etc --sysconfdir=/private/etc
      --with-mysql=/usr/local/mysql --without-pear
      

  7. When configure is completed, run

      make install
      

  8. Edit your profile (~/.profile) to add new php path before existing one. Add something like this to the end of your .profile file:

    PATH=~/custom/php5/bin:$PATH
    export PATH
      
    If you don't know how to edit profile from the command line, just create a plain text file in TextEdit containing those 2 lines and save it in your home folder (~/Users/webdev) as newpath.txt. Then open Terminal and enter command:
    cat newpath.txt >> .profile
      
    The easiest way for changes to take effect is to log out and log back in (or you can use source command).

  9. Congratulations, you now have a custom php installation! If you wish to use other PHP just comment your changes to .profile

  10. Verify your new PHP is the default one:

    # which php-cgi
    /Users/webdev/custom/php5/bin/php-cgi
    #php-cgi -v
    PHP 5.2.3 (cgi) (built: Jul 11 2007 00:48:59)
    Copyright (c) 1997-2007 The PHP Group
    Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
      

  11. If you wish, you may now delete the source files in ~/Temp/php-5.2.3. To remove you custom PHP just delete target folder, i.e. "/Users/webdev/custom/php4".

Once you have custom php installation, configure wxWebServer to use new php binaries. Regardless if you enabled or disabled CLI, you can use php-cgi (output of "which php-cgi", in this case "/Users/webdev/custom/php5/bin/php-cgi"). If you disabled CLI build, you may also use php binary as it will be CGI build. That's it, enjoy your personal PHP build!