简体   繁体   English

PHP脚本不在本地主机上运行

[英]PHP scripts aren't run on localhost

I wrote a simple script and I executed it on localhost but it wasn't executed. 我写了一个简单的脚本,并在localhost上执行了该脚本,但未执行。

I am used to use Ubuntu operation system. 我习惯使用Ubuntu操作系统。

I've checked apache2 web server which to be run by following command : 我已经检查了要通过以下命令运行的apache2 Web服务器:

sudo service apache2 start

After checking the service to be run I wrote simple script such a : 检查要运行的服务后,我编写了一个简单的脚本,例如:

<head>
<title>THE PRACTICING FILE</title> 
</head>
<body>
<?php
echo "HELLO";
?>
</body>

After running the script, I found out that the resulting page just shows the tages which I used in the script such as < title > tag at the top of page. 运行脚本后,我发现结果页面仅显示了我在脚本中使用的标记,例如页面顶部的<title>标记。 But the php codes weren't executed, In this case echo "Hello"; 但是没有执行php代码,在这种情况下, 回显为“ Hello”。 wasn't executed. 没有执行。

What's wrong ? 怎么了 ? why such a thing have been occurred ? 为什么发生这样的事情? and finally how can i resolve the problem ? 最后我该如何解决这个问题?

Apache is a web server and by default serves static content, meaning HTML documents and any other kind of text or binary files as is. Apache是​​Web服务器,默认情况下会提供静态内容,即HTML文档和其他任何类型的文本或二进制文件。 The way you get it to process php content and send its output is by using an Apache PHP module to invoke the PHP processor. 获取它来处理php内容并发送其输出的方法是使用Apache PHP模块调用PHP处理器。

In order to do achieve that, make sure that: 为了做到这一点,请确保:

  • You have installed php and you have a php module configured for apache: sudo apt-get install php5 libapache2-mod-php5 您已经安装了php,并且已经为apache配置了php模块: sudo apt-get install php5 libapache2-mod-php5

  • Your script has a .php extension, such as myscript.php , which is one of the default extensions to be handled by the php module on Apache. 您的脚本具有.php扩展名,例如myscript.php ,这是Apache上php模块要处理的默认扩展名之一。

  • You restarted apache after installing the php module: sudo service apache2 restart 您在安装php模块后重新启动了apache: sudo service apache2 restart

  • You are hitting the proper page on your machine's browser, such as http://localhost/myscript.php , and not something like file:///var/www/html/myscript.php 您正在机器浏览器中访问正确的页面,例如http://localhost/myscript.php ,而不是file:///var/www/html/myscript.php

If it still doesn't work, you can also check the logs on /var/log/httpd/error_log to see what went wrong. 如果仍然无法解决问题,您还可以检查/var/log/httpd/error_log上的/var/log/httpd/error_log ,看看出了什么问题。

For more info on Ubuntu with Apache and PHP (+MySQL= LAMP), follow this tutorial 有关使用Apache和PHP(+ MySQL = LAMP)的Ubuntu的更多信息,请遵循本教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM