Built-in web server

As of PHP 5.4.0, the CLI SAPI provides a built-in web server. 

This web server is designed for developmental purposes only, and should notbe used in production. 

URI requests are served from the current working directory wherePHP was started, unless the -t option is used to specify anexplicit document root. 

If a URI request does not specify a file, then either index.phpor index.html in the given directory are returned. If neither fileexists, then a 404 response code is returned. 

If a PHP file is given on the command line when the web server isstarted it is treated as a "router" script for the web server.The script is run at the start of each HTTP request. If thisscript returns FALSE, then the requested resource is returnedas-is. Otherwise the script's output is returned to the browser. 

Example #1 Starting the web server

$ cd ~/public_html

$ php -S localhost:8000

The terminal will show: 

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

After URI requests for http://localhost:8000/ andhttp://localhost:8000/myscript.html the terminal will showsomething similar to: 

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

Example #2 Starting with a specific document root directory

$ cd ~/public_html

$ php -S localhost:8000 -t foo/

The terminal will show: 

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

Example #3 Using a Router Script

Requests for images will display them, but requests for HTML files will display "Welcome to PHP" 

<?php

// router.php

if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"]))

    return false;    // serve the requested resource as-is.

else { 

    echo "<p>Welcome to PHP</p>";

}

?>  

$ php -S localhost:8000 router.php

After several URI requests the terminal will show something similar to: 

PHP 5.4.0 Development Server started at Thu Jul 21 10:53:19 2011

Listening on localhost:8000

Document root is /home/me/public_html

Press Ctrl-C to quit.

[Thu Jul 21 10:53:45 2011] ::1:55801 GET /mylogo.jpg - Request read

[Thu Jul 21 10:53:52 2011] ::1:55803 GET /abc.html - Request read

[Thu Jul 21 10:53:52 2011] ::1:55804 GET /favicon.ico - Request read

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

热门产品

历史上的今天:03月29日

热门专题

开放大学|开放大学报名,开放大学报考,开放大学,什么是开放大学,开放大学学历,开放大学学费,开放大学报名条件,开放大学报名时间,开放大学学历,开放大学专业
开放大学
云南高职单招|云南单招,云南单招网,云南高职单招网,云南高职单招,云南单招学校,云南单招培训
云南高职单招
昆明网站建设|昆明网站建设,昆明网站开发,昆明网站建设公司,昆明网站建设价格,昆明网站设计,昆明网站制作,网页设计,高端网站建设,高端网站设计
昆明网站建设
综合高中|云南综合高中,昆明综合高中,综合高中能考本一吗,综合高中和普通高中的区别,综合高中是什么意思,综合高中能参加全国统一高考吗,综合高中可以考哪些大学,综合高中的学籍是什么
综合高中
易捷尔高职单招|易捷尔高职单招,易捷尔高职单招培训,单招分数线,单招录取分数线,高职单招学校分数线
易捷尔高职单招
自考本科|自考本科有用吗,自考文凭,自考本科文凭,自考文凭有用吗,自考本科文凭有用吗,自考文凭承认吗
自考本科
外贸网站建设|外贸网站建设,英文网站制作,英文网站设计,美国主机空间,外贸建站平台,多语言网站制作
外贸网站建设
小程序开发|微信小程序,小程序开发,小程序,小程序制作,微信小程序开发,小程序公司,小程序开发公司,分销,三级分销系统,分销系统
小程序开发

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部