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 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 2011
- Listening on localhost: 8000
- Document root is/home/me/public_html
- Press 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 2011
- Listening on localhost: 8000
- Document root is/home/me/public_html
- Press 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 2011
- Listening on localhost: 8000
- Document root is/home/me/public_html/foo
- Press 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 ":
-