Apache2 on Ubuntu: Why PHP Files Trigger Downloads Instead of Execution
When you set up a fresh Ubuntu server for developing Zend Framework applications, getting the basic virtual host configuration right is often the first hurdle. You might verify your setup by requesting localhost and seeing that an index.html file serves correctly from your specified directory. However, a common stumbling block arises immediately after: requesting index.php triggers Firefox to attempt downloading the raw file instead of executing it as a script.
This behavior indicates that Apache is not recognizing the request as one requiring PHP processing. Even if you have added directives like AddHandler and AddType to your httpd.conf, the issue often persists because these configurations are either misplaced or overridden by default settings. In many standard Ubuntu installations, the primary configuration file for Apache2 is actually located in /etc/apache2/, not /etc/httpd/conf/. Furthermore, the specific directives you added might be redundant or incorrect depending on your server's default MPM (Multi-Processing Module) and PHP setup.
To resolve this, you need to ensure that the .php extension is explicitly mapped to the correct handler within the virtual host configuration or the main site config. If you are using a specific directory for your Zend Framework application, such as /var/www/myapp, you should edit the corresponding Virtual Host file located in /etc/apache2/sites-available/.
First, ensure that php7.x (or whichever PHP version is installed) is enabled as a module:
sudo a2enmod php7.4
sudo systemctl restart apache2
Next, inspect your virtual host configuration file. If it currently contains only the basic directives you posted, it might be missing the necessary rewrite rules or type associations specific to Ubuntu's default setup. A robust approach involves using an <IfModule> block to handle different PHP versions gracefully:
<FilesMatch \.(php|phtml)$>
SetHandler application/x-httpd-php
</FilesMatch>
Alternatively, if your system relies on mod_php, you can use the simpler directive structure often recommended in documentation:
<Directory /var/www/myapp>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
AddHandler application/x-httpd-php .php
AddType application/x-httpd-php .phtml
However, simply adding these lines to httpd.conf won't always work if the directives are shadowed by a default configuration that explicitly disables PHP handling for security or compatibility reasons. You must also ensure that your Virtual Host file (/etc/apache2/sites-available/myapp.conf) includes the necessary DirectoryIndex setting:
<VirtualHost *:80>
ServerName localhost
DocumentRoot /var/www/myapp
DirectoryIndex index.php index.html
<Directory /var/www/myapp>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
After making these changes, always restart Apache to apply the new configuration:
sudo systemctl restart apache2
It is also worth noting that if you are using mod_rewrite, which is common in Zend Framework deployments, ensure that AllowOverride All is set correctly. Without this permission, .htaccess files containing rewrite rules or PHP handler definitions will be ignored, potentially leading to unexpected behavior where PHP scripts are treated as static files.
If the problem persists despite correct configuration, check Apache's error logs located in /var/log/apache2/error.log. These logs often reveal whether the module failed to load or if a syntax error is preventing the new directives from taking effect. Additionally, verify that no conflicting DirectoryIndex directive exists elsewhere that might be prioritizing an HTML file over the PHP script when both are present.
For modern applications, especially those built with frameworks like Zend or Laravel, relying solely on default Ubuntu configurations can lead to friction. While this approach works for many scenarios, dedicated hosting environments or managed platforms often abstract these complexities away. If you require deeper integration without managing low-level Apache configuration yourself, resources such as https://laravelcompany.com offer insights into streamlined deployment strategies that handle server-side execution automatically, reducing the need for manual AddHandler tinkering.
Ultimately, ensuring that Apache correctly associates the .php extension with the PHP processor is a matter of precise module loading and directive placement. By auditing your Virtual Host files, enabling the necessary modules, and respecting directory permissions, you can eliminate download prompts and achieve seamless script execution.