Resolving Apache Downloading PHP Files Instead of Executing Them: Configuration Analysis and Practical Guide

Nov 21, 2025 · Programming · 12 views · 7.8

Keywords: Apache Configuration | PHP Module Loading | File Type Association

Abstract: This article addresses the issue where Apache 2.2.15 on CentOS 6.4 downloads PHP 5.5.1 files rather than executing them, providing an in-depth analysis of configuration errors. By verifying PHP module loading paths, correcting file type association directives, and offering a complete troubleshooting workflow, it helps users quickly restore normal PHP script execution. The article includes specific configuration examples and system commands to ensure practical and actionable solutions.

Problem Background and Phenomenon Analysis

On CentOS 6.4 systems, after upgrading PHP from 5.3.x to 5.5.1, the Apache server ceases to parse PHP files and instead serves them as downloadable content to clients. This abnormal behavior typically stems from missing or misconfigured PHP processing modules in Apache configuration. Specifically, when a browser requests a .php file, the server returns the file source code instead of the execution result, severely disrupting the normal operation of web applications.

Core Configuration Error Diagnosis

Based on the user-provided snippets from httpd.conf and php.conf, the initial configuration contains multiple redundant AddHandler and AddType directives:

AddHandler application/x-httpd-php .php5 .php4 .php .php3 .php2 .phtml
AddType application/x-httpd-php .php5 .php4 .php .php3 .php2 .phtml
AddType application/x-httpd-php-source .phps
AddHandler php5-script .php

These directives present two main issues: first, AddType application/x-httpd-php and AddHandler php5-script may conflict, causing Apache to fail in correctly identifying the PHP processor; second, the extensive list of file extensions increases configuration complexity. After the user removed AddType application/x-httpd-php, Apache stopped downloading files but displayed partial source code, indicating that the PHP module was loaded but not properly associated with the file type.

PHP Module Loading Verification

The prerequisite for Apache to execute PHP files is loading the corresponding PHP module. In php.conf, the module loading directive uses conditional checks to adapt to different MPM modes:

<IfModule !worker.c>
  LoadModule php5_module modules/libphp5.so
</IfModule>
<IfModule worker.c>
  LoadModule php5_module modules/libphp5-zts.so
</IfModule>

However, the module path modules/libphp5.so might not be applicable to the newly installed PHP 5.5.1. The correct approach is to use the apache2ctl -M | grep php command to verify loaded modules and ensure the path points to an existing library file, such as modules/mod_php55.so. If the module is not loaded, add or correct the LoadModule php5_module modules/mod_php55.so directive in httpd.conf.

File Type Association Correction

After removing redundant configurations, only essential file type association directives should remain. The recommended configuration is as follows:

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

Here, AddType application/x-httpd-php .php explicitly instructs Apache to recognize .php files as PHP scripts, while AddType application/x-httpd-php-source .phps is used for source code highlighting. Avoid using AddHandler unless specifically required to prevent directive overlap.

Supplementary Troubleshooting and Optimization Suggestions

Beyond the main configuration, other potential factors must be considered: first, check if .htaccess files contain conflicting directives, such as AddHandler x-mapp-php6 .php, which may override the main configuration and cause PHP processing to fail; second, clear browser cache, as browsers like Chrome might cache old responses, misleading fault diagnosis; finally, verify PHP installation integrity with php -v, and reinstall using yum remove php* and yum install php if necessary.

Complete Solution Implementation Steps

  1. Back up existing Apache configuration files.
  2. Edit httpd.conf to ensure LoadModule php5_module modules/mod_php55.so exists and the path is correct.
  3. Add AddType application/x-httpd-php .php and AddType application/x-httpd-php-source .phps in httpd.conf or a separate configuration file.
  4. Remove all redundant AddHandler and AddType directives.
  5. Execute service httpd restart to restart the Apache service.
  6. Test PHP file execution using different browsers to avoid cache interference.

By following these steps, the issue of Apache downloading PHP files can be systematically resolved, ensuring efficient web server operation.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.