Setting PHP as A CGI with Apache and SUEXEC

To setup PHP to run as a CGI for extra security on apache using SUEXEC you will need to follow the steps below:
  • Setup apache with SUEXEC and make sure it works (See the apache site for details).
  • Setup your apache site/vhosts for CGI from a /cgi-bin directory
  • Install PHP from source and compile with the --enable-force-cgi-redirect option eg:

    ./configure -enable-force-cgi-redirect

  • Setup your vhost/site as follows:

    Options +ExecCGI Action php-script /cgi-bin/php AddHandler php-script .php

This will make all files ending in .php will be passed through our own PHP script in the /cgi-bin

  • Create the following /cgi-bin/php file with just one line:

    !/php

eg:

#!/usr/bin/php

Change the permissions on this file to 0755 like any other CGI script

  • Create an example PHP script as /htdocs/test.php:

  • Test by browsing to: /test.php in your browser.

Last updated: 25/11/2005