PHP 54 built-in web server web server ranking web server principle simple web server

WBOY
Release: 2016-07-29 08:50:21
Original
1285 people have browsed it

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:

<ol><li><span><span>$ php -S localhost:8000 </span></span></li></ol>
Copy after login

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 the Web server

<ol>
<li><span><span>$ cd ~/public_html  </span></span></li>
<li><span>$ php -S localhost:8000 </span></li>
</ol>
Copy after login

Terminal output information:

<ol>
<li><span><span>PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011  </span></span></li>
<li><span>Listening on localhost:8000  </span></li>
<li><span>Document root is /home/me/public_html  </span></li>
<li><span>Press Ctrl-C to quit </span></li>
</ol>
Copy after login

After requesting the http://localhost:8000/ and http://localhost:8000/myscript.html addresses, the terminal outputs information similar to the following :

<ol>
<li><span><span>PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011  </span></span></li>
<li><span>Listening on localhost:8000  </span></li>
<li><span>Document root is /home/me/public_html  </span></li>
<li><span>Press Ctrl-C to quit.  </span></li>
<li><span>[Thu Jul 21 10:48:48 2011] ::1:39144 GET /favicon.ico - Request read  </span></li>
<li><span>[Thu Jul 21 10:48:50 2011] ::1:39146 GET / - Request read  </span></li>
<li><span>[Thu Jul 21 10:48:50 2011] ::1:39147 GET /favicon.ico - Request read  </span></li>
<li><span>[Thu Jul 21 10:48:52 2011] ::1:39148 GET /myscript.html - Request read  </span></li>
<li><span>[Thu Jul 21 10:48:52 2011] ::1:39149 GET /favicon.ico - Request read </span></li>
</ol>
Copy after login

Example #2 Specify the root directory of the document when starting the web server

<ol>
<li><span><span>$ cd ~/public_html  </span></span></li>
<li><span>$ php -S localhost:8000 -t foo/ </span></li>
</ol>
Copy after login

Terminal display message:

<ol>
<li><span><span>PHP 5.4.0 Development Server started at Thu Jul 21 10:50:26 2011  </span></span></li>
<li><span>Listening on localhost:8000  </span></li>
<li><span>Document root is /home/me/public_html/foo  </span></li>
<li><span>Press Ctrl-C to quit </span></li>
</ol>
Copy after login

If you append a php script file after the startup command line, then 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 will return the corresponding image, but a request for an HTML file will display "Welcome to PHP":

<ol>
<li><span><span><?php  </span></span></li><li><span>// router.php </span><span> </span></li><li><span>if</span><span> (preg_match(</span><span>'/\.(?:png|jpg|jpeg|gif)$/'</span><span>, </span><span>$_SERVER</span><span>[</span><span>"REQUEST_URI"</span><span>])) {  </span></li><li><span>return</span><span> false;    </span><span>// serve the requested resource as-is. </span><span> </span></li><li><span>} </span><span>else</span><span> {  </span></li><li><span>echo</span><span> </span><span>"<p>Welcome to PHP</p>"</span><span>;  </span></span></li>
<li><span>}  </span></li>
<li><span>?> </span></li>
</ol>
Copy after login
<ol><li><span><span>$ php -S localhost:8000 router.php </span></span></li></ol>
Copy after login
Copy after login
Copy after login

Example #4 Determine whether it is being used The built-in web server

adjusts the different behaviors of the same PHP router script in the built-in web server and the production server through program judgment:

<ol><li><span><span><?php  </span></span></li><li><span>// router.php </span><span> </span></li><li><span>if</span><span> (php_sapi_name() == </span><span>'cli-server'</span><span>) {  </span></li><li><span>/* route static assets and return false */</span><span> </span></li><li><span>}  </span></li><li><span>/* go on with normal index.php operations */</span><span> </span></li><li><span>?> </span></span></li></ol>
Copy after login
<ol><li><span><span>$ php -S localhost:8000 router.php </span></span></li></ol>
Copy after login
Copy after login
Copy after login

This built-in web server can recognize some standard MIME type resources, and their extensions are : .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 the web server to correctly handle unsupported MIME file types, do this:

<ol><li><span><span><?php  </span></span></li><li><span>// router.php </span><span> </span></li><li><span>$path</span><span> = </span><span>pathinfo</span><span>(</span><span>$_SERVER</span><span>[</span><span>"SCRIPT_FILENAME"</span><span>]);  </span></li><li><span>if</span><span> (</span><span>$path</span><span>[</span><span>"extension"</span><span>] == </span><span>"ogg"</span><span>) {  </span></li><li><span>header(</span><span>"Content-Type: video/ogg"</span><span>);  </span></li><li><span>readfile(</span><span>$_SERVER</span><span>[</span><span>"SCRIPT_FILENAME"</span><span>]);  </span></li><li><span>}  </span></li><li><span>else</span><span> {  </span></li><li><span>return</span><span> FALSE;  </span></li><li><span>}  </span></li><li><span>?> </span></span></li></ol>
Copy after login
<ol><li><span><span>$ php -S localhost:8000 router.php </span></span></li></ol>
Copy after login
Copy after login
Copy after login

If you want to access the built-in web server remotely, Your startup command needs to be changed to the following:

Example #6 Remote access to this built-in web server

<ol><li><span><span>$ php -S 0.0.0.0:8000 </span></span></li></ol>
Copy after login

So you can remotely access this built-in web server through port 8000

The above has introduced the PHP 54 built-in Web server, including the content of the Web server. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template