Using PHP as CGI in Apache 2.4
September 13, 2017
Posted by on
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)
# all PHP files should be handled by a call to /phpMyAdmin/dophp.cgi
Next I created the file dophp.cgi in the same directory:
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.