Directory
- Start the server
- Configure the server
- Router Script
- Determine if the server is built-in
PHP5.4.0, PHP has a built-in Web server. It's a great tool for local development, and it's easy to debug locally without having to install Wamp, Xamp, or da Dah's Web server.
Start the server
Go to the root of the project and execute the command
php -S localhost:4000 #地址:监听端口
Or specify the site root directory directly
php -S localhost:4000 -t D:\website
When the browser opens localhost:4000, you can go to the Web browsing app.
To stop the PHP Web server, close the terminal app or press CTRL + C.
Configure the server
Specifies the initialization file. Special requirements for memory usage, file upload, parsing, or bytecode caching are available using a proprietary PHP initial configuration file.
php -S localhost:4000 -c app/config/php/ini
Router Script
The built-in server cannot route parsing, forwarding, redirecting, etc., and does not support. htaccess files. It is therefore difficult to use common front-end controllers in most popular PHP frameworks.
Use router scripting to compensate for this missing feature. Execute this router script before each HTTP request is processed. It works just like the. htaccess file.
php -S localhost:4000 router.php
The router script, such as a request for a picture, returns the corresponding picture, but the request to the HTML file displays "Welcome to PHP":
<?php// router.phpif (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"])) {return false; // serve the requested resource as-is.} else {echo "<p>Welcome to PHP</p>";}?>
Determine if the server is built-in
<?php if(php_sapi_name() === 'cli_server'){ //PHP内置服务器 }else{ //其他Web服务器 }
Disadvantage: Can not be used in production environment, can only be used for local development.
- Poor performance. Only one request can be processed at a time;
- Support for a small number of media types;
- Support for a small number of URL rewrite rules.
Built-in HTTP Server "modern PHP"