New features of modern PHP series (7)-built-in HTTP server

Source: Internet
Author: User
Tags php web server
New features of modern PHP series (7)-built-in HTTP servers from PHP 5.4.0, PHP built-in Web servers, this is a hidden feature for developers who think that Apache or Nginx is required to preview PHP applications. This built-in Web server should not be used in the production environment, but is an excellent tool for local development. Laravel Valet initially used this built-in server, but replaced it with Caddy after version 1.1.0 (view related news ).

1. start

This built-in Web server is easy to start. open the terminal (cmd command line in Windows), enter the project root directory, and execute the following command:

php -S localhost:8000

The above Command will launch a new PHP Web server with the address localhost and listening port 8000. the current directory is the root directory of the Web server.

Now, open your browser and access http: // localhost: 8000 to preview the application. When browsing an application in a Web browser, the information of each HTTP request is recorded in the standard output of the terminal. Therefore, we can check whether the application throws a 404 or 500 response:

Sometimes we need to access this server (such as an iPad or a local virtual machine) from another device in the same LAN. Therefore, we can change localhost to 0.0.0.0 to allow the PHP Web server to listen to all interfaces:

php -S 0.0.0.0:8000

To stop the Web server, you can close the terminal or press Ctrl + C.

2. configuration

Applications often need to use exclusive PHP configuration files, especially when they have special requirements on memory usage, file upload, analysis, or bytecode cache, we can use the-c option to allow the PHP built-in server to use the specified configuration file:

php -S localhost:8000 -c app/config/php.ini
3. routing script

The PHP built-in server does not support a function, which is different from Apache and Nginx. therefore, it is difficult for this server to use the common front-end controllers (single entry file index) in most popular PHP frameworks. php is used to forward all HTTP requests. Currently, mainstream PHP frameworks such as Laravel and Symfony are like this ).

The PHP built-in server uses the routing script to make up for this defect. before processing each HTTP request, the routing script will be executed first. if the result is false, returns the static resource URI referenced in the current HTTP request. Otherwise, the routing script execution result is returned as an HTTP response body. In other words, the routing script serves the same purpose as. htaccess.

The routing script is easy to use. you only need to specify the path of the PHP script file when starting the PHP built-in server:

php -S localhost:8000 router.php

About routing scripts, interested students can study LaravelValet underlying server. php (https://github.com/laravel/valet/blob/master/server.php ).

4. judgment functions

Sometimes we need to know whether PHP scripts use PHP built-in Web servers or traditional Web servers, so that we can set different response headers for different servers. We can use the php_sapi_name () function to check which PHP Web server is used. if the current script uses the PHP built-in server, the function returns the string cli-server:

 5. Disadvantages

PHP built-in Web servers cannot be used in the production environment, but can only be used in the local development environment, because they have many disadvantages compared with Apache or Nginx:

  • Poor performance. Only one request can be processed at a time, and other requests will be blocked. If a process takes a long time (database query or remote API call), the entire Web application will be paused.
  • Few media types are supported (this is a major improvement in PHP 5.5.7 and later ).
  • The routing script only supports a small amount of URL rewriting. Apache or Nginx is required for more advanced routing.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.