Apache is downloading php files instead of displaying them

Apache is downloading php files instead of displaying them

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Apache is downloading php files instead of displaying them

When upgrading from PHP 5.3.x to PHP 5.5.1 on a CentOS 6.4 system running Apache 2.2.15, it is common to encounter a situation where the server treats .php files as plain text downloads rather than executing them. This behavior usually indicates a misconfiguration in how Apache maps file extensions to internal handlers or content types, rather than a missing PHP installation.

The root cause often lies in the interplay between AddHandler and AddType directives within your httpd.conf. In the scenario described, the configuration explicitly defines application/x-httpd-php for .php files via AddHandler and AddType. However, Apache relies on a specific priority order when determining how to process a file. If an AddType directive is present without a corresponding handler that explicitly sets the script execution logic, or if it conflicts with existing mappings, Apache may serve the raw source code or force a download.

The user's initial configuration included:

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

The presence of AddHandler php5-script .php is particularly problematic in this context. When multiple handlers are defined for the same extension, Apache can become confused about which module should process the request. Specifically, if php5-script is registered but the main handler logic isn't explicitly prioritized or if a conflicting AddType forces a content-type change that bypasses the handler, the result is often a file download or partial content rendering.

Removing the first AddType line resolved the download issue, but it introduced a new problem: Apache began serving the source code of the PHP files instead of parsing them. This happens because application/x-httpd-php tells Apache that the file contains executable scripts, but without the correct handler association or if the module isn't loaded correctly, Apache might just serve the MIME type as text/html or text/plain depending on other rules.

To fix this, you need to ensure that the PHP module is actually loaded and that the handler mapping is unambiguous. The provided php.conf snippet uses conditional loading based on the MPM (Multi-Processing Module):

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

This is correct for CentOS, which typically uses the mpm_prefork or mpm_worker modules. If you are running on a system with mpm_prefork, the first block loads the standard library. If you switch to mpm_worker or mpm_event, you must load the thread-safe version (-zts). Ensure that your current MPM matches the loaded module; otherwise, PHP will not execute even if the handler is correct.

After verifying the module load, simplify the handler configuration. You should avoid defining the same extension multiple times with different handlers or types unless necessary. A clean approach for Apache 2.2.x on CentOS looks like this:

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

# If you want to show source code, use a distinct extension and handler
AddType application/x-httpd-php-source .phps

Notice that php5-script is omitted here. On modern setups or when using CGI wrappers, explicit handlers like php5-script are often redundant if application/x-httpd-php is set up correctly. The key is to ensure that the MIME type application/x-httpd-php is associated with .php extensions and that no conflicting AddType directives (like text/html) override this association in a way that prevents execution.

If you added AddType text/html .php, it likely caused Apache to treat PHP files as static HTML, which disables the handler logic required for parsing scripts. This explains why adding that line resulted in no change or continued issues. The server needs to know that .php files are executable scripts via the application/x-httpd-php type.

Finally, verify that your configuration changes were applied by restarting Apache:

service httpd restart

Check for errors in /var/log/httpd/error_log. If PHP still downloads instead of executing, ensure that there isn't a global default handler overriding your specific settings or that the DirectoryIndex directive isn't interfering with direct file access. In some cases, placing files outside the document root or having conflicting .htaccess rules can also lead to these behaviors.

For production environments where stability is critical, consider upgrading from CentOS 6 and Apache 2.2 to newer stacks like RHEL/CentOS 7/8 with Apache 2.4 and PHP 7.x or 8.x. The older stack has known security vulnerabilities and lacks modern extensions required for frameworks like Laravel. If you are moving to a framework-based application, resources from https://laravelcompany.com can provide guidance on setting up the appropriate server configuration for modern PHP versions.

Our network