8

I have an apache web server. Let's say this server's domain is example.com.

When I access example.com, then the index.php file is correctly displayed in the browser.

However, when I access e.g. example.com/~user, then the index.php file of /home/user/public_html/index.php file is downloaded rather than displayed.

How do I fix this problem? I changed "expose_php = Off" in php.ini, but nothing has changed.

icc97
  • 11,395
  • 8
  • 76
  • 90
Eugenie Lee
  • 81
  • 1
  • 1
  • 2
  • 2
    What linux distribution you are using? By default debian/ubuntu disable the php parsing in the users public directories. – RageZ Apr 08 '11 at 07:09

3 Answers3

12

If you are on Debian/Ubuntu take a look at this file /etc/apache2/mods-available/php5.conf

mine looks like this and you can see I had to comment some line to get PHP working in the user dir

<IfModule mod_php5.c>
    <FilesMatch "\.ph(p3?|tml)$">
        SetHandler application/x-httpd-php
    </FilesMatch>
    <FilesMatch "\.php$">
        SetHandler application/x-httpd-php
    </FilesMatch>
    <FilesMatch "\.phps$">
        SetHandler application/x-httpd-php-source
    </FilesMatch>
    # 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.
    #<IfModule mod_userdir.c>
    #    <Directory /home/*/public_html>
    #        php_admin_value engine Off
    #    </Directory>
    #</IfModule>
</IfModule>

Please note that after editing the file you would have to restart apache for the modifications to take effect, the command to restart apache on a debian based system is: /etc/init.d/apache2 restart

RageZ
  • 26,800
  • 12
  • 67
  • 76
5

Hope this saves someone else the headache. I know this question is old, but it still comes up when searching for this problem.

I'm not sure if this works across all installations of apache2, but I am running apache2 on ubuntu and had the problem of my web browser downloading files instead of displaying the correct index file.

The problem lies in the file /etc/apache2/mods-enabled/dir.conf The default document setting here was overriding what I had set in /etc/apache2/httpd.conf

So just open up /etc/apache2/mods-enabled/dir.conf and change the order of the files listed.

:)

pmilb21
  • 91
  • 1
  • 3
0

I've had a similar experience - some php files working OK, but others seem to have the raw php code downloaded.

In my case, it was due to the broken files using the short tag format of <? and ?>. This is not recommended, and you may find the default php.ini has this support forced off. With support off, the php code is sent down to the browser as if it was HTML.

If you can't avoid short tags (as in my case - a whole legacy codebase using short tags), then you can set it to be allowed in php.ini:

short_open_tag = On
tardate
  • 16,424
  • 15
  • 50
  • 50