Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

browser downloads php file from apache web server

Tags:

php

apache2

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.

like image 909
Eugenie Lee Avatar asked Apr 08 '11 07:04

Eugenie Lee


People also ask

Why are my PHP pages downloading?

This is normally due to an improper handler code. In the . htaccess file, you will want to ensure the handler code matches your version of php. If it does not, the php files may try to download instead of process.

Where does Apache save PHP files?

A developer will need to create a folder named upload in the Apache server's htdocs directory to support the PHP file upload component. This folder is where the HTML5 file uploads will be saved.


2 Answers

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

like image 93
RageZ Avatar answered Nov 13 '22 09:11

RageZ


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.

:)

like image 26
pmilb21 Avatar answered Nov 13 '22 09:11

pmilb21