Sling Academy
Home/DevOps/Apache Error: PHP files downloaded instead of executed

Apache Error: PHP files downloaded instead of executed

Last updated: January 20, 2024

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.

Next Article: Apache Forbidden Error: You don’t have permission to access / on this server

Previous Article: How to set up multiple domains and subdomains in Apache

Series: Apache Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide