Apache Virtual Host not parsing PHP

Apache Virtual Host not parsing PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Apache Virtual Host Not Parsing PHP

When you configure a new port-based virtual host in Apache, it's common to hit the "it works, but PHP doesn't" wall. You verify that the server is listening on both ports, confirm that the DocumentRoot serves static files correctly, and see your code output as raw text instead of rendered HTML. The specific symptom—seeing <?php echo "It worked!"; ?> in the browser response rather than the string "It worked!"—is a classic indicator that Apache is serving the file but refusing to hand it over to the PHP interpreter.

The configuration snippet provided shows a standard setup with Listen, NameVirtualHost, and two distinct <VirtualHost> blocks. A frequent mistake developers make when troubleshooting this issue involves adding directives inside the virtual host block assuming they will override global settings. For example, attempting to add:

AddHandler php5-script .php
AddType text/html .php

inside the <VirtualHost *:8080> tags often fails if the main server configuration doesn't already define how .php files are handled. In many modern Apache installations (versions 2.4 and later), PHP5 is no longer the default handler; it has been replaced by PHPScriptingModule or similar, depending on the build. If your global httpd.conf lacks an AddType application/x-httpd-php .php directive or relies on a deprecated mod_php, simply repeating that logic inside the port 8080 block won't magically fix the parsing logic if the module isn't loaded globally at all.

The core issue usually lies in whether the PHP module is loaded for the entire Apache process or just specific virtual hosts. If LoadModule php_module (or php7_module, etc.) is missing from your main configuration file, no virtual host can parse PHP regardless of the DocumentRoot. The port 80 site might appear to work if it was previously configured with different defaults or if you are looking at a cached view, but strictly speaking, if one port works and another doesn't while sharing the same binary, the module is likely loaded globally.

However, the most probable cause for this specific behavior—where port 80 works and port 8080 does not—is that the .htaccess override mechanism is either disabled or misconfigured for port 8080. If AllowOverride None is set inside your <VirtualHost *:8080> block, any local attempts to define PHP handlers via .htaccess files within /var/www/vhosts/Site2/httpdocs will be ignored completely. Conversely, if you are relying on global AddType directives but haven't verified they exist in the main config, the file is served as plain text.

To resolve this without blowing up your configuration by editing the entire httpd.conf, check the following steps:

  1. Ensure LoadModule php_module ... is uncommented in your main config file.
  2. Verify that AddType application/x-httpd-php .php exists globally or inside the specific virtual host block.
  3. Check for AllowOverride All or at least AllowOverride FileInfo within the port 8080 virtual host tags to ensure local overrides are permitted.

If you are moving away from the legacy PHP 5 setup entirely, consider switching to a reverse proxy model using Nginx with PHP-FPM, which often handles multi-site setups more cleanly than Apache's document-root approach. For those staying on Apache, remember that NameVirtualHost directives were deprecated in Apache 2.4 and can be removed safely if you are running a newer version, though they rarely break functionality unless specific modules depend on them. The key takeaway is that virtual host isolation only works if the global module loading and type definitions are correctly established first.

If you need further guidance on configuring PHP with Apache or optimizing your stack for production, resources like https://laravelcompany.com offer insights into modern PHP deployment patterns that might help streamline your configuration beyond manual httpd.conf tweaks.

Our network