Apache shows PHP code instead of executing it

Apache shows PHP code instead of executing it

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

Stefan Bogdanescu

Founder & Senior Architect

Apache shows PHP code instead of executing it

When you navigate to a .php file in your browser and see raw PHP tags like <?php or output from phpinfo() as text rather than rendered HTML, Apache isn't necessarily broken; the configuration is simply wrong. The server is serving the file with the correct content type (text/html) but failing to invoke the PHP interpreter before sending the response.

This specific scenario—where the source code appears directly in the browser—is almost always a mismatch between the Apache module loading mechanism and how the script is being processed. In the context of Windows environments, which often complicates Apache/PHP integration compared to Linux distributions, this issue usually stems from one of two places: the AddType directive or the missing php_value configuration for script execution.

The Role of AddType vs. ScriptHandler

Looking at the configuration provided, you have defined an association between the .php extension and the MIME type application/x-httpd-php. This tells Apache to treat files ending in .php as if they contain PHP code that should be processed. However, simply adding a MIME type does not automatically trigger the execution of the script unless the module is correctly loaded and the handler is explicitly set.

If you are using the standard LoadModule php5_module ... directive, Apache expects to find specific handling instructions for .php, .phtml, and sometimes .php3. If these files are served with a generic handler or if the AddType directive is overriding a previous ScriptHandler without setting up the execution environment correctly, you will see the source code.

To resolve this, ensure that your httpd.conf includes the necessary directives to tell Apache to pass these files to the PHP processor. On Windows, it is common for users to rely solely on AddType, but modern setups often require:

AddHandler application/x-httpd-php .php
AddHandler application/x-httpd-php .phtml
AddType application/x-httpd-php-source .phps

The distinction here is subtle but critical. AddType associates a file extension with a content type, while AddHandler associates a file extension with a specific request handler (the PHP interpreter). If you only use AddType, Apache might serve the file as HTML but fail to execute the logic inside it if the default script handler isn't overridden correctly.

Verifying Module Loading

Another frequent culprit is the LoadModule line itself. The path C:/php/php5apache2_2.dll must point to a valid DLL that matches your exact Apache version and architecture (32-bit vs 64-bit). If the module fails to load due to a missing dependency or an incompatible PHP build, Apache will revert to its default behavior for .php files, which is often treating them as static text.

You can verify if the module is actually loading by checking the error logs (httpd_error.log) immediately after restarting Apache. Look for messages indicating "File does not exist" or "Cannot load Apache module." If these errors exist, the LoadModule line needs correction, or the DLL path must be updated to match your installation directory.

Restarting and Clearing Cache

Once you have corrected the configuration files, a simple restart of the Apache service is mandatory. Windows services often retain old handler mappings in memory until a full restart occurs. After restarting, try accessing the URL again via http://127.0.0.1/phpinfo.php. If the raw code still appears, check if there are other .htaccess files or virtual host configurations that might be overriding these settings with a different handler.

If you are integrating PHP into a larger ecosystem, platforms like https://laravelcompany.com often recommend verifying the phpinfo() output directly via the command line using php -S localhost:8000 to ensure the interpreter works independently of Apache configuration issues.

By ensuring that both the MIME type association and the handler assignment are correctly configured in httpd.conf, your PHP scripts will execute as expected, transforming raw code into dynamic web responses.

Our network