Apache 正在下载 php 文件而不是显示它们

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18422140/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:32:27  来源:igfitidea点击:

Apache is downloading php files instead of displaying them

phpapachecentos

提问by Anthony Gainor

OS and server information:

操作系统和服务器信息:

  • CentOS 6.4 (Final)
  • Apache 2.2.15
  • PHP 5.5.1
  • CentOS 6.4(最终版)
  • 阿帕奇 2.2.15
  • PHP 5.5.1

I previously had php 5.3.x installed but decided to upgrade. I first uninstalled the php 5.3.x and then installed php 5.5.1 but after the installation completed apache did not parse the php files it just downloaded them. I have checked similar questions here in stackoverflow but none of them have helped me so far.

我以前安装了 php 5.3.x,但决定升级。我首先卸载了 php 5.3.x,然后安装了 php 5.5.1,但是在安装完成后,apache 没有解析它刚刚下载的 php 文件。我已经在 stackoverflow 中检查过类似的问题,但到目前为止没有一个对我有帮助。

For the record I have the following lines in my httpd.conf and php.conf that should make php work but don't:

作为记录,我在 httpd.conf 和 php.conf 中有以下几行应该使 php 工作但不工作:

AddHandler application/x-httpd-php .php5 .php4 .php .php3 .php2 .phtml
AddType application/x-httpd-php .php5 .php4 .php .php3 .php2 .phtml
AddType application/x-httpd-php-source .phps
AddHandler php5-script .php

I would really appreciate any help. Thank you.

我真的很感激任何帮助。谢谢你。

EDIT:

编辑:

I have these lines in the php.conf

我在 php.conf 中有这些行

<IfModule !worker.c>
  LoadModule php5_module modules/libphp5.so
</IfModule>
<IfModule worker.c>
  LoadModule php5_module modules/libphp5-zts.so
</IfModule>

EDIT:

编辑:

By removing the

通过删除

AddType application/x-httpd-php .php5 .php4 .php .php3 .php2 .phtml

apache no longer downloads the file. Now apache is showing the source code, but not all of it just part. I added

apache 不再下载该文件。现在 apache 正在显示源代码,但并非全部只是一部分。我加了

AddType text/html .php

but no luck.

但没有运气。

采纳答案by Mihkel

The correct AddType for php is application/x-httpd-php

php 的正确 AddType 是 application/x-httpd-php

AddType  application/x-httpd-php         .php
AddType  application/x-httpd-php-source  .phps

Also make sure your php module is loaded

还要确保您的 php 模块已加载

LoadModule php5_module        modules/mod_php55.so

When you're configuring apache then try to view the page from another browser - I've had days when chrome stubbornly caches the result and it keeps downloading the source code while in another browser it's just fine.

当您配置 apache 时,然后尝试从另一个浏览器查看页面 - 我有几天 chrome 顽固地缓存结果并不断下载源代码,而在另一个浏览器中它很好。

回答by markus

I came across this issue today and none of the solutions described worked for me. So here is another possible cause:

我今天遇到了这个问题,所描述的解决方案都不适合我。所以这是另一个可能的原因:

If you have something like

如果你有类似的东西

AddHandler x-mapp-php6 .php3 .php4 .php .phtml

in a .htaccessfile of your web content folder, it can cause your PHP scripts to stop working. In my case the server did not know the x-mapp-php6type, since that .htaccessfile was something I imported from a different web host when I transferred the website content.

.htaccessWeb 内容文件夹的文件中,它可能会导致您的 PHP 脚本停止工作。在我的情况下,服务器不知道x-mapp-php6类型,因为该.htaccess文件是我在传输网站内容时从其他网络主机导入的内容。

Just removing the AddHandlerline from the .htaccessfile solved it for me.

只需AddHandler.htaccess文件中删除该行即可为我解决。

回答by Ravi Shankar

After struggling a lot I finally solved the problem.

经过一番挣扎,我终于解决了这个问题。

If you are prompted to download a .phpfile instead of executing it, then here is the perfect solution: I assume that you have installed PHP5 already and still getting this error.

如果系统提示您下载.php文件而不是执行它,那么这里是完美的解决方案:我假设您已经安装了 PHP5 并且仍然收到此错误。

$ sudo su
$ a2enmod php5

This is it.

就是这个。

But If you are still getting the error :

但是如果您仍然收到错误:

Config file php5.conf not properly enabled: /etc/apache2/mods-enabled/php5.conf is a real file, not touching it

then do the following:

然后执行以下操作:

Turns out files shouldn't be stored in mods-enabled, but should rather be stored in mods-available. A symlink should then be created in mods-enabled pointing to the file stored in mods-available.

原来文件不mods-enabled应该存储在mods-available. 然后应该在 mods-enabled 中创建一个符号链接,指向存储在 mods-available 中的文件。

First remove the original:

首先删除原来的:

$ mv /etc/apache2/mods-enabled/php5.conf /etc/apache2/mods-available/

Then create the symbolic link:

然后创建符号链接:

$ ln -s /etc/apache2/mods-available/php5.conf /etc/apache2/mods-enabled/php5.conf

I hope your problem is solved.

我希望你的问题得到解决。

回答by Sam Azer

Please take a look at your addtype directives.

请查看您的 addtype 指令。

It looks to me like Apache is telling the browser that it's sending a document type of application/php for scripts with extensions like .php5. In fact Apache is supposed to tell the browser that the script is outputting text/html.

在我看来,Apache 正在告诉浏览器它正在为具有 .php5 等扩展名的脚本发送 application/php 的文档类型。事实上,Apache 应该告诉浏览器该脚本正在输出 text/html。

Please try this:

请试试这个:

AddType text/html .php

Regarding the suggestion above that you should tell the browser that you are outputting a PHP script: It seemed like an unusual idea to me. I googled it and found that there is quite a bit of discussion about it on the web. Apparently there are cases where you might want to say that you are sending a PHP script (even though Apache is supposed to execute the script and emit text/html,) and there are also cases where the browser simply doesn't recognize that specific Mime Type.

关于上面的建议,您应该告诉浏览器您正在输出一个 PHP 脚本:这对我来说似乎是一个不寻常的想法。我用谷歌搜索了一下,发现网上有很多关于它的讨论。显然,在某些情况下,您可能想说您正在发送一个 PHP 脚本(即使 Apache 应该执行该脚本并发出文本/html),并且在某些情况下,浏览器根本无法识别特定的 Mime类型。

Clearing your browser cache is always a good idea.

清除浏览器缓存始终是一个好主意。

In case it's helpful here's a copy of my /etc/httpd/conf.d/php.conf file from a server running CentOS 5.9:

如果有帮助,请从运行 CentOS 5.9 的服务器上复制我的 /etc/httpd/conf.d/php.conf 文件:

#        
# PHP is an HTML-embedded scripting language which attempts to make it                                             
# easy for developers to write dynamically generated webpages.                                                  
#
<IfModule prefork.c>
  LoadModule php5_module modules/libphp5.so
</IfModule>
<IfModule worker.c>
  LoadModule php5_module modules/libphp5-zts.so
</IfModule>

#
# Cause the PHP interpreter to handle files with a .php extension.
#
AddHandler php5-script .php
AddType text/html .php

#
# Add index.php to the list of files that will be served as directory
# indexes.
#
DirectoryIndex index.php

#
# Uncomment the following line to allow PHP to pretty-print .phps
# files as PHP source code:
#
#AddType application/x-httpd-php-source .phps

回答by Noushad

This might be happening due to the missing modules required for your php. Assuming you have php7 installed, search available php7 modules using

这可能是由于缺少 php.ini 所需的模块而发生的。假设你已经安装了 php7,搜索可用的 php7 模块使用

sudo apt-cache search php7-*

Above command will list all available PHP7 modules for installation. You can begin installation of modules like,

上面的命令将列出所有可用的 PHP7 模块进行安装。您可以开始安装模块,例如,

sudo apt-get install libapache2-mod-php7.0 php7.0-mysql php7.0-curl php7.0-json

回答by user252566

If Your .htaccess have anything like this ... AddHandler application/x-httpd-php53 .php .php5 .php4 .php3then comment it and try again refreshing this worked for me...

如果你的 .htaccess 有这样的东西...... AddHandler application/x-httpd-php53 .php .php5 .php4 .php3然后评论它并再次尝试刷新这对我有用......

回答by Ladislao Garcia

I have the same problem. Apache doesn't load php files from a certain website, just downloaded it. I read this post and the answers and I have seen I've got this line into the last place of the .htaccess file:

我也有同样的问题。Apache 不会从某个网站加载 php 文件,只是下载了它。我阅读了这篇文章和答案,我看到我把这一行放到了 .htaccess 文件的最后一个地方:

AddHandler x-mapp-php5.5  .php

I have commented it and everything works fine.

我已经评论过了,一切正常。

Thanks to all !!!

谢谢大家 !!!

回答by derchris

I previously has a similar issue, after upgrading from 5.3 to 5.4. But my setup looks a little bit different as that I'm running Debian and using fcgid to server the PHP pages, and not the PHP5 apache/cgi module. So after I upgraded, it also installed php5_cgi, which collided with my fcgid setup, and would not execute PHP files anymore.

我以前有过类似的问题,从 5.3 升级到 5.4 后。但是我的设置看起来有点不同,因为我运行的是 Debian 并使用 fcgid 来服务 PHP 页面,而不是 PHP5 apache/cgi 模块。所以我升级之后,还安装了php5_cgi,和我的fcgid设置冲突了,不会再执行PHP文件了。

I had to disable the Apache Module and restart Apache

我不得不禁用 Apache 模块并重新启动 Apache

a2dismod php5_cgi
/etc/init.d/apache2 restart

Once the php5_cgi module was out of the way, fcgid was able to serve PHP pages again.

一旦 php5_cgi 模块被排除在外,fcgid 就能够再次为 PHP 页面提供服务。

回答by MedoAlmasry

In case someone is using php7 under a Linux environment

如果有人在 Linux 环境下使用 php7

Make sure you enable php7

确保启用 php7

sudo a2enmod php7

Restart the mysql service and Apache

重启mysql服务和Apache

sudo systemctl restart mysql
sudo systemctl restart apache2

回答by hexicle

It's also possible that you have nginx running but your php is set up to run with apache. To verify, run service nginx statusand service apache2 statusto see which is running. In the case that nginx is running and apache is not, just run sudo service nginx stop; sudo service apache2 startand your server will now serve php files as expected.

也有可能您运行了 nginx,但您的 php 设置为与 apache 一起运行。要验证,请运行service nginx statusservice apache2 status查看哪个正在运行。如果 nginx 正在运行而 apache 没有运行,只需运行sudo service nginx stop; sudo service apache2 start,您的服务器现在将按预期提供 php 文件。