Table of Contents
PHP Architecture
SAPI Introduction
CGI
FPM (FastCGI)
APACHE2HANDLER
CLI
SAPI process
SAPI
Home Backend Development PHP7 What is the life cycle of PHP7

What is the life cycle of PHP7

May 10, 2021 am 09:19 AM
php7 life cycle

This article will introduce you to the life cycle of PHP7. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

What is the life cycle of PHP7

PHP Architecture

SAPI Introduction

Definition: Server Application Programming InterfaceServer Application Programming port. It is the interface for PHP to interact with other applications. PHP scripts can be executed in many ways, through the web server, directly on the command line, or embedded in other programs. SAPI provides an interface for external communication. Common SAPIs include: cgi, fast-cgi, cli, Apache module dll, etc.

  • CGI

CGI is the common gateway interface (common gateway interface). It is a program. In layman's terms, CGI is like a bridge. , connects the web page and the execution program in the WEB server, it passes the instructions received by the HTML to the server's execution program, and then returns the results of the server's execution program to the HTML page. CGI is extremely cross-platform and can be implemented on almost any operating system.

When the CGI method encounters a connection request (user request), it must first create a cgi sub-process, activate a CGI process, then process the request, and end the sub-process after processing. This is the fork-and-execute pattern. Therefore, a server using CGI will have as many CGI sub-processes as there are connection requests. Repeated loading of sub-processes is the main reason for low CGI performance. When the number of user requests is very large, a large amount of system resources such as memory, CPU time, etc. will be occupied, resulting in low performance.

  • FPM (FastCGI)

fast-cgi is an upgraded version of cgi, FastCGI is like a long-live type CGI can be executed all the time. As long as it is activated, it will not take time to fork every time. PHP uses PHP-FPM (FastCGI Process Manager), the full name of PHP FastCGI Process Manager, for management.

The FastCGI process manager (IIS ISAPI or Apache Module) is loaded when the Web Server starts. The FastCGI process manager initializes itself, starts multiple CGI interpreter processes (visible multiple php-cgi) and waits for connections from the Web Server.

When a client request reaches the Web Server, the FastCGI process manager selects and connects to a CGI interpreter. The web server sends CGI environment variables and standard input to the FastCGI subprocess php-cgi.

After the FastCGI sub-process completes processing, it returns standard output and error information to the Web Server from the same connection. When the FastCGI child process closes the connection, the request is processed. The FastCGI child process then waits for and handles the next connection from the FastCGI process manager (running in the Web Server). In CGI mode, php-cgi exits at this point.

In the above case, you can imagine how slow CGI is usually. Every web request to PHP must reparse php.ini, reload all extensions, and reinitialize all data structures. With FastCGI, all of this happens only once, when the process starts. An added benefit is that persistent database connections work.

  • APACHE2HANDLER

PHP as an Apache module, the Apache server pre-generates multiple process copies to reside in the memory after the system starts. When a request occurs, these idle sub-processes are immediately used for processing, so that there is no delay caused by generating sub-processes. These server copies do not exit immediately after processing an HTTP request, but stay in the computer waiting for the next request. The response to client browser requests is faster and the performance is higher.

  • CLI

cli is the command line running mode of php. You often use it, but you may not notice it (for example: we Under Linux, "php -m" is often used to find out which extensions PHP has installed, which is the PHP command line running mode.

SAPI process

Structure:

struct _sapi_module_struct { 
      char *name; 
      char *pretty_name;
      int (*startup)(struct _sapi_module_struct *sapi_module); 
      int (*shutdown)(struct _sapi_module_struct *sapi_module); 
      int (*activate)(void); 
      int (*deactivate)(void); 
      size_t (*ub_write)(const char *str, size_t str_length); 
      void (*flush)(void *server_context); 
     …
}
Copy after login

Calling API :

Called when the module starts:

int (*startup)(struct _sapi_module_struct *sapi_module);
Copy after login

Called when the module ends:

int (*shutdown)(struct _sapi_module_struct *sapi_module);
Copy after login

Called when processing the request:

int (*activate)(void);
Copy after login
Copy after login

Called after processing the request:

int (*activate)(void);
Copy after login
Copy after login

SAPI

structure corresponding to CGI mode:

##CLI life cycle:

SAPI

structure corresponding to FPM mode:

Life cycle of FPM mode:

# Recommended learning:

php video tutorial

The above is the detailed content of What is the life cycle of PHP7. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

vue3 changed several life cycle functions vue3 changed several life cycle functions Jan 13, 2023 pm 05:57 PM

vue3 changed 4 life cycle functions. The Vue3 combined API cancels the beforeCreated and created hook functions and uses the step hook instead, and this cannot be used in it. The hook functions for component destruction in Vue3 have been changed from destroyed and beforeDestroy to beforeUnmount and unmounted.

How to deal with destruction and life cycle management of C++ function pointers? How to deal with destruction and life cycle management of C++ function pointers? Apr 17, 2024 pm 05:48 PM

In C++, function pointers require proper destruction and life cycle management. This can be achieved by manually destructing the function pointer and releasing the memory. Use smart pointers, such as std::unique_ptr or std::shared_ptr, to automatically manage the life cycle of function pointers. Bind the function pointer to the object, and the object life cycle manages the destruction of the function pointer. In GUI programming, using smart pointers or binding to objects ensures that callback functions are destructed at the appropriate time, avoiding memory leaks and inconsistencies.

The servlet life cycle is divided into several stages The servlet life cycle is divided into several stages Feb 23, 2023 pm 01:46 PM

The Servlet life cycle refers to the entire process from creation to destruction of a servlet, which can be divided into three stages: 1. Initialization stage, calling the init() method to initialize the Servlet; 2. Running stage (processing requests), the container will Request to create a ServletRequest object representing an HTTP request and a ServletResponse object representing an HTTP response, and then pass them as parameters to the service() method of the Servlet; 3. Destruction phase.

How to solve the problem when php7 detects that the tcp port is not working How to solve the problem when php7 detects that the tcp port is not working Mar 22, 2023 am 09:30 AM

In php5, we can use the fsockopen() function to detect the TCP port. This function can be used to open a network connection and perform some network communication. But in php7, the fsockopen() function may encounter some problems, such as being unable to open the port, unable to connect to the server, etc. In order to solve this problem, we can use the socket_create() function and socket_connect() function to detect the TCP port.

What should I do if the plug-in is installed in php7.0 but it still shows that it is not installed? What should I do if the plug-in is installed in php7.0 but it still shows that it is not installed? Apr 02, 2024 pm 07:39 PM

To resolve the plugin not showing installed issue in PHP 7.0: Check the plugin configuration and enable the plugin. Restart PHP to apply configuration changes. Check the plugin file permissions to make sure they are correct. Install missing dependencies to ensure the plugin functions properly. If all other steps fail, rebuild PHP. Other possible causes include incompatible plugin versions, loading the wrong version, or PHP configuration issues.

Lifecycle functions in Vue3: Quickly master the lifecycle of Vue3 Lifecycle functions in Vue3: Quickly master the lifecycle of Vue3 Jun 18, 2023 am 08:20 AM

Vue3 is currently one of the most popular frameworks in the front-end world, and the life cycle function of Vue3 is a very important part of Vue3. Vue3's life cycle function allows us to trigger specific events at specific times, enhancing the high degree of controllability of components. This article will explore and explain in detail the basic concepts of Vue3's life cycle functions, the roles and usage of each life cycle function, and implementation cases, to help readers quickly master Vue3's life cycle functions. 1. Vue3’s life cycle function

How to install mongo extension in php7.0 How to install mongo extension in php7.0 Nov 21, 2022 am 10:25 AM

How to install the mongo extension in php7.0: 1. Create the mongodb user group and user; 2. Download the mongodb source code package and place the source code package in the "/usr/local/src/" directory; 3. Enter "src/" directory; 4. Unzip the source code package; 5. Create the mongodb file directory; 6. Copy the files to the "mongodb/" directory; 7. Create the mongodb configuration file and modify the configuration.

How to control the life cycle of Golang coroutines? How to control the life cycle of Golang coroutines? May 31, 2024 pm 06:05 PM

Controlling the life cycle of a Go coroutine can be done in the following ways: Create a coroutine: Use the go keyword to start a new task. Terminate coroutines: wait for all coroutines to complete, use sync.WaitGroup. Use channel closing signals. Use context context.Context.

See all articles