PHP 未解释,显示在视图源代码中

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

PHP not interpreted, showing in view source

phpapache

提问by Mark

I'm hoping that this will be a useful page for getting started running php code as well as solve the current problem I'm having some very simple code as follows:

我希望这将是开始运行 php 代码以及解决当前问题的有用页面我有一些非常简单的代码如下:

<html>
<head>
<title> Practice</title></head>
<body>
This is HTML
<?php
echo "This is PHP";
?>
</body>
<html>

This is uploaded on an ec2 website which has apache running. The code isn't interpreted, and when you view source of the page it shows the php code.

这是在运行 apache 的 ec2 网站上上传的。代码不会被解释,当您查看页面的源代码时,它会显示 php 代码。

You can see the page.

你可以看到页面

Any ideas? The php code is so basic that I think it might have to do with the apache configuration. Please let me know any additional information you need and I'll provide it, hopefully tell me how to get it to.

有任何想法吗?php 代码非常基础,我认为它可能与 apache 配置有关。请让我知道您需要的任何其他信息,我会提供它,希望告诉我如何获取它。

采纳答案by secretformula

Are you sure you have php installed? If it is you need to make sure that apache is associating .phpfiles with the php handler. Look for an entry similar to the following in /etc/apache/apache.conf

你确定你安装了php?如果是,您需要确保 apache 将.php文件与 php 处理程序相关联。在中查找类似于以下内容的条目/etc/apache/apache.conf

 LoadModule php5_module modules/libphp5.so

and

 application/x-httpd-php        php php5

upon changing the file you will need to restart apache via sudo service httpd restart

更改文件后,您需要通过以下方式重新启动 apache sudo service httpd restart

回答by Nvan

You can install libapache2-mod-php5using

您可以安装中的libapache2-MOD-PHP5使用

apt-get install libapache2-mod-php5

Worked for me.

为我工作。

回答by Raghuraman K

If you are using php7 make sure you installed this module.

如果您使用的是 php7,请确保您安装了此模块。

sudo apt-get install libapache2-mod-php7.0

Replace 7.0 with the version of php you are using.

将 7.0 替换为您正在使用的 php 版本。

To find version of php, use

要查找 php 版本,请使用

php -v 

回答by Arcatica

You probably need addHandler or addType in either the .htaccess file or Apache config itself: e.g. AddType application/x-httpd-php .php

您可能需要在 .htaccess 文件或 Apache 配置本身中使用 addHandler 或 addType:例如 AddType application/x-httpd-php .php

回答by LinuxSecurityFreak

If you have the contents of the web page in user directory like:

如果您在用户目录中有网页内容,例如:

/home/*/public_html

Then you need to enable executing those, it's disabled by default:

然后你需要启用执行这些,默认情况下它是禁用的:

# Running PHP scripts in user directories is disabled by default
# 
# 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_flag engine Off
    </Directory>
</IfModule>

Just comment out this piece of code located in the file:

只需注释掉位于文件中的这段代码:

/etc/apache2/mods-enabled/php7.3.conf

Adjust the path and file name to your system, PHP version, etc.

将路径和文件名调整为您的系统、PHP 版本等。

回答by Edoardo Vignati

In my case I had the /var/www/folder with wrong permissions.

就我而言,我的/var/www/文件夹权限错误。

I had to run:

我不得不跑:

sudo chown -R www-data /var/www/
sudo chgrp -R www-data /var/www/