Php – Apache executes HTML, but not PHP; PHP CLI works

apache-2.2PHPphp-cliunix

I am running php 5.3.3 on rhel 6.2 with Apache 2.2.15 and am failing to get PHP code interpreted by Apache.

There are no errors (in the system log, httpd/error_log, or php_errors.log) – I have enabled error reporting in php.ini:

error_reporting = E_ALL & ~E_DEPRECATED
display_errors = On
log_errors = On
error_log = /var/log/php_errors.log

Yet, when i view my index.php, which contains the following code, directly from my browser (e.g. myserver.com/index.php), nothing appears but a white-screen:

<?php echo 'Hello php World'; ?>

When executed by php on the command line, I get the expected text output to the terminal ("Hello php World").

When, I add some HTML to index.php as in:

<html>
    <p>Hello from within html</p>
    <?php echo 'Hello php World'; ?>
</html>

and view it with my browser, it returns just "Hello from within html." However, when executed from the command line, I get:

<html>
    <p>Hello from within html</p>
    Hello php World</html>

So, the PHP is parsed and interpreted via the CLI, but not by Apache.

I have confirmed that index.php and its parent file structure are owned by apache:apache and that the selinux context of the apache modules :

chcon -t httpd_sys_script_exec_t  /usr/lib/httpd/modules/*
chcon -t httpd_sys_script_exec_t  /usr/lib/php/modules/*

and of the .php files to be parsed:

chcon -R -h -t httpd_sys_content_t /export1

are correct. I don't think selinux is the problem because I don't get any permission errors, and I've temporarily disabled selinux echo 0 >/selinux/enforce with no resultant success of the php being interpreted by apache.

The php module is being loaded with its configuration and the .php extension is being understood as configured in httpd.conf:

LoadModule php5_module /usr/lib/httpd/modules/libphp5.so
AddType application/x-httpd-php .php .phtml
PHPIniDir /etc/

And, apache knows to look for index.php from:

DirectoryIndex index.html index.html.var index.php

as set in httpd.conf.

I have exhausted my ideas of where my problem is – even tried reinstalling PHP – and sure would appreciate any ideas. In case it's of use, here are the contents of /var/log/httpd/error_log when I start up apache:

[Fri Feb 03 13:44:53 2012] [notice] SELinux policy enabled; httpd running as context unconfined_u:system_r:httpd_t:s0
[Fri Feb 03 13:44:53 2012] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Fri Feb 03 13:44:53 2012] [warn] module php5_module is already loaded, skipping
[Fri Feb 03 13:44:53 2012] [notice] Digest: generating secret for digest authentication ...
[Fri Feb 03 13:44:53 2012] [notice] Digest: done
[Fri Feb 03 13:44:54 2012] [warn] mod_wsgi: Compiled for Python/2.6.2.
[Fri Feb 03 13:44:54 2012] [warn] mod_wsgi: Runtime using Python/2.6.6.
[Fri Feb 03 13:44:54 2012] [notice] Apache/2.2.15 (Unix) DAV/2 PHP/5.3.3 mod_ssl/2.2.15 OpenSSL/1.0.0-fips mod_wsgi/3.2 Python/2.6.6 mod_perl/2.0.4 Perl/v5.10.1 configured -- resuming normal operations

Please help!

Best Answer

It seems that php5_module is already loaded by yourself and loaded later by the default rhel6 configuration or the configuration on rhel6 is loading the module php5_module... which in itself doesn't matter except for the fact that the configuration you are entering is being overridden by the default rhel6 configuration.

Looking at step 11 from Dan's site, which has worked for me in the past whenever I forgot and needed a quick reminder:

# Make sure there's only **1** line for each of these 2 directives:

# Use for PHP 4.x:
#LoadModule php4_module modules/libphp4.so
#AddHandler php-script .php

# Use for PHP 5.x:
LoadModule php5_module modules/libphp5.so
AddHandler php5-script .php 

# Add index.php to your DirectoryIndex line:
DirectoryIndex index.html index.php

AddType text/html .php

# PHP Syntax Coloring
# (optional but useful for reading PHP source for debugging):
AddType application/x-httpd-php-source phps