How To Fix Apache Downloading PHP Files Instead Of Executing Them?

Published September 16, 2024

Problem: Apache Downloading PHP Files

When Apache server doesn't run PHP files and offers them for download instead, it stops PHP-based websites from working correctly. This problem usually happens because of wrong settings in Apache or incorrect file permissions.

Common Causes of PHP Execution Issues

Incorrect Apache Configuration

When Apache doesn't run PHP files correctly, it's often due to configuration issues. One problem is missing or incorrect PHP module loading. The PHP module must be loaded in Apache to process PHP files. If this module is not loaded or is loaded incorrectly, Apache won't handle PHP files.

Another issue is improper file type associations. Apache needs to know which file types to treat as PHP files. If these associations are not set up correctly, Apache may not recognize PHP files and will instead offer them for download or display the source code.

Tip: Check Apache Configuration

To verify if Apache is configured correctly for PHP, look for the following line in your Apache configuration file (usually httpd.conf or apache2.conf):

LoadModule php_module modules/libphp.so

Make sure this line is not commented out (no # at the start) and the path is correct for your system.

PHP Installation Problems

Sometimes, the issue is with the PHP installation itself. An incomplete or corrupted PHP installation can prevent Apache from executing PHP files correctly. This can happen if the installation process was interrupted or if some components were not installed properly.

Version compatibility issues can also cause problems. Different PHP versions may need different Apache configurations or modules. If the installed PHP version doesn't match the Apache version or its settings, it can lead to execution problems.

Solutions to Fix PHP Execution in Apache

Verify PHP Module Loading

To fix PHP execution issues in Apache, check the Apache configuration files. Look for the PHP module loading directive in the main Apache configuration file or a separate PHP configuration file. Make sure this line is present and not commented out:

LoadModule php_module modules/libphp.so

The path may vary depending on your system. If this line is missing or commented out, add or uncomment it to enable PHP module loading.

Tip: Check PHP Module Path

If you're unsure about the correct path for the PHP module, you can use the following command to find it:

apache2ctl -M | grep php

This command lists all loaded Apache modules and filters for PHP-related ones, helping you identify the correct module name and path.

Configure File Type Associations

Set up the file type associations for PHP files. Add these directives to your Apache configuration:

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

These lines tell Apache to treat .php files as PHP scripts and process them.

Restart Apache Server

After changing the Apache configuration, restart the Apache server to apply the new settings. This step clears cached configurations and ensures Apache uses the updated settings. Use the command for your system to restart Apache:

sudo systemctl restart apache2

or

sudo service apache2 restart

Check PHP Installation

If the problem continues, check your PHP installation. Verify that the installed PHP version works with your Apache version. Check the PHP version by running:

php -v

If you think there are issues with the PHP installation, consider reinstalling PHP. Remove the old installation before installing the new one. Use your system's package manager to reinstall:

sudo apt-get remove php*
sudo apt-get install php

These steps should solve most issues related to PHP execution in Apache. If problems persist, review your server logs for more detailed error messages that might point to other issues.