Documenting Problems That Were Difficult To Find The Answer To

Using PHP as CGI in Apache 2.4

I wanted to use PHP in CGI mode on a webserver. Granted – it’s not as convenient or fast as mod_php. I was using this to allow myself to run phpMyAdmin on a low memory server.

I created an .htaccess in my phpMyAdmin path:

# create an action called "dophp" which calls /phpMyAdmin/dophp.cgi
Action dophp /phpMyAdmin/dophp.cgi

# allow files ending in .cgi to be executed (our script dophp.cgi)
<FilesMatch "\.cgi$">
  Options +ExecCGI
  SetHandler cgi-script

# all PHP files should be handled by a call to /phpMyAdmin/dophp.cgi
<FilesMatch "\.php$">
  SetHandler dophp

Next I created the file dophp.cgi in the same directory:


exec /usr/bin/php-cgi

I ensured that my Apache configuration had enabled modules actions and either cgid (if you’re not using the prefork MPM) or cgi (if you are using the prefork MPM).

Lastly I needed to edit /etc/php/7.0/cgi/php.ini and change the following line:

cgi.force_redirect = 0

Otherwise I’d get the error in my Apache log:

malformed header from script 'dophp.cgi': Bad header: <b>Security Alert!</b> The PHP CGI cannot be accessed directly.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: