现代 PHP 新特性系列(七) -- 内置的 HTTP 服务器
从PHP 5.4.0 起,PHP内置了Web服务器,这对于认为需要Apache或Nginx才能预览PHP应用的开发者来说又是一个隐藏功能。这个内置的Web服务器不应该用于生产环境,但对于本地开发来说是个极好的工具。Laravel Valet 起初就是使用这个内置的服务器,但是在1.1.0版本后将其替换为Caddy(查看相关新闻)。
1、启动
这个内置的Web服务器很容易启动,打开终端(Windows下对应是cmd命令行),进入项目根目录,执行如下命令即可:
php -S localhost:8000
上述命令会新启动一个PHP Web服务器,地址是 localhost ,监听的端口是 8000 ,当前所在目录就是这个Web服务器的根目录。
现在,打开浏览器,访问 http://localhost:8000 就可以预览应用了。在Web浏览器中浏览应用时,每个HTTP请求的信息都会记录到终端的标准输出中,因此我们可以查看应用是否抛出了404或500响应:
有时候我们需要在同一局域网中的另一台设备中访问这个服务器(例如iPad或本地虚拟机),为此,我们可以把 localhost 换成 0.0.0.0 ,让PHP Web服务器监听所有接口:
php -S 0.0.0.0:8000
要想停止Web服务器,可以关闭终端,也可以按Ctrl+C快捷键。
2、配置
应用常常需要使用专属的PHP配置文件,尤其是对内存使用、文件上传、分析或对字节码缓存有特殊要求时,一定要单独配置,我们可以使用 -c 选项,让PHP内置的服务器使用指定的配置文件:
php -S localhost:8000 -c app/config/php.ini
3、路由脚本
PHP内置服务器明显遗漏了一个功能:与Apache和Nginx不同,它不支持 .htaccess 文件,因此,这个服务器很难使用多数流行的PHP框架中常见的前端控制器(单一入口文件 index.php ,用于转发所有HTTP请求,现在主流PHP框架如Laravel、Symfony都是这样)。
PHP内置服务器使用路由脚本弥补了这一缺憾,处理每个HTTP请求前,会先执行这个路由脚本,如果结果为 false ,返回当前HTTP请求中引用的静态资源URI,否则会把路由脚本的执行结果当做HTTP响应主体返回。换句话说,路由脚本的作用其实和 .htaccess 一样。
路由脚本的用法很简单,只需要在启动PHP内置服务器时指定这个PHP脚本文件的路径即可:
php -S localhost:8000 router.php
关于路由脚本,有兴趣的同学可以研究下LaravelValet底层的 server.php ( https://github.com/laravel/valet/blob/master/server.php )。
4、判断函数
有时候需要知道PHP脚本使用的是PHP内置的Web服务器还是使用传统的Web服务器,这样方便我们为不同服务器设定不同的响应头。我们可以使用 php_sapi_name() 函数检查使用的是哪个PHP Web服务器,如果当前脚本使用的是PHP内置服务器,则该函数返回字符串 cli-server :
<?phpif (php_sapi_name() == ‘cli-server') { // PHP 内置 Web 服务器} else { // 其他Web服务器}
5、缺点
PHP内置的Web服务器不能在生成环境使用,只能在本地开发环境中使用,这是因为其相比Apache或Nginx有诸多不足:
- 性能不佳。一次只能处理一个请求,其他请求会受到阻塞。如果某个进程耗时较长(数据库查询、远程API调用),则整个Web应用会陷入停顿状态。
- 支持媒体类型较少(这一点PHP 5.5.7以后有较大改进)。
- 路由脚本仅支持少量的URL重写,更高级则还是需要Apache或Nginx。

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 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 is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

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.
