PHP – Fix PHP Code Returned Instead of Executed

apache-http-serverlocalhostPHP

I have L-Ubuntu 11.04. I've installed Apache2 and I'm trying to simulate a server on my local file system by navigating to localhost in the browser.

The problem is that I can't seem to get php to execute when on localhost. The PHP code is simply printed in the browser (instead of being executed and the result being printed).

  • libapache2-mod-php5 is installed and the latest version.
  • The Apache module php5 is enabled.

How can I get PHP to run on localhost?

Best Answer

The configuration file /etc/apache2/mods-available/php5.conf controls which files Apache recognizes as php scripts (based on their extensions).

Be default (in PHP 5.3.2), the file contains the following code:

<FilesMatch "\.ph(p3?|tml)$">
    SetHandler application/x-httpd-php
</FilesMatch>

This affects files with the following extensions:

  • .php
  • .php3
  • .phtml

From our discussion in chat, I know that your files have a html extension. The server was configured to treat .html files as php files, but your home computer is not. That leaves you with two options:

  1. Rename your .html files that contain php code to one of the above extensions.

  2. Replace the line

    <FilesMatch "\.ph(p3?|tml)$">
    

    in your php5.conf by

    <FilesMatch "\.(ph(p3?|tml)|html?)$">
    

    and reload apache by executing the following command:

    sudo service apache2 reload
    

    In addition to the previously mentioned extensions, the new configuration also affects:

    • .htm
    • .html
Related Question