PHP Files won't open in browser - only download. What do I need to change to make it work properly?

血红的双手。 提交于 2021-02-07 18:16:21

问题


I have tried reinstalling PHP. PHP is working, and Apache2 is running. I don't know why it's not opening in a browser and displaying normally.

Just so you know - my httpd.conf is empty - and instead I have everything in apache2.conf. This is because I'm using Ubuntu.

Can you help me? I know it's something simple, but I can't seem to find the answer.


回答1:


Have you virtual host on this project?

Are you open php file with http://localhost/file.php or directly like file://...../file.php ?




回答2:


In my case there was a modification of the /etc/apache2/mods-enabled/php5.conf by module userdir

<IfModule mod_php5.c>
    <FilesMatch "\.ph(p3?|tml)$">
    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>

The solution is inside the config file, just comment the lines from <IfModule mod_userdir> to </IfModule>.




回答3:


In your apache conf:

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

Don't forget to restart apache afterwards. Let me know how it goes.




回答4:


Try

sudo a2enmod php5

in terminal. Assuming you are using php 5 :)




回答5:


If you are using php5 the complete solution would be

sudo apt-get install libapache2-mod-php5

Then

sudo a2enmod php5



回答6:


I recently had this problem, but only when accessing my site's root (e.g. http://example.com) - it worked as expected when accessing index.php explicitly (e.g. http://example.com/index.php).

The issue was that before creating index.php, I had an index.html file in the web root, but then I renamed it to index.html.bak to 'move it out of the way'. Unfortunately, that doesn't move it out of the way. For whatever reason, Apache will serve that file up with a MIME type of application/x-trash.
(As the /etc/mime.types file contains a line reading "application/x-trash ~ % bak old sik")

Maybe that will help someone else who is scratching their head as I was.




回答7:


Also, when moving a site and you encounter this, make sure your .htaccess file doesn't specify a different method handler for php. We ran into this moving a wordpress site for a client.




回答8:


Had same problem - and it was in the .htaccess file I had accidentally downloaded from the live server. Once you've deleted the .htaccess file, you will need to clear your cache to be able to load the .php file in your browser via http://



来源:https://stackoverflow.com/questions/5397469/php-files-wont-open-in-browser-only-download-what-do-i-need-to-change-to-mak

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