[Modern PHP] 第二章 新特性之七 内置HTTP服务器
内置HTTP服务器
你知道PHP从5.4.0开始有了一个内置的web服务器吗?对于那些只知道使用Apache或者nginx去预览PHP页面的PHP开发者们来说这又是一块未被发掘的宝石。虽然你不能在产品环境中使用PHP的内置web服务器,但是这个功能对于本地开发来说真是的一个完美的工具。
无论我是否在写PHP代码,反正每天都会使用PHP的内置web服务器。我会使用它来预览Laravel和Slim Framework(译者注:框架的作者就是本书的作者Josh Lockhart)应用程序,在使用Drupal框架的内容管理系统搭建网站时也会使用它,甚至在构造原型页面时使用它来预览静态的HTML和CSS。
记住,PHP内置的服务器是一个web服务器。它只使用HTTP协议,除了PHP文件之外,它还可以返回静态资源。我们可以在不使用MAMP、WAMP或者其它重量级的web服务器的情况下在本地很好的完成HTML页面的开发和预览操作。
启动服务器
启动PHP的web服务器很简单。打开你的终端程序,进入你的项目的文档根目录并执行下面的命令:
php -S localhost:4000
这个命令可以启动一个新的PHP web服务器,我们通过localhost就可以访问这个服务器。它会监听4000端口。而你当前的工作目录就是web服务器的文档根目录。
打开浏览器访问 http://localhost:4000 就可以预览你的应用程序了。由于你使用web浏览器浏览自己的应用程序,每个HTTP请求都会在你的终端程序中记录并直接在终端输出出来,通过它可以检测到你的应用程序是否会出现400或者500错误。
有时候你会需要允许其他的设备来访问你本地环境中的PHP web服务器(例如使用你的iPad或者本地的Windows虚拟机进行预览)。要实现这个需求,你可以让PHP web服务器使用0.0.0.0代替localhost来监听来自所有网络连接的请求。
php -S 0.0.0.0:4000
当你准备停止PHP web服务器时,直接关闭终端程序或者按Ctrl+C就可以了。
配置服务器
一个应用程序对应一个PHP的INI配置文件是很常用的处理,尤其是你的应用程序对内存使用、文件上传、调试或者字节码缓存等有着特别的需求。使用PHP内置服务器时我们可以使用-c参数告诉服务器去使用某个指定的INI文件:
php -S localhost:8000 -c app/config/php.ini
把自定义的INI文件放到应用程序的根目录下是很好的主意,甚至可以使用版本控制来保存INI文件,以便与开发团队中的其他开发者们进行共享。
路由脚本
不同于Apache或者nginx,PHP内置服务器有个明显的缺陷:它不支持.htaccess文件。因此很多流行的PHP框架都使用的前端控制器在PHP内置服务器上用起来却很麻烦。
前端控制器是一个单独的PHP文件,所有的HTTP请求都被转发到它上面(通过.htaccess文件或者rewrite规则)。前端控制器文件负责将请求进行路由并分发到对应的PHP代码上。Symfony和其他流行的PHP框架都使用了这个常用的模式。
PHP内置服务器使用路由脚本可以稍微弥补这个缺陷。路由脚本会在每次HTTP请求之前被执行。如果脚本返回false,那么会直接返回当前HTTP请求地址对应的静态资源,反之,则将脚本返回的内容作为HTTP返回内容返回给浏览器。换句话说,你的路由脚本事实上就是在照搬.htaccess文件的功能。
使用路由脚本很简单,只需要将PHP脚本的文件地址作为一个桉树传递给PHP内置服务器的启动命令:
php -S localhost:8000 router.php
判断来自内置服务器的请求
有时候能够区分访问的PHP脚本是来自PHP内置服务器还是类似Apache和nginx的传统的web服务器是很有用的。或许你需要在nginx中添加一个专门的HTTP头(例如 Status:)但是在PHP web服务器上却坐不到。你可以通过php_sapi_name()函数来判断出PHP web server。当你当前请求的脚本是来自PHP内置服务器时这个函数会返回字符串 cli-server:
<?phpif (php_sapi_name() === 'cil-server') { // PHP内置web服务器} else { // 其它web服务器}
缺点
PHP内置web服务器不可以在产品环境中使用。它仅仅是为了本地开发设计。如果你在产品服务器上使用了PHP内置服务器,你将面对的是大量失望的用户以及来自Pingdom的洪水般的当机通知。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
