PHP CGI

Gentoo

Install apache with threads, mpm worker, plus mod_fcgid package and add -D FCGID to apache2 conf.

In the Directory configuration of every site, add this:

<Directory /var/www/localhost/htdocs>
Options -Indexes FollowSymLinks +ExecCGI
AddType text/html .php .phps
AddHandler php-fastcgi .php .html .htm .js
Action php-fastcgi /cgi-bin/php.fcgi
DirectoryIndex index.php index.html
</Directory>

Add this so it is global:

ScriptAlias /cgi-bin/ /var/www/localhost/cgi-bin/
<Directory /var/www/localhost/cgi-bin/>
        Options +ExecCGI
        Order allow,deny
        Allow from all
</Directory>

The php.fcgi file is created in /var/www/localhost/cgi-bin/. It should be executable, and it holds the configuration settings for that CGI installation.

#!/bin/bash
PHP_CGI=/usr/lib/php5.3/bin/php-cgi
PHP_FCGI_CHILDREN=4
PHP_FCGI_MAX_REQUESTS=1000
export PHP_FCGI_CHILDREN
export PHP_FCGI_MAX_REQUESTS
exec $PHP_CGI
Parsing .phps

If you want to use .phps to parse the source file and display it in colored format, you need to update the Apache config file and create a new CGI shell script with slight differences.

AddHandler phps-fastcgi .phps
Action phps-fastcgi /cgi-bin/phps.fcgi
#!/bin/bash
PHP_CGI="/usr/lib/php5.3/bin/php-cgi -s"
...

Migration to PHP CGI

There will be some things that need to change when migrating from a module to CGI.

.htaccess PHP changes

Things like php_value and php_flag must be put somewhere else.

If you are running a custom php.ini for the site, you can make them directly there. Otherwise, you can use .user.ini and override php.ini changes.

Debugging

No input file

If you run into an issue where it says “No input file specified”, then the PHP binary is not executing the code for some reason, and will send a 404 HTTP header. Check the configuration file for errors, turn on error reporting, or watch the error log to flush out the issue.

The most likely culprit is open_basedir.