PHP Is Not Working on Ubuntu Apache Server
When a simple index.php renders raw HTML or comments your PHP tags instead of executing them, it usually isn't a mystery about the PHP interpreter itself. The CLI version is running fineâyour php -v output confirms PHP 7.0.15 is installed and functional. The issue lies in Apache's configuration: it simply doesn't know how to hand off .php requests to the PHP engine.
On Ubuntu systems, this manifests as the server treating the file as a static text document. You see <code>Welcome!</code> because that HTML exists, but the <?php echo "Hello World!"; ?> block remains untouched in the browser source. This happens when Apache lacks the necessary MIME type mapping or the specific handler module to process PHP scripts.
Enabling the PHP Module
Apache on Ubuntu relies on modules being explicitly enabled. Even if you installed PHP, the bridge between Apache and PHP might not be active. The standard approach involves enabling the libapache2-mod-php package.
Run the following command to ensure the module is loaded:
sudo a2enmod php7.0
Note: On Ubuntu 17.04, PHP 7.0 is the default version provided by the repositories. If you installed a different version manually, adjust the module name accordingly (e.g., php8.1).
After enabling the module, Apache must be restarted to apply changes:
sudo systemctl restart apache2
If your system uses the legacy init script mentioned in your logs (/etc/init.d/apache2), use that instead:
sudo service apache2 restart
Verifying MIME Types and Handlers
Enabling the module is often not enough. You must verify that Apache recognizes .php files as executable scripts rather than static text. This is controlled by the mod_mime package and the configuration in /etc/apache2/mods-enabled/.
Check if the mime module is enabled:
sudo a2enmod mime
Once enabled, restart Apache again. The mod_mime package adds the necessary mapping that tells the server to send the application/x-httpd-php header for files with a .php extension. Without this, Apache ignores the handler directives and serves the content as-is.
You can also inspect the configuration file directly:
cat /etc/apache2/mods-enabled/php7.conf
This file typically contains lines defining the AddHandler, AddType, and LoadModule directives. Ensure that AddHandler application/x-httpd-php .php is present. If you manually installed PHP without using the apt package manager, you might need to add this line yourself or create a symbolic link from your custom config directory to /etc/apache2/sites-enabled/000-default.conf.
Checking for Syntax Errors in VirtualHosts
Sometimes, configuration errors in your virtual host files can break execution silently. Open your default site configuration:
sudo nano /etc/apache2/sites-available/000-default.conf
Ensure that the Directory block serving your public web root includes the necessary settings for PHP processing. While modern Apache configurations often inherit these from global settings, it is best practice to explicitly allow PHP execution in the directory hosting your site:
<Directory /var/www/html>
AllowOverride All
Require all granted
</Directory>
Avoid setting Options -Indexes or restrictive permissions that might inadvertently block access, though those rarely cause raw code rendering. The most common pitfall here is forgetting to reload Apache after making changes to these config files:
sudo systemctl reload apache2
Conclusion
The symptom of seeing PHP code commented out in the browser source is a definitive sign that the server is not invoking the interpreter. By ensuring libapache2-mod-php is installed, enabling the mime module for correct file type mapping, and verifying your virtual host configurations, you restore the dynamic execution capabilities of your Ubuntu Apache setup. Once these links are established, your index.php will output the "Hello World!" string as expected.