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?

| |
  • 1
    1. The problem is [...] when on localhost. Does that mean it works from elsewhere? 2. [...] I can't seem to get php to execute [...] What exactly happens? – Dennis May 16 '12 at 12:12
  • 1. Yeah it works when I have that very folder structure on the server. But if I copy the website to my local computer and put it into the localhost... no php. 2. What happends is that the php code simply gets printed directly instead of being interpreted. – Fzs2 May 16 '12 at 12:14
  • Try copying the files /etc/apache2/mods-available/php5.* from your server to your home computer and restart apache2. Just to be sure, make backup copies first. – Dennis May 16 '12 at 12:32
  • It's a rented server space and sadly I don't have access to that folder. – Fzs2 May 16 '12 at 12:36
  • 1
    let us continue this discussion in chat – Dennis May 16 '12 at 12:45

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

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
| |

is apache running?

when open localhost, you must see "It works!" page. if apache isn't running, you can use /etc/init.d/apache2 start

Maybe php module is not enabled. To enable

a2enmod php5


/etc/init.d/apache2 reload

You can use

tail -f /var/log/apache2/error.log

to see the error logs.

| |
  • apache is running and the module is enabled. But when i run that tail command i get "[Wed May 16 13:24:11 2012] [error] [client] File does not exist: /var/www/favicon.ico [Wed May 16 13:24:30 2012] [notice] caught SIGTERM, shutting down [Wed May 16 13:25:25 2012] [notice] Apache/2.2.17 (Ubuntu) PHP/5.3.5-1ubuntu7.8 with Suhosin-Patch configured -- resuming normal operations [Wed May 16 13:25:57 2012] [error] [client] File does not exist: /var/www/favicon.ico " – Fzs2 May 16 '12 at 11:54
  • the favicon error is weird because that picture file is definitely there. – Fzs2 May 16 '12 at 11:56
  • do you check permissions? – kelebek May 16 '12 at 19:31
  • the problem is solved so i don't really care. but thanks. – Fzs2 May 16 '12 at 19:45

You must install php module.

apt-get install libapache2-mod-php5
| |
  • it's already installed and at the newest version. – Fzs2 May 16 '12 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.