Heim  >  Artikel  >  Backend-Entwicklung  >  PHP5.4内置web服务器_php实例

PHP5.4内置web服务器_php实例

WBOY
WBOYOriginal
2016-08-17 13:02:35917Durchsuche

PHP是一种脚本语言,它需要PHP解释器来分析运行PHP文件。当把PHP做为CGI服务Web请求时,它需要被嵌入到某种Web服务器里,最常见的是集成到Apache或IIS里,这就是说,在使用PHP前,你需要安装Apache或IIS,并且正确的配置它们和PHP集成的参数。虽然这种配置已经很规范,文档非常丰富,但我们还是经常在安装Apache和PHP集成时遇到问题,而且,有时候我们只想测试一个简单的PHP特征,不想就为此安装、启动Apache服务。

但据官方文档上说,这个内置的Web服务器只是提供开发测试使用,不推荐使用中生产环境中。因为这个服务器接受处理请求时顺序执行的,不能并发处理。

这个内置的web服务器使用起来非常的方便,你只需要执行下面的命令:

$ php -S localhost:8000
然后就可以访问了。这样启动后,默认的web服务目录是执行命令的当前目录,如果不想使用当前目录,你需要使用 -t 参数来指定。

例 #1 启动Web服务器

$ cd ~/public_html
$ php -S localhost:8000
终端输出信息:

PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011
Listening on localhost:8000
Document root is /home/me/public_html
Press Ctrl-C to quit
当请求了 http://localhost:8000/ http://localhost:8000/myscript.html 地址后,终端输出类似如下的信息:

PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011
Listening on localhost:8000
Document root is /home/me/public_html
Press Ctrl-C to quit.
[Thu Jul 21 10:48:48 2011] ::1:39144 GET /favicon.ico - Request read
[Thu Jul 21 10:48:50 2011] ::1:39146 GET / - Request read
[Thu Jul 21 10:48:50 2011] ::1:39147 GET /favicon.ico - Request read
[Thu Jul 21 10:48:52 2011] ::1:39148 GET /myscript.html - Request read
[Thu Jul 21 10:48:52 2011] ::1:39149 GET /favicon.ico - Request read


例 #2 启动web服务器时指定文档的根目录

$ cd ~/public_html
$ php -S localhost:8000 -t foo/
终端显示信息:

PHP 5.4.0 Development Server started at Thu Jul 21 10:50:26 2011
Listening on localhost:8000
Document root is /home/me/public_html/foo
Press Ctrl-C to quit
如果你在启动命令行后面附加一个php脚本文件,那这个文件将会被当成一个“路由器”脚本。这个脚本将负责所有的HTTP请求,如果这个脚本执行时返回FALSE,则被请求的资源会正常的返回。如果不是FALSE,浏览里显示的将会是这个脚本产生的内容。

例 #3 使用路由器脚本

在这个例子中,对图片的请求会返回相应的图片,但对HTML文件的请求会显示“Welcome to PHP”:

<&#63;php
// router.php
if (preg_match('/\.(&#63;:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"])) {
return false;  // serve the requested resource as-is.
} else {
echo "<p>Welcome to PHP</p>";
}
&#63;>

$ php -S localhost:8000 router.php
例 #4 判断是否是在使用内置web服务器

通过程序判断来调整同一个PHP路由器脚本在内置Web服务器中和在生产服务器中的不同行为:

<&#63;php
// router.php
if (php_sapi_name() == 'cli-server') {
/* route static assets and return false */
}
/* go on with normal index.php operations */
&#63;>

$ php -S localhost:8000 router.php
这个内置的web服务器能识别一些标准的MIME类型资源,它们的扩展有:.css, .gif, .htm, .html, .jpe, .jpeg, .jpg, .js, .png, .svg, and .txt。对.htm 和 .svg 扩展到支持是在PHP 5.4.4之后才支持的。

例 #5 处理不支持的文件类型

如果你希望这个Web服务器能够正确的处理不被支持的MIME文件类型,这样做:

<&#63;php
// router.php
$path = pathinfo($_SERVER["SCRIPT_FILENAME"]);
if ($path["extension"] == "ogg") {
header("Content-Type: video/ogg");
readfile($_SERVER["SCRIPT_FILENAME"]);
}
else {
return FALSE;
}
&#63;>

$ php -S localhost:8000 router.php

如果你希望能远程的访问这个内置的web服务器,你的启动命令需要改成下面这样:

例 #6 远程访问这个内置Web服务器

$ php -S 0.0.0.0:8000
这样你就可以通过 8000 端口远程的访问这个内置的web服务器了

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn