PHP5.4 built-in Web server _php instance

Source: Internet
Author: User
Tags mime file
PHP is a scripting language that requires the PHP interpreter to parse and run PHP files. 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, before using PHP, you need to install Apache or IIS, and correctly configure them and PHP integrated parameters. Although this configuration is already very prescriptive and the documentation is very rich, we often have problems installing Apache and PHP integration, and sometimes we just want to test a simple PHP feature and don't want to install and start the Apache service for this.

But according to official documentation, this built-in Web server is only available for development testing and is not recommended for use in production environments. Because this server accepts processing requests in sequence, it cannot be processed concurrently.

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

$ php-s localhost:8000
Then you can access it. When this is started, 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 with the-t parameter.

Example #1 starting a 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 10:43:28 2011Listening on localhost:8000document root Is/home/me/publi C_htmlpress ctrl-c to quit. [Thu Jul 10:48:48]:: 1:39144 get/favicon.ico-request read[thu Jul 10:48:50]:: 1:39146 GET/-Request R Ead[thu Jul 10:48:50]:: 1:39147 get/favicon.ico-request read[thu Jul 10:48:52]:: 1:39148 get/myscript. Html-request Read[thu Jul 10:48:52]:: 1:39149 get/favicon.ico-request Read

example #2 The root directory of the specified 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 attach a PHP script file after the startup 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, the content displayed in the browser will be the result of this script.

example #3 using router scripts

In this example, the request for the picture returns the corresponding image, but the request to the HTML file displays "Welcome to PHP":

<?php//router.phpif (Preg_match ('/\. (?:p ng|jpg|jpeg|gif) $/', $_server["Request_uri"])) {return false;  Serve the requested resource as-is.} else {echo '

Welcome to PHP

";}? >

$ php-s localhost:8000 router.php
Example #4 determine if you are using the built-in Web server

The different behavior of the same PHP router script in the built-in Web server and in the production server is adjusted by program judgment:

<?php//router.phpif (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 a number of standard MIME-type resources, and their extensions are:. css,. gif,. htm,. html,. JPE,. jpeg,. jpg,. js,. png,. svg,. 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 the MIME file types that are not supported 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 boot 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 the built-in Web server remotely via port 8000.

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support the script home.

  • 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.