为什么我的 PHP 文件显示为纯文本?

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

Why are my PHP files showing as plain text?

phpapache

提问by iamjonesy

I've been writing PHP applications using PHP for a while in WAMP. Now I'm installing PHP and Apache HTTP Server separately on my work PC. I've installed PHP 5, and the latest Apache. I go to localhost and see it works!

我在 WAMP 中使用 PHP 编写 PHP 应用程序已经有一段时间了。现在我正在我的工作 PC 上分别安装 PHP 和 Apache HTTP Server。我已经安装了 PHP 5 和最新的 Apache。我去本地主机,看看它的工作原理!

Now I add a file called test.phpwhich displays:

现在我添加一个名为的文件test.php,它显示:

<?php
    phpinfo();
?>

But in the browser it just displays plain text. Is there somewhere I have explicitly tell it to use PHP 5?

但在浏览器中它只显示纯文本。有没有我明确告诉它使用 PHP 5 的地方?

回答by dav

You should install the PHP 5 library for Apache.

您应该为 Apache 安装 PHP 5 库。

For Debian and Ubuntu:

对于 Debian 和 Ubuntu:

apt-get install libapache2-mod-php5

And restart the Apache:

并重新启动Apache:

service apache2 restart

回答by Kris

You'll need to add this to your server configuration:

您需要将其添加到您的服务器配置中:

AddType application/x-httpd-php .php

That is assuming you have installed PHP properly, which may not be the case since it doesn't work where it normally would immediately after installing.

那是假设您已经正确安装了 PHP,但情况可能并非如此,因为它在安装后通常无法正常工作。

It is entirely possible that you'll also have to add the php.so/.dll file to your Apache configuration using a LoadModuledirective (usually in httpd.conf).

您完全有可能还必须php使用LoadModule指令(通常在 中httpd.conf)将.so/.dll 文件添加到您的 Apache 配置中。

回答by Piskvor left the building

You need to configure Apache (the webserver) to process PHP scripts as PHP. Check Apache's configuration. You need to load the module (the path may differ on your system):

您需要配置 Apache(网络服务器)以将 PHP 脚本作为 PHP 处理。检查Apache的配置。您需要加载模块(路径在您的系统上可能不同):

LoadModule php5_module "c:/php/php5apache.dll"

And you also need to tell Apache what to process with PHP:

你还需要告诉 Apache 用 PHP 处理什么:

AddType application/x-httpd-php .php

See the documentation for more details.

有关更多详细信息,请参阅文档

回答by Roman Bekkiev

Yet another reason (not for this case, but maybe it'll save some nerves for someone) is that in PHP 5.5 short open tags <? phpinfo(); ?>are disabled by default.

另一个原因(不是这种情况,但可能会为某些人节省一些神经)是在 PHP 5.5<? phpinfo(); ?>中,默认情况下禁用短打开标签。

So the PHP interpreter would process code within short tags as plain text. In previous versions PHP this feature was enable by default. So the new behaviour can be a little bit mysterious.

因此 PHP 解释器会将短标签内的代码作为纯文本处理。在以前的 PHP 版本中,此功能默认启用。所以新行为可能有点神秘。

回答by user3604332

You might also, like me, have installed php-cgi prior to installing Apache and when doing so it doesn't set up Apache properly to run PHP, removing PHP entirely and reinstalling seemed to fix my problem.

您可能也像我一样,在安装 Apache 之前安装了 php-cgi,这样做时它没有正确设置 Apache 来运行 PHP,完全删除 PHP 并重新安装似乎解决了我的问题。

回答by Satyanarayana

You will need to add handlers in Apache to handle php code.

您需要在 Apache 中添加处理程序来处理 php 代码。

Edit by command sudo vi /etc/httpd/conf/httpd.conf

通过命令 sudo vi /etc/httpd/conf/httpd.conf 进行编辑

Add these two handlers

添加这两个处理程序

  AddType application/x-httpd-php .php
  AddType application/x-httpd-php .php3

at position specified below

在下面指定的位置

<IfModule mime_module>

 AddType application/x-compress .Z
 AddType application/x-gzip .gz .tgz

--Add Here--


</IfModule>

for more details on AddType handlers

有关 AddType 处理程序的更多详细信息

http://httpd.apache.org/docs/2.2/mod/mod_mime.html

http://httpd.apache.org/docs/2.2/mod/mod_mime.html

回答by Henrik Sommerland

Are you using the userdir mod?

你在使用 userdir mod 吗?

In that case the thing is that PHP5 seems to be disabling running scripts from that location by default and you have to comment out the following lines:

在这种情况下,PHP5 似乎默认禁止从该位置运行脚本,您必须注释掉以下几行:

<IfModule mod_userdir.c>
    <Directory /home/*/public_html>
        php_admin_flag engine Off
    </Directory>
</IfModule>

in /etc/apache2/mods-enabled/php5.conf(on a ubuntu system)

/etc/apache2/mods-enabled/php5.conf(在 ubuntu 系统上)