Apache Error: PHP code not working but plain text is displayed

Updated: January 21, 2024 By: Guest Contributor Post a comment

The Problem

When working with PHP applications on an Apache server, occasionally developers face a situation where their PHP code is not executed. Instead, the server sends the PHP code as plain text to the browser. This is not just a nuisance but also a security threat as it exposes your server-side code to the public. This tutorial aims to help you troubleshoot and fix the issue where PHP code is simply displayed as plain text by the web server.

Causes & Solutions for the Error

Check If PHP is Installed

Begin by ensuring that PHP is installed on your system. On a Linux system, you can check this by running:

php -v

If PHP is not installed, you need to install it. For Ubuntu, you can install PHP with the following command:

sudo apt-get install php libapache2-mod-php

Verify PHP Module is Enabled

After ensuring that PHP is installed, make sure the PHP module is enabled in Apache. Usually, this is done automatically but you can check with:

apache2ctl -M | grep php

If you don’t see a ‘php’ module, enable it with:

sudo a2enmod php7.x

Remember to replace ‘7.x’ with your PHP version number. After enabling the module, restart Apache:

sudo systemctl restart apache2

Inspect the Apache Configuration File

The Apache configuration must include the proper directives for processing PHP files. Ensure that the following lines are present in the Apache configuration file (httpd.conf) or the respective virtual host file:

<FilesMatch \.php>
   SetHandler application/x-httpd-php 
</FilesMatch>

This tells Apache to process files with a .php extension using PHP. If these lines are not present, add them and restart Apache.

Check if .htaccess Overrides are Allowed

If your application uses a .htaccess file to manage URL rewrites and other configurations, Apache must be configured to allow overrides. Look for the ‘AllowOverride’ directive in your Apache configuration files and make sure it is set to ‘All’ for the directory in question. For example:

<Directory /var/www/html>
    AllowOverride All
</Directory>

Don’t forget to restart Apache after making the changes.

Consider MIME Type Issues

Apache needs to be aware of the correct MIME type for PHP files. This is typically not a problem, but if there are custom MIME type settings, ensure that PHP files are interpreted correctly with:

 AddType application/x-httpd-php .php

Testing with PHP

After you’ve made changes to the configuration, test if PHP is now working correctly. Create a PHP file called test.php with the following content:

<?php phpinfo(); ?>

Upload this file to your web server’s document root and access it via a browser. If PHP is configured correctly, you should see a page displaying details about your PHP environment.

PHP File Permissions

Incorrect file permissions can also be the culprit. Verify that your PHP files have the correct permissions, typically ‘644’ for files and ‘755’ for directories. To set the file permissions for a PHP file, use:

chmod 644 test.php

Error Logs

Error logs can provide valuable information. Check your Apache error logs to look for clues on what might be wrong. The error log is typically found at:

/var/log/apache2/error.log

If you notice any errors that seem related to PHP file execution, this can guide you in resolving the problem.

Concluding Notes

If you’ve confirmed that PHP is installed correctly, the Apache configuration file directs the server to handle PHP files with the PHP interpreter, .htaccess files are allowed when needed, file permissions are correct, and your error logs don’t point to other issues, but PHP files are still not being processed as expected, then it may be time to seek help from your hosting provider or a professional with experience in Apache/PHP environments.

Working through these checks systematically should help resolve why PHP code is not executing as expected and ensure that your Apache server is correctly serving PHP content. If problems persist, consider a deeper dive into custom server settings, security modules, or potentially conflicting software.