Php running modes CLI, CGI, FastCGI, mod_php, fastcgimod_php_PHP tutorial

Source: Internet
Author: User
Tags sapi website server
Several php running modes are CLI, CGI, FastCGI, mod_php, and fastcgimod_php. Several php running modes: CLI, CGI, FastCGI, mod_php, fastcgimod_php1, and CLI: Are command lines. for example, you can type the command in the console or shell: php-findex.php and then get several php running modes CLI, CGI, FastCGI, mod_php, fastcgimod_php

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

php -f index.php

Then obtain the output.

2. CGI: different statements and understandings are as follows:

Public Gateway Interface (Common Gateway Interface) is a tool used by the HTTP server to "talk" with programs on your or other machines. its programs must run on the network server. In the server environment, a standard interface is provided for the program. through this interface, the program can perform some operations on the information exchanged between the server and the client. "Program" language is not required. The program operates the interface. To support CGI, the server must provide the environment variables required by CGI, or there is something else.

The protocol between the HTTP Server and an independent process sets the HTTP Request Header as the environment variable of the process, and the HTTP Request body as the standard input of the process, the standard output of the process is that the HTTP Response includes the Header and body.

This Web server usesUNIX shell environment variablesTo save the parameters passed from the Web server, and then generate a CGI-runningIndependent process.

Programs written in different languages can interact with web servers as long as they comply with cgi standards.

When running in CGI mode, the web server transfers user requests to an independent PHP process in the form of messages, and there is no subordination between PHP and web services.

Personal Understanding: CGI specifies the rules for php to communicate with the web server, which is equivalent to executing response = exec ("php-f index. php-url = xxx-cookie = xxx-xxx = xxx ").

The difference between CGI and CLI can be viewed in the official documentation said quite well: http://php.net/manual/zh/features.commandline.php

This article describes the significant differences between CGI and CLI:

The following areCLI SAPIAnd other CLISAPISignificant differences between modules:

  • AndCGI SAPIDifferent, the output does not have any header information.

    AlthoughCGI SAPITo cancel the HTTP header informationCLI SAPIThere is no similar method to enable the output of HTTP header information.

    CLI starts in quiet mode by default, but to ensure compatibility, The-q and -- no-header parameters are retained for backward compatibility, so that the old CGI script can be used.

    At runtime, 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 ).

    When an error occurs, the error message of plain text is output (not in HTML format ).

3. FastCGI: CGI has many disadvantages. every time a request is received, it must be processed by a process fork and only one request can be received to make one response. The process ends after the request ends. FastCGI is started in advance. as a cgi management server, FastCGI starts a series of sub-processes in advance to wait for processing, and then waits for requests sent by the web server, once the request is received, it is handled by the sub-process. as a result, it is much faster to start cgi after the request is received. FastCGI uses the process/thread pool to process a series of requests. These processes/threads are managed by the FastCGI server, rather than the Web server. When a request comes in, the Web server sends the environment variable and the page request to the FastCGI process through a Socket persistent connection. FastCGI is like a resident CGI, which can always be executed, when a request arrives, it does not take time to fork a process for processing (this is the fork-and-execute mode that is criticized by CGI ). Because it is only a communication protocol, it also supports distributed computing, that is, the FastCGI program can be executed on a host other than the website server and receive requests from other website servers.

FastCGI wholeProcess:

    1. Load FastCGI process manager when the Web server starts

    2. FastCGI initializes itself, starts multiple CGI interpreter processes (multiple php-cgi are visible), and waits for requests from the Web server

    3. When a Web server is requested, the Web server sends a socket request to the FastCGI Process Manager. The FastCGI process manager selects and connects to a CGI interpreter, the Web server sends CGI environment variables and standard input to the FastCGI sub-process php-cgi

    4. After the FastCGI sub-process processes the request, it returns the standard output and error from the same connection to the Web server. when the FastCGI sub-process ends, the request ends. The FastCGI sub-process then waits for processing the next connection from the FastCGI Process Manager. in CGI mode, php-cgi exits here.

Php-fpm: FastCGI Process Manager of PHP

4. mod_php: apache's php module, which uses PHP as the sub-process control of web-server. the most obvious example is that in CGI mode, if PHP is modified. the INI configuration file takes 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 started as a module of apache. Therefore, php is only read when apache is started. ini configuration file and load the extension module. during apache running, the extension module will not be read or loaded again.

Apache working mode How prefork works

A separate control process (parent process) is responsible for generating child processes that are used to listen for requests and respond. Apache always tries to keep some standby (spare) or idle sub-processes to meet the upcoming requests. In this way, the client does not have to wait for the sub-process to generate before obtaining the service. In Unix systems, the parent process is usually run as root to stick port 80, while the child process generated by Apache is usually run as a low-privilege user. User and Group commands are used to configure low-privilege users of sub-processes. A user running a sub-process must have the read permission on the content of the sub-process, but must have as few permissions as possible for resources other than the service content.

Working Principle of worker

The number of threads that each process can possess is fixed. The server increases or reduces the number of processes based on the load. A separate control process (parent process) is responsible for the establishment of child processes. Each sub-process can establish a ThreadsPerChild number of service threads and a listening thread. the listening thread monitors access requests and passes them to the service thread for processing and response. Apache always tries to maintain a standby (spare) or idle service thread pool. In this way, the client can be processed without waiting for the creation of a new thread or process. In Unix, in order to be able to bind port 80, the parent process is generally started as root, and then Apache creates sub-processes and threads with lower permissions. The User and Group commands are used to configure the permissions of Apache sub-processes. Although a sub-process must have the read permission on the content provided by it, it should be given as few privileges as possible. In addition, unless suexec is used, the permissions configured for these commands will be inherited by CGI scripts.

My understanding: This mode embeds php into apache, which is equivalent to adding the php file parsing function to apache.

CLI: is the command line, for example, you can type the command in the console or shell: php-findex.php and then get to lose...

Related Article

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.