6

I'm running a Ubuntu Server on a VM, to test out different web forum solutions. I have set up a ~/public_html/ to be accessible with the apache2 web server, and that works fine.

However when I go to a .php file on a browser (using my VM's ip-address/~username/phpfile.php) it does not display it as it should. Instead it offers to save to file/asks what program to open it with. Interestingly though that dialog box does recognise that it is a php file.


I have the following version of php installed on the system:

PHP 5.3.2-1ubuntu4.5 with Suhosin-Patch (cli) (built: Sep 17 2010 13:49:46)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

And the following server:

Server version: Apache/2.2.14 (Ubuntu)
Server built:   Nov 18 2010 21:19:09


If anyone knows what might be causing this/potential solutions it would make me very happy :)

EDIT: Turns out files this behaviour was only apparent on files in the ~/public_html/ directory. All php files in /var/www/ work fine.

Prizes go to whoever can explain why? :D (And by prizes I just mean a well done, no actual prizes I'm afraid.)

6
  • What's in your PHP file? Also, do you have PHP configured to work with the server? Dec 22, 2010 at 15:36
  • sounds like php is not configured with apache2. maybe do a apache2 restart.
    – kjy112
    Dec 22, 2010 at 15:37
  • 1
    Does the downloaded file contain the original PHP source? If so, it's not executing. If it contains the HTML that you would expect to be displayed, it'll be something like the Content-Type or Content-Disposition that's incorrect.
    – Joe
    Dec 22, 2010 at 15:42
  • @Stephen I may see what they say as well.
    – Jonathan
    Dec 22, 2010 at 15:57
  • @Joe I does contain the original PHP source yes.
    – Jonathan
    Dec 22, 2010 at 15:57

7 Answers 7

6

You might have only have the cli version of PHP installed currently try

sudo apt-get install php5

and restart apache

sudo apache2ctl restart.

You can also check the PHP module is loaded into apache by doing

sudo apache2ctl -M

Which if the module is loaded then it should show something like

Loaded Modules:
 ...
 php5_module (shared)
 ...
Syntax OK
5

add this to your apache config

AddType application/x-httpd-php .php 
1
  • 2
    Wow.. this worked for me. I am using Zend Apache server (CGI/FastCGI) and i frequently encountered with this error, until i made the change you mentioned, in httpd.conf file. Thanks a lot. Nov 12, 2013 at 13:50
2

You need to configure apache so that it will start the php interpreter when a .php file is requested instead of just returning the file's contents.

This needs to be done in httpd.conf for the directory in question. It would look something like this:

# Use for PHP 5.x:
LoadModule php5_module        modules/libphp5.so
AddHandler php5-script php 

# Add index.php to your DirectoryIndex line:
DirectoryIndex index.html index.php

AddType text/html       php
2

I had the same problem actually in the user's public_html directory and I found that in the /etc/apache2/mods-available/php5.conf file there are some comment lines included and here they are:

# To re-enable php in user directories comment the following lines
# (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
# prevents .htaccess files from disabling it.

I did what it says and restarted apache and the problem had gone.

1

Check .htaccess for AddType row. I had a custom AddType which caused bad content-type. Firefox helped me debug the issue, but you can also use wget.

Firefox Wget

0

Easy to forget, but a common way this happens is forgetting the opening and closing <?php ?> tags.

0

I've recently got this problem and after eliminating all other causes in this thread, I found that mod_php5 extension (in my system at /usr/lib64/apache2/mod_php5.so ) was without executing pemitions.

After chmoding it it worked.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .