[ModernPHP] Chapter 2 new features 7 built-in HTTP server
Built-in HTTP server
Do you know that PHP has a built-in web server since 5.4.0? For PHP developers who only know how to preview PHP pages using Apache or nginx, this is an undiscovered gem. Although you cannot use the PHP built-in web server in the product environment, this function is a perfect tool for local development.
Whether or not I am writing PHP code, I use PHP's built-in web server every day. I will use it to preview Laravel and Slim Framework (note: The Framework author is the book author Josh Lockhart) applications, it is used when a website is built using the content management system of the Drupal Framework, and even when a prototype page is constructed, it is used to preview static HTML and CSS.
Remember, the built-in PHP Server is a web server. It only uses the HTTP protocol. in addition to the PHP file, it can return static resources. We can develop and preview HTML pages locally without using MAMP, WAMP, or other heavyweight web servers.
Start the server
It is easy to start the PHP web server. Open your terminal program, enter the document root directory of your project, and execute the following command:
Php-S localhost: 4000
This command can start a new PHP web server, which can be accessed through localhost. It listens to Port 4000. Your current working directory is the document root directory of the web server.
Open your browser and access http: // localhost: 4000 to preview your application. Because you use a web browser to browse your applications, each HTTP request is recorded in your terminal program and output directly on the terminal, it can detect whether your application has a 400 or 500 error.
Sometimes you need to allow other devices to access the PHP web server in your local environment (for example, preview using your iPad or local Windows virtual machine ). To achieve this, you can allow the PHP web server to use 0.0.0.0 instead of localhost to listen for requests from all network connections.
Php-S 0.0.0.0: 4000
When you are about to stop the PHP web server, simply close the terminal program or press Ctrl + C.
Configuration Server
An application corresponds to a php ini configuration file, which is very common. in particular, your application has special requirements for memory usage, file upload, debugging, or bytecode cache. When using the PHP built-in server, we can use the-c parameter to tell the server to use a specified INI file:
Php-S localhost: 8000-c app/config/php. ini
It is a good idea to put a custom INI file under the root directory of the application. you can even use version control to save the INI file so that it can be shared with other developers in the development team.
Routing script
Unlike Apache or nginx, the PHP built-in server has a major defect: it does not support. htaccess files. Therefore, the front-end controllers used by many popular PHP frameworks are difficult to use on the PHP built-in server.
The front-end controller is a separate PHP file, and all HTTP requests are forwarded to it (by using the. htaccess file or rewrite rules ). The front-end controller file routes requests and distributes them to the corresponding PHP code. Symfony and other popular PHP frameworks use this common mode.
PHP built-in servers use routing scripts to slightly compensate for this defect. The routing script is executed before each HTTP request. If the script returns false, the static resource corresponding to the current HTTP request address is directly returned. Otherwise, the content returned by the script is returned to the browser as the HTTP response. In other words, your routing script is actually copying the. htaccess file function.
The routing script is simple. you only need to use the file address of the PHP script as a eucalyptus to pass the startup command to the PHP built-in server:
Php-S localhost: 8000 router. php
Determine requests from built-in servers
Sometimes it is useful to distinguish whether the Accessed PHP script is from the PHP built-in server or a traditional web server similar to Apache and nginx. You may need to add a special HTTP header (such as Status :) in nginx, but you cannot sit on the PHP web server. You can use the php_sapi_name () function to determine the PHP web server. When the script you are requesting is from the PHP built-in server, this function returns the string cli-server:
Disadvantages
PHP built-in web servers cannot be used in the product environment. It is only designed for local development. If you use a PHP built-in server on the product server, you will be faced with a large number of disappointed users and flood of on-board notifications from Pingdom.
The performance of the built-in server is poor because it can only process one request at a time, and other requests will be blocked. If a php file is stuck in a slow query or remote API return, your web application will not be able to respond. Built-in servers only support limited MIME types The routing script used by the built-in server restricts URL rewriting. You need Apache or nginx for more advanced URL rewriting operations.