PHP7 + Apache2 on OpenWrt

The solution is to ditch PHP+Apache approach entirely and let CGI handle it instead.
Also, don't bother with the following php7 packages if you're using Apache:

  • php7-cgi
  • php7-fpm

Use php7 + php7-cli packages instead. This will probably save someone days of frustration :confounded: :cry:

Finally, don't forget to add print "Content-type: text/html\n\n"; right after the <?php tag for your php/cgi script to return data to your browser.

I think both Apache and especially PHP7 needs to be rebuilt from source to support direct loading of PHP modules (libphp7.so or mod_php.so do not exist in OWrt Apache package right now)

Apache Configuration:

LoadModule cgid_module lib/apache2/mod_cgid.so
LoadModule cgi_module lib/apache2/mod_cgi.so

<IfModule alias_module>
ScriptAlias /cgi-bin/ "/usr/share/apache2/cgi-bin/"
</IfModule>

<Directory "/usr/share/apache2/cgi-bin">
    AllowOverride None
    Options ExecCGI
    AddHandler cgi-script .cgi .php
    Require all granted
</Directory>

Don't forget to delete Action application/x-httpd-php ................ at the end of the Apache config file.

php.ini

default_mimetype = "text/html"

doc_root =
user_dir =
extension_dir = "/usr/lib/php"
enable_dl = On
cgi.force_redirect = 1
cgi.fix_pathinfo=1

.

Test PHP/CGI File (saved in /usr/share/apache2/cgi-bin, chmod 755 index.cgi permission)

#!/usr/bin/php-cli
<?php
print "Content-type: text/html\n\n";
echo "<h1>PHP is working</h1>";
?>

If this was useful, :+1: this answer.

Capturess

1 Like