PHP's several operating modes CLI, CGI, FastCGI, mod_php,fastcgimod_php_php tutorial

Source: Internet
Author: User
Tags apache php sapi

PHP's several operating modes CLI, CGI, FastCGI, mod_php,fastcgimod_php


1, CLI: is the command line, for example, you can type the command in the console or the shell:

Php-f index.php

Then get the output

2, CGI: The following are different statements and understanding

Public Gateway Interface (Common Gateway Interface), an HTTP server is a tool for "talking" to programs on your or other machines, and its programs must be running on a Web server. In a server environment, the "program" provides a standard interface through which the "program" can do something about the information exchanged between the server and the client. The language of the "program" is not required. The program operates on the interface. The server wants to support CGI by providing the environment variables required in the CGI, or something else.

HTTP server and a separate process protocol, the HTTP request header is set to the environment variable of the process, the body of the HTTP request is set to the standard input of the process, and the standard output of the process is the HTTP Response includes header and text.

The Web server uses the UNIX shell environment variable to hold parameters passed from the Web server, and then generates a separate process to run the CGI.

Programs written in different languages can interact with the Web server as a CGI program as long as they conform to the CGI standard

When run in CGI, Web server transfers the user request to the PHP standalone process as a message, with no dependencies between PHP and the Web service.

Personal Understanding: CGI Specifies the rules for PHP to communicate with Web server, equivalent to executing response = EXEC ("Php-f index.php-url=xxx-cookie=xxx-xxx=xxx").

The difference between CGI and CLI can be seen in official documents that's pretty good: http://php.net/manual/zh/features.commandline.php

The notable differences between CGI and CLI are described in this article:

The following are notable differences compared to the CLI SAPI and other CLI SAPI modules:

    • Unlike CGI SAPI , its output has no header information.

      Although CGI SAPI provides a way to cancel the HTTP header information, there is no similar method in CLI SAPI to turn on the output of the HTTP header information.

      The CLI starts in quiet mode by default, but for compatibility, the-Q and --no-header parameters remain for backwards compatibility, making it possible to use old CGI scripts.

      At run time, the working directory is not changed to the current directory of the script (you can use the -C and --no-chdir parameters to be compatible with CGI mode).

      Error message (non-HTML format) for outputting plain text when an error occurs.

3, FASTCGI:CGI has many shortcomings, each request will fork a process processing, can only receive a request to make a response. The process ends after the request is finished. And fastcgi will start up in advance, as a CGI Management Server exists, pre-start a series of sub-processes to wait for processing, and then wait for the Web server to send a request, once the request is received by the child process processing, so that it does not need to start the CGI after the request, it will be much faster. FASTCGI uses a process/thread pool to process a series of requests. These processes/threads are managed by the FASTCGI server, not the Web server. When a request comes in, the Web server passes the environment variable and the page request through a long socket connection to the fastcgi process. FastCGI is like a resident CGI, which can be executed all the time, and it will not take a moment to fork a process when the request arrives (this is the Fork-and-execute mode that CGI has criticized for being a bit). Because it is just a communication protocol, it also supports distributed operations where the FASTCGI program can execute and accept requests from other Web servers on hosts other than the Web server

FastCGI the entire process :

      1. Loading the FASTCGI process Manager when Web server starts

      2. FASTCGI itself initialization, starting multiple CGI interpreter processes (visible multiple php-cgi) and waiting for requests from the Web server

      3. When requesting Web server, Web server fastcgi the process manager through the socket request, the FASTCGI process manager selects and connects to a CGI interpreter, the web The server sends CGI environment variables and standard input to the FASTCGI child process php-cgi

      4. After the FASTCGI process requests are completed, the standard output and errors are returned from the same connection to the Web server, and the request ends when the fastcgi child process finishes. The fastcgi child process then waits to process the next connection from the FASTCGI process Manager, and in CGI mode, the php-cgi exits.

php-fpm:P HP's fastcgi process Manager

4, mod_php: That is, Apache PHP module, the PHP as a web-server sub-process control, there is a subordinate relationship between the two. The most obvious example is that in CGI mode, if you modify the php.ini configuration file, you can take effect without restarting the Web service. In module mode, you need to restart the Web service. Running PHP in mod_php mode means that PHP is launched as a module of Apache, so only the php.ini configuration file is read and the extension is loaded when Apache is started, and the extension is no longer read and loaded during Apache runtime.

Apache's working mode How the Prefork works

A separate control process (the parent process) is responsible for generating child processes that are used to listen for requests and respond. Apache always tries to keep some spare (spare) or idle child processes in the process of meeting incoming requests. This eliminates the need for the client to wait for the child process to be generated before the service is received. In Unix systems, the parent process typically runs as root for 80 ports, and Apache-generated child processes are typically run with a low-privileged user. User and group directives are used to configure low-privileged users of child processes. The user who runs the child process must have read access to the content he serves, but must have as few permissions as possible for resources other than the content of the service.

How the Worker Works

The number of threads that each process can have is fixed. The server increases or decreases the number of processes depending on the load. A separate control process (parent process) is responsible for the creation of child processes. Each subprocess can establish a threadsperchild number of service threads and a listener thread that listens to the access request and passes it to the service thread for processing and answering. Apache always tries to maintain a standby (spare) or free service thread pool. This way, the client does not have to wait for new threads or new processes to be established to be processed. In Unix, in order to be able to bind port 80, the parent process is typically started as root, and then Apache creates child processes and threads with lower-privileged users. The user and group directives are used to configure permissions for the Apache child process. Although the child process must have read access to the content it provides, it should give him less privileges as much as possible. Also, unless suEXEC is used, the permissions configured by these directives will be inherited by the CGI script

Personal Understanding: This model embeds PHP into Apache, which is equivalent to the ability to parse PHP files into Apache.

http://www.bkjia.com/PHPjc/1109844.html www.bkjia.com true http://www.bkjia.com/PHPjc/1109844.html techarticle PHP Operating Modes CLI, CGI, FastCGI, mod_php,fastcgimod_php 1, CLI: is the command line, for example, you can type a command in the console or in the shell: php-findex.php then get lost ...

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