PHP is a scripting language that requires a PHP interpreter to analyze and run PHP files. When PHP is used as a CGI service Web request, it needs to be embedded into a Web server. The most common thing is to integrate it into Apache or IIS. that is to say, before using PHP, you need to install Apache or iis php as a script language, which requires the PHP interpreter to analyze and run the PHP file. When PHP is used as a CGI service Web request, it needs to be embedded into a Web server. The most common thing is to integrate it into Apache or IIS. that is to say, before using PHP, you need to install Apache or IIS and correctly configure the parameters they integrate with PHP. Although this configuration is already very standard and has rich documentation, we often encounter problems when installing Apache and PHP integration. In addition, sometimes we only want to test a simple PHP feature, you do not want to install or start the Apache service.
However, according to the official documentation, this built-in Web server is only available for development and testing and is not recommended in the production environment. Because this server is executed sequentially when processing requests, it cannot be processed concurrently.
This built-in web server is very convenient to use. you only need to execute the following command:
$ php -S localhost:8000
Then you can access it. In this way, the default web Service Directory is the current directory for executing commands. if you do not want to use the current directory, you need to use the-t parameter to specify it.
Example #1 start the Web server
$ cd ~/public_html$ php -S localhost:8000
Terminal output information:
PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011Listening on localhost:8000Document root is /home/me/public_htmlPress Ctrl-C to quit
After the http: // localhost: 8000/and http: // localhost: 8000/myscript.html addresses are requested, the terminal outputs the following information:
PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011Listening on localhost:8000Document root is /home/me/public_htmlPress Ctrl-C to quit.[Thu Jul 21 10:48:48 2011] ::1:39144 GET /favicon.ico - Request read[Thu Jul 21 10:48:50 2011] ::1:39146 GET / - Request read[Thu Jul 21 10:48:50 2011] ::1:39147 GET /favicon.ico - Request read[Thu Jul 21 10:48:52 2011] ::1:39148 GET /myscript.html - Request read[Thu Jul 21 10:48:52 2011] ::1:39149 GET /favicon.ico - Request read
Example #2 specify the root directory of the document when starting the web server
$ cd ~/public_html$ php -S localhost:8000 -t foo/
Terminal Display Information:
PHP 5.4.0 Development Server started at Thu Jul 21 10:50:26 2011Listening on localhost:8000Document root is /home/me/public_html/fooPress Ctrl-C to quit
If you append a php script file to the startup command line, the file will be treated as a "router" script. This script will be responsible for all HTTP requests. if FALSE is returned when the script is executed, the requested resource will be returned normally. If it is not FALSE, the content displayed in the browser will be generated by this script.
Example #3 use a vro script
In this example, the corresponding image will be returned for the image request, but the request to the HTML file will display "Welcome to PHP ":
Welcome to PHP";}?>
$ php -S localhost:8000 router.php
Example #4 check whether a built-in web server is used
Adjust the different behaviors of the same PHP router script on the built-in Web server and on the production server through program judgment:
$ php -S localhost:8000 router.php
This built-in web server can recognize some standard MIME-type resources. Their extensions include :. css ,. gif ,. htm ,. html ,. jpe ,. jpeg ,. jpg ,. js ,. png ,. svg, and. txt. Extension to .htm and. svg is supported only after PHP 5.4.4.
Example #5 process unsupported file types
If you want the Web server to correctly process unsupported MIME file types, do the following:
$ php -S localhost:8000 router.php
If you want to remotely access this built-in web server, change your startup command to the following:
Example #6 remotely access this built-in Web server
$ php -S 0.0.0.0:8000
In this way, you can remotely access this built-in web server through port 8000.