PHP 文件不会在浏览器中打开 - 只能下载。我需要更改什么才能使其正常工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5397469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Files won't open in browser - only download. What do I need to change to make it work properly?
提问by Itamar
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.
我试过重新安装 PHP。PHP 正在运行,Apache2 正在运行。我不知道为什么它不能在浏览器中打开并正常显示。
Just so you know - my httpd.conf is empty - and instead I have everything in apache2.conf. This is because I'm using Ubuntu.
只是让您知道 - 我的 httpd.conf 是空的 - 而我在 apache2.conf 中拥有所有内容。这是因为我使用的是 Ubuntu。
Can you help me? I know it's something simple, but I can't seem to find the answer.
你能帮助我吗?我知道这很简单,但我似乎无法找到答案。
回答by Faraona
Have you virtual host on this project?
你有这个项目的虚拟主机吗?
Are you open php file with http://localhost/file.phpor directly like file://...../file.php ?
你是用http://localhost/file.php还是直接像 file://...../file.php 一样打开 php 文件?
回答by ChrisLo
In my case there was a modification of the /etc/apache2/mods-enabled/php5.conf by module userdir
在我的情况下,模块 userdir 修改了 /etc/apache2/mods-enabled/php5.conf
<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>
.
解决方案在配置文件中,只需注释<IfModule mod_userdir>
to 中的行</IfModule>
。
回答by Michael McTiernan
In your apache conf:
在你的 apache conf 中:
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
Don't forget to restart apache afterwards. Let me know how it goes.
之后不要忘记重新启动apache。让我知道事情的后续。
回答by Mutuma
If you are using php5 the complete solution would be
如果您使用的是 php5,则完整的解决方案是
sudo apt-get install libapache2-mod-php5
Then
然后
sudo a2enmod php5
回答by Ivan Ivani?
Try
尝试
sudo a2enmod php5
in terminal. Assuming you are using php 5 :)
在终端。假设您使用的是 php 5 :)
回答by DavidJ
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
).
我最近遇到了这个问题,但只有在访问我网站的根目录时(例如http://example.com
) - 它在显式访问 index.php 时(例如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
")
问题是,在创建之前index.php
,我index.html
在 Web 根目录中有一个文件,但后来我将其重命名index.html.bak
为“将其移开”。不幸的是,这并没有把它移开。无论出于何种原因,Apache 都会为该文件提供 MIME 类型的application/x-trash
.
(因为/etc/mime.types
文件包含一行阅读“ application/x-trash ~ % bak old sik
”)
Maybe that will help someone else who is scratching their head as I was.
也许这会帮助那些像我一样挠头的人。
回答by lthorne
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.
此外,在移动站点时遇到这种情况,请确保您的 .htaccess 文件没有为 php 指定不同的方法处理程序。我们遇到了为客户移动 wordpress 站点的情况。
回答by SugarHill Works LLC
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://
有同样的问题 - 它在我不小心从实时服务器下载的 .htaccess 文件中。删除 .htaccess 文件后,您需要清除缓存才能通过 http:// 在浏览器中加载 .php 文件