Home  >  Article  >  Backend Development  >  What is SAPI in PHP? How to achieve? (pictures and text)

What is SAPI in PHP? How to achieve? (pictures and text)

不言
不言Original
2018-09-18 15:13:096660browse

The content of this article is about what is SAPI in PHP? How to achieve? (Pictures and text), it has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

SAPI, I checked it specifically. It is the abbreviation of Server Application Programming Interface, which means server-side application programming interface.

This is the interface provided by the PHP kernel to the outside world to call its services. That is, external systems can call the services of compiling scripts and executing scripts provided by PHP through SAPI. There are many SAPIs implemented in PHP, Cli and Fpm are the more common ones.

From the figure below, you can clearly understand how the external system calls PHP services through SAPI

What is SAPI in PHP? How to achieve? (pictures and text)

Pictures come from the Internet

The following is mainly about how our common Cli, and Fpm work.

Cli

Cli (Command Line Interface), which is the command line interface, is used to execute PHP scripts under the command line, just like Shell, it is to execute PHP Script is the easiest way.

Cli is a single-process mode. It is shut down directly after processing the request. The life cycle goes through module startup, request startup, execute script, request shutdown, and module shutdown. Its execution process is relatively simple, and the key processing process is As follows:

main()-> php_cli_startup()-> do_cli()-> php_module_shutdown()
Fpm

Fpm (FastCGI Process Manager) is a process manager for PHP FastCGI operating mode. From its definition, it can be seen that the core function of Fpm is process management.

FastCGI is a communication protocol between web servers (such as Nginx, Apache) and handlers. It is an application layer communication protocol similar to HTTP.
Note: It is just an agreement!

Fpm is a multi-process model, which consists of a master process and multiple worker processes. The master process will create a socket when it starts, but it will not receive or process requests. Instead, the forked worker child process will complete the reception and processing of requests. That is, the master process manages the worker process, and the worker process is the real processing request.

After startup, Fpm will first perform the SAPI registration operation; then it will enter the module startup stage of the PHP life cycle, during which the MINT hook functions defined by each extension will be called. Then a series of initialization operations will be performed, and finally the master and worker processes will enter different processing links.

The life cycle of the worder process is as follows:

What is SAPI in PHP? How to achieve? (pictures and text)

The picture comes from the Internet

The main experiences of its life cycle These stages: waiting for requests, parsing requests, request initialization, executing PHP scripts, and closing requests.

The master process mainly manages the worder process in three different ways, namely static mode (static), dynamic mode (dynamic), and on-demand mode (ondemand). The specific mode to be used can be specified through pm in the conf configuration

The above is the detailed content of What is SAPI in PHP? How to achieve? (pictures and text). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn