PHP is a scripting language, which requires a PHP interpreter to analyze and run PHP files. When using PHP as a CGI to serve web requests, it needs to be embedded into some kind of web server, most commonly integrated into Apache or IIS. This means that before using PHP, you need to install Apache or IIS, and Correctly configure them and PHP integration parameters. Although this configuration has been standardized and the documentation is very rich, we still often encounter problems when installing Apache and PHP integration. Moreover, sometimes we just want to test a simple PHP feature and do not want to install and start the Apache service for this purpose. .
But according to the official documentation, this built-in web server is only for development and testing, and is not recommended for use in production environments. Because this server accepts and processes requests sequentially and cannot handle them 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. After starting in this way, the default web service directory is the current directory where the command is executed. If you do not want to use the current directory, you need to use the -t parameter to specify it.
Example #1 Start 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
When the http://localhost:8000/ and http://localhost:8000/myscript.html addresses are requested, the terminal outputs information similar to the following:
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, this file will be treated as a "router" script. This script will be responsible for all HTTP requests. If this script returns FALSE when executed, the requested resource will be returned normally. If it is not FALSE, the content generated by this script will be displayed in the browser.
Example #3 Using Router Script
In this example, a request for an image returns the corresponding image, but a request for an HTML file displays "Welcome to PHP":
<?php // router.php if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"])) { return false; // serve the requested resource as-is. } else { echo "<p>Welcome to PHP</p>"; } ?>
$ php -S localhost:8000 router.php
Example #4 Determine whether you are using the built-in web server
Use program judgment to adjust the different behaviors of the same PHP router script in the built-in web server and in the production server:
<?php // router.php if (php_sapi_name() == 'cli-server') { /* route static assets and return false */ } /* go on with normal index.php operations */ ?>
$ php -S localhost:8000 router.php
This built-in web server recognizes some standard MIME type resources with extensions: .css, .gif, .htm, .html, .jpe, .jpeg, .jpg, .js, .png, .svg, and .txt. Support for .htm and .svg extensions is only supported after PHP 5.4.4.
Example #5 Handling unsupported file types
If you want this web server to correctly handle unsupported MIME file types, do this:
<?php // router.php $path = pathinfo($_SERVER["SCRIPT_FILENAME"]); if ($path["extension"] == "ogg") { header("Content-Type: video/ogg"); readfile($_SERVER["SCRIPT_FILENAME"]); } else { return FALSE; } ?> $ php -S localhost:8000 router.php
If you want to remotely access this built-in web server, your startup command needs to be changed 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 the built-in web server through port 8000
The above is the entire content of this article. I hope it will be helpful to everyone’s study. I also hope that everyone will support Script Home.