Executing PHP scripts as CGI scripts without using a "sh-bang" line

If you run PHP scripts using the CGI method you may have to use the following 'sh-bang' line at the start of your code:

#!/usr/bin/php

This can cause annoying problems if you need to install third-party PHP applications. As a fix you can use the kernel's support for misc binaries.

  • First compile your kernel with the following options:

    CONFIG_BINFMT_MISC=y CONFIG_PROC_FS=y

  • Add the following lines to your fstab:

    proc /proc proc defaults 0 0 none /proc/sys/fs/binfmt_misc binfmt_misc defaults 0 0

  • Add the following lines to a boot scripts such as /etc/init.d/boot.local:

    php bin format support

    /bin/echo ':PHP:E::php::/usr/bin/php:' > /proc/sys/fs/binfmt_misc/register /bin/echo ':PHP3:E::php3::/usr/bin/php:' > /proc/sys/fs/binfmt_misc/register /bin/echo ':PHP4:E::php4::/usr/bin/php:' > /proc/sys/fs/binfmt_misc/register

(make sure the lines are not wrapped)

  • Reboot

  • Test by running a php script from the shell using the following method:

    ./script.php

The script still needs to be executable, but without starting with a "sh-bang" line.

Last updated: 25/11/2005