browser downloads php file from apache web server

亡梦爱人 提交于 2019-12-19 17:40:16

问题


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.


回答1:


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




回答2:


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.

:)




回答3:


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


来源:https://stackoverflow.com/questions/5591650/browser-downloads-php-file-from-apache-web-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!