PHP5.4 built-in web server and php5.4 built-in web server

Source: Internet
Author: User
Tags mime file unsupported

PHP5.4 built-in web server and php5.4 built-in web server

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 2011Listening on localhost:8000Document root is /home/me/public_htmlPress 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 #2Specify 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 #3Use 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 ":

<?php// router.phpif (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 check whether a built-in web server is used

Adjust the different behaviors of the same PHP router script on the built-in Web server and on the production server through 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 can recognize some standard MIME-type resources. Their extensions include :. css ,. gif ,. htm ,. html ,. jpe ,. jpeg ,. jpg ,. js ,. png ,. svg, and. txt. Extension to .htm and. svg is supported only after PHP 5.4.4.

Example #5Process unsupported file types

If you want the Web server to correctly process unsupported MIME file types, do the following:

<?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, change your startup command to the following:

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

$ Php-S 0.0.0.0: 8000
In this way, you can remotely access this built-in web server through port 8000.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.