Heim  >  Artikel  >  Backend-Entwicklung  >  PHP 5.4 内置Web服务器_PHP教程

PHP 5.4 内置Web服务器_PHP教程

WBOY
WBOYOriginal
2016-07-20 10:58:43875Durchsuche

 

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

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

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


  1. <span><span>$ php -S localhost:8000 </span></span>

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

例 #1 启动Web服务器


  1. <span><span>$ cd ~/public_html  </span></span>
  2. $ php -S localhost:8000 

终端输出信息:


  1. <span><span>PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011  </span></span>
  2. Listening on localhost:8000  
  3. Document root is /home/me/public_html  
  4. Press Ctrl-C to quit 

当请求了 http://localhost:8000/ 和 http://localhost:8000/myscript.html 地址后,终端输出类似如下的信息:


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

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


  1. <span><span>$ cd ~/public_html  </span></span>
  2. $ php -S localhost:8000 -t foo/ 

终端显示信息:


  1. <span><span>PHP 5.4.0 Development Server started at Thu Jul 21 10:50:26 2011  </span></span>
  2. Listening on localhost:8000  
  3. Document root is /home/me/public_html/foo  
  4. Press Ctrl-C to quit 

如果你在启动命令行后面附加一个php脚本文件,那这个文件将会被当成一个“路由器”脚本。这个脚本将负责所有的HTTP请求,如果这个脚本执行时返回FALSE,则被请求的资源会正常的返回。如果不是FALSE,浏览里显示的将会是这个脚本产生的内容。

例 #3 使用路由器脚本

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


  1. <span class="comment">// router.php </span><span> </span>
  2. if (preg_match('/.(?:png|jpg|jpeg|gif)$/'$_SERVER["REQUEST_URI"])) {  
  3. return false;    // serve the requested resource as-is.  
  4. else {  
  5. echo "

    Welcome to PHP

    ";  
  6. }  
  7. ?> 

  1. <span><span>$ php -S localhost:8000 router.php </span></span>

例 #4 判断是否是在使用内置web服务器

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


  1. <span class="comment">// router.php </span><span> </span>
  2. if (php_sapi_name() == 'cli-server') {  
  3. /* route static assets and return false */ 
  4. }  
  5. /* go on with normal index.php operations */ 
  6. ?> 

  1. <span><span>$ php -S localhost:8000 router.php </span></span>

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

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

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


  1. <span class="comment">// router.php </span><span> </span>
  2. $path = pathinfo($_SERVER["SCRIPT_FILENAME"]);  
  3. if ($path["extension"] == "ogg") {  
  4. header("Content-Type: video/ogg");  
  5. readfile($_SERVER["SCRIPT_FILENAME"]);  
  6. }  
  7. else {  
  8. return FALSE;  
  9. }  
  10. ?> 

  1. <span><span>$ php -S localhost:8000 router.php </span></span>

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

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


  1. <span><span>$ php -S 0.0.0.0:8000 </span></span>

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


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445655.htmlTechArticlePHP是一种脚本语言,它需要PHP解释器来分析运行PHP文件。当把PHP做为CGI服务Web请求时,它需要被嵌入到某种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