PHP 5.4 built-in Web server

Source: Internet
Author: User

 

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:

 
  1. $ 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

 
  1. $ cd ~/public_html  
  2. $ Php-S localhost: 8000

Terminal output information:

 
  1. PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011  
  2. Listening on localhost: 8000
  3. Document root is/home/me/public_html
  4. 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:

 
  1. PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011  
  2. Listening on localhost: 8000
  3. Document root is/home/me/public_html
  4. Press Ctrl-C to quit.
  5. [Thu Jul 21 10:48:48 2011]: 1: 39144 GET/favicon. ico-Request read
  6. [Thu Jul 21 10:48:50 2011]: 1: 39146 GET/-Request read
  7. [Thu Jul 21 10:48:50 2011]: 1: 39147 GET/favicon. ico-Request read
  8. [Thu Jul 21 10:48:52 2011]: 1: 39148 GET/myscript.html-Request read
  9. [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

 
  1. $ cd ~/public_html  
  2. $ Php-S localhost: 8000-t foo/

Terminal display information:

 
  1. PHP 5.4.0 Development Server started at Thu Jul 21 10:50:26 2011  
  2. Listening on localhost: 8000
  3. Document root is/home/me/public_html/foo
  4. 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 #3 use a 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 ":

 

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.