Apache Error: PHP files downloaded instead of executed

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

The Error

If you have encountered an issue where your PHP files are being downloaded instead of executed when visiting a website running on an Apache server, it can be both confusing and frustrating. This behavior usually indicates a misconfiguration on the server that prevents PHP files from being processed correctly. In this tutorial, we’ll explore several solutions to fix this common Apache error.

Solutions

Solution 1: Ensure PHP Module is Installed and Enabled

Before PHP files can be executed, the PHP module must be installed and enabled in Apache.

  1. Check if PHP is installed by running php -v in the terminal.
  2. If PHP is not installed, you’ll need to install it using your system’s package manager.
  3. Once PHP is installed, enable the PHP module in Apache by running a2enmod php, replacing with the version number of PHP installed on your system.

Example of enabling PHP module in Apache:


a2enmod php7.4

Note: After making these changes, be sure to restart Apache for the changes to take effect.

Solution 2: Check Apache Configuration for AddType

The AddType directive in the Apache configuration files (.htaccess or apache2.conf) tells the server to treat files with the .php extension as PHP files.

  1. Open your Apache configuration file or .htaccess file.
  2. Look for a line containing AddType application/x-httpd-php .php.
  3. If such a line does not exist, add it to the file.
  4. Save changes and restart Apache.

Example of AddType directive in Apache configuration:


AddType application/x-httpd-php .php

Note: Incorrect configuration or file permissions might prevent this directive from functioning correctly, so ensure you have the correct permissions set for your configuration files.

Solution 3: Verify mime_module is Enabled

The mime_module should be enabled in Apache since this module allows Apache to identify and correctly handle different types of files based on their MIME types.

  1. Check if mime_module is loaded by reviewing your Apache configuration.
  2. If it’s not, enable it using a2enmod mime.
  3. Restart Apache to apply changes.

Example of enabling mime_module in Apache:

a2enmod mime

Note: Disabling unnecessary modules can enhance server performance, but essential modules like mime_module must be enabled for proper functioning.

Final Words

Fixing the problem of PHP files being downloaded instead of executed usually comes down to ensuring that PHP is correctly installed, the PHP module is enabled, and the server is properly configured to handle PHP files. While each of the solutions given above is relatively simple to implement, understanding the interaction between Apache modules, configuration directives, and system permissions is crucial in maintaining and troubleshooting a web server. After you apply these changes, your PHP should be processed as expected, and your web application can run smoothly.