Why Is Apache Showing PHP Code Instead Of Executing It?

Published August 27, 2024

Problem: Apache Displaying PHP Code

When Apache serves PHP files, it sometimes shows the raw PHP code instead of executing it. This issue stops PHP-based websites and applications from working correctly. Several configuration problems or missing parts in the server setup can cause this problem.

Potential Causes for Apache Not Executing PHP

Apache Configuration Issues

Apache may not execute PHP code due to configuration problems. A missing PHP module can prevent Apache from processing PHP files. If the PHP module is present but not loaded correctly, Apache won't recognize PHP code. Incorrect file associations can also cause Apache to treat PHP files as plain text instead of scripts.

Tip: Check Apache Configuration

To verify if Apache is correctly configured to handle PHP, look for the following line in your Apache configuration file (usually httpd.conf or apache2.conf):

LoadModule php_module modules/libphp.so

Make sure this line is uncommented and the path to the PHP module is correct.

PHP Installation Problems

Issues with PHP installation can lead to execution problems. An incomplete PHP setup might result in missing components needed for proper functioning. Using a PHP version that doesn't match your Apache installation can cause conflicts. A misconfigured php.ini file may prevent Apache from recognizing or executing PHP code correctly.

Solutions for Apache PHP Execution Issues

Verifying Apache PHP Module

To fix Apache PHP execution issues, check the Apache configuration for the PHP module. Look for a line that loads the PHP module, like LoadModule php_module modules/libphp.so. Make sure this line is not commented out and the path to the module is correct. If the module is missing, you may need to install it or update your Apache configuration.

Check that the module path is correct. The path should point to your PHP installation location. If you're unsure about the correct path, consult your PHP installation documentation or use the command whereis php on Unix-based systems to find the PHP files.

To confirm module activation, restart Apache and check the error logs for messages related to PHP module loading. If there are no errors, the module should be active.

Tip: Verify PHP Module with phpinfo()

Create a PHP file with the phpinfo() function to check if PHP is working correctly:

<?php
phpinfo();
?>

Save this as info.php in your web root directory and access it through your browser. If you see the PHP configuration page, the module is working.

Configuring Apache to Handle PHP Files

Add the correct MIME type for PHP by including this line in your Apache configuration:

AddType application/x-httpd-php .php

This tells Apache to treat files with the .php extension as PHP scripts.

Set up file extensions by adding any other extensions you want Apache to treat as PHP files. For example:

AddType application/x-httpd-php .php .phtml

After making these changes, restart Apache to apply the new configuration. On most systems, you can do this with the command sudo service apache2 restart or sudo systemctl restart apache2.

Troubleshooting PHP Installation

Confirm that PHP is installed by running php -v in the command line. This command shows the PHP version and installation details. If it doesn't work, PHP may not be installed or not in your system's PATH.

Check PHP version compatibility with your Apache installation. Make sure you're using a PHP version that works with your Apache version. Check the documentation for both Apache and PHP to find compatible versions.

Review your php.ini settings to make sure PHP is set up correctly. Look for settings like display_errors and error_reporting to help diagnose any PHP-related issues. You can find the location of your php.ini file by running php --ini in the command line.