Table of Contents

php cgi

Goal: build and install php as a cgi program to interface with the Apache web server

Background

In July of 2006, the Fink project stopped supporting the 10.4 transitional branch, and I needed to upgrade all of my Fink installed programs. In September, there was a mapping project that only worked with php as a cgi program. After removing all of the old files and installing Fink from scratch, I found that most things were available as pre-compiled binaries such as Apache2, but php was only available to build from source in the unstable branch.

Rather than open up the unstable branch for all programs, I opted to install everything else from stable binaries and build php from source. One advantage to using php as a cgi program is that I can upgrade Apache without having to rebuild php. One drawback is that performance speed is degraded.

Apple's php binary

It should be noted that the php binary included with Mac OS X is only a cli build (command line interface). You cannot complete the steps below using this binary. In order for the steps below to work, you must compile a php binary from source - in cgi mode, not cli.

The frustrating error that may show up if you try to use the wrong binary is an Internal Server Error from apache. The log might say “premature end of script headers.” Since your php binary wasn't built with cgi enabled, it doesn't generate proper html code, but rather plain text to be read in a terminal. The initial response from a proper cgi binary of php should contain “Content-type: xxxx/xxxx”

Setup

  1. Install Fink as usual
  2. Install Apache2 binary
  3. binaries, development files, and shared libraries from the following may need to be installed before compiling php (use fink):
    • gd2
    • libjepg
    • libpng/libpng3
    • freetype2
    • libmcrypt
    • openldap (if you want ldap)
    • gettext (req'd by phpldapadmin)
  4. download php source from http://www.php.net (version used here was 4.4.4)
  5. extract the php archive, and build using the following commands:
    ./configure --prefix=/sw --mandir=/sw/share/man --infodir=/sw/share/info --enable-force-cgi-redirect --with-config-file-path=/sw/etc/php4/cgi --with-gd=/sw --with-jpeg-dir=/sw --with-ldap=/sw --with-gettext=/sw --with-png-dir=/sw --with-zlib-dir=/sw --with-freetype-dir=/sw/lib/freetype2 --with-mcrypt=/sw --enable-fastcgi --enable-exif --without-mm --without-pgsql
    
    make
  6. copy the <php 4.4.4 dir>/sapi/cgi/php file to your apache webserver's cgi executables directory, and set the ownership permissions to be that of the other web server files (probably www:admin)
  7. add the following lines to your apache configuration file (/sw/etc/apache2/httpd.conf)
    ## php as cgi for all
    Action php4-script /cgi-bin/php
    AddHandler php4-script .php
  8. modify the directory index:
    <IfModule mod_dir.c>
        DirectoryIndex index.html index.htm index.php
    </IfModule>
  9. restart apache or reboot and test a php script with the .php extension

* NOTE: it is possible that with this php install there are some needed features that were not enabled during the build process; this is the config command that fink was going to use for the unstable branch

configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --disable-dependency-tracking --with-apxs --with-ldap=/usr --with-kerberos=/usr --enable-cli --with-zlib-dir=/usr --enable-trans-sid --with-xml --enable-exif --enable-ftp --enable-mbstring --enable-mbregex --enable-dbx --enable-sockets --with-iodbc=/usr --with-curl=/usr --with-config-file-path=/etc --sysconf

Setup (using custom MySQL)

* same steps as above, with the following modification to the configure command:

--with-mysql=/sw 

full command would then be:

./configure --prefix=/sw --mandir=/sw/share/man --infodir=/sw/share/info --enable-force-cgi-redirect --with-config-file-path=/sw/etc/php4/cgi --with-mysql=/sw --with-gd=/sw --with-jpeg-dir=/sw --with-ldap=/sw --with-gettext=/sw --with-png-dir=/sw --with-zlib-dir=/sw --with-freetype-dir=/sw/lib/freetype2 --with-mcrypt=/sw --enable-fastcgi --enable-exif --without-mm --without-pgsql