Apache 显示 php 代码而不是执行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12142172/
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
Apache shows php code instead of executing
提问by imulsion
I have recently been trying to install php and apache on my computer. After many hours, they're installed. I have modified the httpd.conf and php.ini files like everyone says. I then created a simple php script:
我最近一直在尝试在我的电脑上安装 php 和 apache。几个小时后,它们被安装。我像大家说的那样修改了 httpd.conf 和 php.ini 文件。然后我创建了一个简单的 php 脚本:
<?php phpinfo(); ?>
But when I try to run it with http://127.0.0.1/phpinfo.phpit just shows the source code instead of executing. I am using apache2, php5 and windows vista. I have been searching all over the internet without success and am almost banging my head against the wall. Can anyone save me from a concussion?
但是当我尝试用http://127.0.0.1/phpinfo.php它运行它时,它只显示源代码而不是执行。我正在使用 apache2、php5 和 windows vista。我一直在互联网上搜索但没有成功,几乎把我的头撞在墙上。谁能救我免于脑震荡?
EDITED LINES:
编辑的行:
PHP.INI:
PHP.INI:
short_open_tag = On
HTTPD.CONF
配置文件
LoadModule php5_module "C:/php/php5apache2_2.dll"
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
PHPIniDir "C:/php"
采纳答案by Daniel Figueroa
You must enable php! Check the folder mods-enabledin the Apache directory (default: /etc/apache2/) to see if you find a file named php. I don't remember the extension but I think it's .so.
你必须启用php!检查mods-enabledApache 目录中的文件夹(默认:/etc/apache2/)以查看是否找到名为php. 我不记得扩展名,但我认为它是.so.
Also check in /var/log/apache2/error.logto see if you have any other errors.
还要检查/var/log/apache2/error.log是否有任何其他错误。
回答by helmy
Wow, lots of solutions here! Here's what I did on Ubuntu 16.04:
哇,这里有很多解决方案!这是我在Ubuntu 16.04上所做的:
sudo apt-get install php libapache2-mod-php
sudo a2enmod mpm_prefork && sudo a2enmod php7.0
sudo service apache2 restart
回答by RyanNerd
For PHP 7 (May apply to previous versions as well), but I had to do this:
对于 PHP 7(也可能适用于以前的版本),但我必须这样做:
Add this to the bottom of /etc/apache2/apache2.conf
将此添加到 /etc/apache2/apache2.conf 的底部
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
Run this from the terminal:
从终端运行:
sudo a2dismod mpm_event && sudo a2enmod mpm_prefork && sudo a2enmod php7
Then don't forget to restart Apache so it knows you changed stuff:
然后不要忘记重新启动Apache,以便它知道您更改了内容:
sudo service apache2 restart
This is a summary from: https://www.atlantic.net/community/howto/try-php7-lamp-ubuntu-14-04/
这是摘自:https: //www.atlantic.net/community/howto/try-php7-lamp-ubuntu-14-04/
回答by inigo333
open the file
打开文件
/etc/apache2/httpd.conf
/etc/apache2/httpd.conf
and change
和改变
#LoadModule php5_module libexec/apache2/libphp5.so
into
进入
LoadModule php5_module libexec/apache2/libphp5.so
So just uncoment the PHP module load in httpd.conf
所以只需取消注释 PHP 模块加载 httpd.conf
回答by Pawel
I found this to solve my related problem.I added it to the relevant <Directory>section:
我发现这可以解决我的相关问题。我将其添加到相关<Directory>部分:
<IfModule mod_php5.c>
php_admin_flag engine on
</IfModule>
回答by Simon Mohoalali
A different answer that worked for me. Is that, to install [sudo] apt-get install libapache2-mod-php5.X
一个对我有用的不同答案。是吗,要安装[sudo] apt-get install libapache2-mod-php5.X
回答by evanmcd
Posting what worked for me in case in helps someone down the road, though it is an unusual case.
发布对我有用的内容以防万一可以帮助某人,尽管这是一个不寻常的案例。
I had set a handler to force my web host to use a higher version of php than their default. There's was 5.1, but I wanted 5.6 so I had this:
我设置了一个处理程序来强制我的网络主机使用比默认版本更高版本的 php。有 5.1,但我想要 5.6,所以我有这个:
<FilesMatch \.php$>
SetHandler php56-cgi
</FilesMatch>
in my .htaccess file.
在我的 .htaccess 文件中。
When trying to run my site locally, having that in there caused php code to be output to the browser. Removing it solved the problem.
尝试在本地运行我的网站时,将其放入会导致 php 代码输出到浏览器。删除它解决了问题。
回答by DAniel
Alright if you've tried what you've been told above or earlier(which are possible reasons) and it still displays the code instead of executing it then there is one thing which you are doing wrong that hasn't been addressed. The url you used to access your php code; some people try to execute their php code by just dragging the .php file into the web browser. this is wrong practice and could lead to this kind of problem. if you have saved a file as "test.php" in the C://wamp/www folder then you must access this file this way: localhost://test.php. this kind of mistake will arise when you access it this way: localhost://wamp/www/test.php
好吧,如果您已经尝试了上面或之前告诉您的内容(这是可能的原因)并且它仍然显示代码而不是执行它,那么您做错的一件事尚未得到解决。你用来访问你的php代码的url;有些人试图通过将 .php 文件拖入网络浏览器来执行他们的 php 代码。这是错误的做法,可能会导致此类问题。如果您在 C://wamp/www 文件夹中将文件保存为“test.php”,那么您必须通过以下方式访问该文件:localhost://test.php。这样访问就会出现这种错误:localhost://wamp/www/test.php
Hope I helped someone out there. o/ ~Daniel
希望我帮助了那里的人。o/ ~丹尼尔
回答by bokistack
You could delete your .htaceessfile and let the system creates a new (blank or basic) one. It may be that some lines you added are incorrect statements, or something else inside is causing the problem. You can then add line by line from your old .htaccessfile to see what caused the problem.
您可以删除.htaceess文件并让系统创建一个新的(空白或基本的)文件。可能是您添加的某些行是不正确的语句,或者内部的其他内容导致了问题。然后,您可以从旧的.htaccess文件中逐行添加以查看导致问题的原因。
That worked for me. The lines telling machine not to display php extensions were problem in my case (RewriteCondand RewriteRule). My old .htaccessfile worked just fine on a shared server, but this issue appeared when I switched to VPS.
那对我有用。在我的情况下,告诉机器不要显示 php 扩展名的行是问题(RewriteCond和RewriteRule)。我的旧.htaccess文件在共享服务器上工作得很好,但是当我切换到 VPS 时出现了这个问题。
回答by djnz0feh
Thanks to others on this thread for their suggestions. Following the steps mentioned I found that the apache server was not able to start reporting a syntax error in a loadfile in /etc/apache2/mods-enabled. Turns out that both php7.0 and php7.1 were enabled.
感谢此线程上的其他人的建议。继步骤中提到我发现,Apache服务器无法启动在报告语法错误load的文件/etc/apache2/mods-enabled。结果发现 php7.0 和 php7.1 都启用了。
a2dismod php7.0
systemctl restart apache2
and php is rendered correctly again.
并且 php 再次正确呈现。

![isset PHP isset($_GET['something']) ? $_GET['东西'] : ''](/res/img/loading.gif)