Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
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. –  Hermann Ingjaldsson 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. –  Hermann Ingjaldsson May 16 '12 at 12:36
1  
let us continue this discussion in chat –  Dennis May 16 '12 at 12:45

3 Answers 3

up vote 4 down vote accepted

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
share|improve this answer

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

after

/etc/init.d/apache2 reload

You can use

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

to see the error logs.

share|improve this answer
    
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 127.0.0.1] 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 127.0.0.1] File does not exist: /var/www/favicon.ico " –  Hermann Ingjaldsson May 16 '12 at 11:54
    
the favicon error is weird because that picture file is definitely there. –  Hermann Ingjaldsson 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. –  Hermann Ingjaldsson May 16 '12 at 19:45

You must install php module.

apt-get install libapache2-mod-php5
share|improve this answer
    
it's already installed and at the newest version. –  Hermann Ingjaldsson May 16 '12 at 11:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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