Centos – Can’t get PHP to work on Apache VirtualHosting environment

apache-2.2centosPHPvirtualhost

Trying to setup an Apache VirtualHosting environment with PHP in a freshly created CentOS node under DigitalOcean cloud server.

The trouble is, although Virtual hosting is working per se, PHP isn't.

Server Setup:

  • Web Server : CentOS 6.5 with Apache/2.2.15
  • PHP Version: 5.3.3 (To be used as an Apache Module)

Steps Taken:

Logging in as the root user,

  • Installing Apache and php : yum -y install httpd php
  • Creating a new virtual host configuration file like below:

File: /etc/httpd/conf.d/vhosts.conf

NameVirtualHost *:80
<VirtualHost *:80>
     ServerAdmin admin@example.org
     ServerName example.org
     ServerAlias www.example.org
     DocumentRoot /srv/www/example.org/public_html/
     ErrorLog /srv/www/example.org/logs/error.log
     CustomLog /srv/www/example.org/logs/access.log combined
</VirtualHost>
  • Creating new directories for the virtual host site

Like below,

mkdir -p /srv/www/example.org/public_html
mkdir -p /srv/www/example.org/logs
  • Placing a .php and .html test files like below:

Files under /srv/www/example.org/public_html/ (DocumentRoot)

echo "<HTML>It work's</HTML>"      > /srv/www/example.org/public_html/index.html
echo "<?php echo ("It works"); ?>" > /srv/www/example.org/public_html/index.php

Next, when I point my browser to index.html, it works. But when I point my browser to index.php I get a blank page.

Troubleshooting

(Things that I already did/checked without any luck)

  • Ensured Apache is running and DocumentRoot is accessible
  • All the directories are 755 and files 644
  • Checked Apache access log. The html page requests are there with 200 status code, but no .php page request present in access log
  • No error in Apache or PHP error log, no error in /var/log/messages
  • Tried adding php_admin_flag engine on in vhost config file

Also see below the contents of /etc/httpd/conf.d/php.conf

<IfModule prefork.c>
  LoadModule php5_module modules/libphp5.so
</IfModule>
<IfModule worker.c>
  LoadModule php5_module modules/libphp5-zts.so
</IfModule>
AddHandler php5-script .php
AddType text/html .php
DirectoryIndex index.php

Best Answer

In my experience PHP renders a blank page when it encounters a serious underlying error. It is usually easy to fix, but unfortunately nothing is typically logged in the Apache logs, PHP logs, or syslog making the troubleshooting process difficult.

Find your php.ini configuration file using:

php -i | grep "php.ini"

This should produce something like this:

Configuration File (php.ini) Path => /etc
Loaded Configuration File => /etc/php.ini

Edit this configuration file (/etc/php.ini in this case) with your favorite text editor and be sure that displaying errors and startup errors is turned on. The entries should look like this:

display_errors = On
display_startup_errors = On

Finally restart Apache so that it reads the new PHP configuration and access your page via curl or a browser. This should produce an error message that will help in the troubleshooting process.