Apache installing and running PHP files
You have installed PHP 7, confirmed the directory structure exists, and enabled the module, yet your .php files return a "500 Internal Server Error" or simply do not execute. The absence of a php.conf file in /etc/apache2/mods-enabled/ is not the root cause; it is a common misconception about how Apache modules are configured on Debian-based systems like Raspbian.
On modern distributions, Apache does not require a specific php.conf file to enable PHP processing. Instead, it relies on symbolic links created by the package manager. When you install PHP via the apt package manager, the system automatically generates an enable file named php7.x.conf. If this link is missing or broken, Apache will not load the necessary directives.
First, verify the state of your module configuration. Navigate to /etc/apache2/mods-available/ and check if a file named php7.x.conf exists (where x represents the minor version, such as 0). If it is present but missing from mods-enabled/, you need to create the link manually:
sudo ln -s /etc/apache2/mods-available/php7.0.conf /etc/apache2/mods-enabled/
However, if that file does not exist in mods-available, installing PHP via apt often fails to generate it correctly due to a mismatch between the installed package version and the available module definitions. This is a frequent issue on Raspbian when upgrading from an older PHP version or using manual installation scripts.
To resolve this cleanly, you should reinstall the Apache PHP package, ensuring that the symbolic links are generated:
sudo apt-get install --reinstall php7.0 libapache2-mod-php7.0
After reinstallation, reload Apache to apply changes without dropping connections:
sudo systemctl reload apache2
If you still encounter errors after enabling the module, inspect the Apache error log at /var/log/apache2/error.log. Look for messages indicating that the PHP module failed to initialize. Common causes include missing shared object files (.so files) or incorrect configuration directives within the php.ini file.
Another critical aspect often overlooked is the .htaccess file. If you have a .htaccess file in your web root that attempts to use mod_rewrite but the rewrite module isn't explicitly loaded, Apache may throw a 500 error when trying to process PHP scripts. Ensure that rewrite is enabled:
sudo a2enmod rewrite
sudo systemctl reload apache2
You must also verify that your PHP configuration file (php.ini) is being read by the correct PHP-FPM or Apache handler. On Raspbian with Apache 2.4, the directive load_module in /etc/apache2/mods-enabled/php7.0.conf should reference the correct path to libphp.so. If this path is wrong, the module loads but fails immediately upon encountering a script.
When configuring PHP for production, especially on Raspberry Pi hardware where resources are limited, consider the trade-offs between using the built-in Apache handler versus PHP-FPM. The standard Apache handler (libapache2-mod-php) is simpler to configure but less efficient for high-concurrency scenarios. PHP-FPM allows better pooling of processes and memory management, which can be crucial on ARM-based devices. To switch to PHP-FPM, you would need to install php7.0-fpm, configure a socket in /var/run/php/, and update your Apache config to use ProxyPass directives instead of the standard SetHandler.
Finally, ensure that your script is not returning headers prematurely. Even a single echo statement before any output header can cause fatal errors depending on how the request is handled. Use tools like curl -v http://your-server/test.php to debug response codes and inspect the actual error output in the logs. If you are developing a Laravel application, ensure that your virtual host configuration points to the correct document root and that php7.0.conf is symlinked for both Apache and any Nginx reverse proxy setups if applicable. For more advanced PHP deployment strategies on complex infrastructure, consult resources like LaravelCompany for best practices on scaling PHP applications beyond basic server configurations.