PHP5.4 built-in Web server _php instance

Source: Internet
Author: User
Tags mime file php script unsupported

PHP is a scripting language that requires a PHP interpreter to parse the running PHP file. When PHP is used as a CGI service Web request, it needs to be embedded in some kind of Web server, most commonly integrated into Apache or IIS, that is, you need to install Apache or IIS before using PHP, and configure them correctly with the parameters of the PHP integration. Although this configuration is already very standard, the documentation is very rich, but we often have problems installing Apache and PHP integration, and sometimes we just want to test a simple PHP features, do not want to install, start the Apache service.

However, according to official documents, this built-in Web server only provides development testing and is not recommended for use in production environments. Because this server accepts processing requests sequentially, it cannot be processed concurrently.

This built-in Web server is very handy to use, you only need to execute the following command:

$ php-s localhost:8000
And then you can access it. After this starts, the default Web service directory is the current directory where the command is executed, and if you do not want to use the current directory, you need to specify it using the-t parameter.

Example #1 start a Web server

$ CD ~/public_html
$ php-s localhost:8000
Terminal Output Information:

PHP 5.4.0 Development Server started at Thu June 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 June 10:43:28 listening on
localhost:8000
Document root Is/hom E/me/public_html Press
Ctrl-c to quit.
[Thu June 10:48:48]:: 1:39144 get/favicon.ico-request Read
[Thu June 10:48:50]:: 1:39146 get/-requ  EST read
[Thu June 10:48:50]:: 1:39147 get/favicon.ico-request Read
[Thu June 21 10:48:52 2011]:: 1:39,148 Get/myscript.html-request read
[Thu June 10:48:52]:: 1:39149 get/favicon.ico-request Read


example #2 Specify the root directory of a document when starting a Web server

$ CD ~/public_html
$ php-s localhost:8000-t foo/
Terminal Display information:

PHP 5.4.0 Development Server started at Thu June 21 10:50:26 2011
Listening on localhost:8000
Document Root Is/home/me/public_html/foo
Press Ctrl-c to quit
If you attach a PHP script file after the start command line, the file will be treated as a "router" script. This script will be responsible for all HTTP requests, and if the script returns False when it executes, the requested resource will return normally. If it is not false, what is displayed in the browse will be what the script produces.

example #3 using a router script

In this example, the request to the picture will return the corresponding picture, but the request to the HTML file will display "Welcome to PHP":

<?php
//router.php
if (Preg_match ('/\. (?:p ng|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 if the built-in Web server is being used

Adjust the different behavior of the same PHP router script between the built-in Web server and the production server by program judgment:

<?php
//router.php
if (php_sapi_name () = = ' Cli-server ') {/
* route static assets and return false */
} /* Go in with
normal index.php operations * *
?>

$ php-s localhost:8000 router.php
This built-in Web server recognizes some standard MIME-type resources, and their extensions include:. css,. gif,. htm,. html,. JPE,. jpeg,. jpg,. js,. png,. svg, and. txt. Extension of. htm and. svg to support is supported after PHP 5.4.4.

example #5 handling unsupported file types

If you want this Web server to handle unsupported MIME file types correctly, 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 be able to access this 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

$ php-s 0.0.0.0:8000
This allows you to access this built-in Web server remotely via 8000 ports.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.