First, need to make clear a few noun concepts

1. The Common Gateway Interface (CGI) is the standard of external programs for Web server runtime. Programs written according to CGI can extend server functions. CGI applications can interact with browsers and communicate with external data sources, such as a database server, through data apis to retrieve data from the database server. After formatting it as an HTML document and sending it to the browser, you can also put the data you get from the browser into a database. Almost all servers support CGI, which can be written in any language, including the popular C, C ++, Java, VB, and Delphi. So CGI is a protocol, not a process. **CGI exists to allow users to access dynamic programs executed on the server through a browser; CGI is the standard for transferring data between Web servers and CGI programs.

Server-side CGI programs receive information in three ways: environment variables, command lines, and standard input.

The command line:

Standard input: Method=POST of the form, the form encoding information is passed to the CGI through standard input.

** Environment variables: ** Form encoding information is passed through the environment variable QUERY_STRING.

How the browser passes data:

The CGI program checks the environment variable REQUEST_METHOD to determine whether POST is used and whether standard input is used.

**GET:** Can be used to send data smaller than 1024 bytes

**URL+? : ** belongs to GET mode

! [](https://pic4.zhimg.com/80/v2-6ae806cbcd313fd437de3cee330e43b7_720w.png)

How CGI works

! [](https://pic1.zhimg.com/80/v2-343ac3a36e6d9821cae43ae3359468a8_720w.jpg)

Traditional CGI program execution process

2. FastCGI (Fast Common Gateway Interface) is the optimization and upgrade of CGI.

FastCGI development:

The main disadvantage of the traditional CGI interface approach is poor performance, because every time the HTTP server encounters a dynamic program, it needs to restart the script parser (php-CGI) to perform the parsing, and then the results are returned to the HTTP server. It is almost unavailable when dealing with high concurrency.

The FastCGI interface uses C/S structure to separate the HTTP server from the script parsing server and start one or more script parsing daemons on the script parsing server. Each time the HTTP server encounters a dynamic program, it can deliver it directly to the FastCGI process for execution, and then return the results to the browser. This approach allows the HTTP server to exclusively handle static requests or return the results of the dynamic script server to the client, greatly improving the overall performance of the application system.

One of the differences between FastCGI and traditional CGI is that instead of executing CGI programs directly, the Web server interacts with the FastCGI responder (FastCGI process manager) through sockets. The Web server needs to send CGI interface data wrapped in a FastCGI compliant package to the FastCGI responder program. Because the FastCGI process manager is socket-based communication, it is also distributed, with the Web server and CGI responder server deployed separately.

! [](https://pic2.zhimg.com/80/v2-8e767578a90b900e5e3c16bcd77f7afd_720w.jpg)

Based on the FastCGI process manager program to execute the process

3. Php-cgi is a PHP interpreter. It is a CGI program that can only parse requests, not manage processes.

4. Php-fpm (FastCGI Process Manager: FastCGI Process Manager) is a program used to schedule and manage php-Fastcgi processes. Prior to PHP5.3.3, it was a patch package for the PHP kernel, which integrated php-FPM in later versions.

Nginx+FastCGI

Nginx does not support direct calls or parsing of external programs. All external programs (including PHP) must be called through the FastCGI interface. The FastCGI interface is a Socket under Linux (this socket can be a file socket or an IP socket).

To call a CGI program, you also need a FastCGI wrapper (wrapper can be a program used to start another program) bound to a fixed socket, such as a port or file socket. When Nginx sends a CGI request to the socket, the Wrapper receives the request through the FastCGI interface and spawns a new thread that calls the interpreter or external program to process the script and read the returned data. The Wrapper then passes the data back to Nginx through the FastCGI interface along the fixed socket. Finally, Nginx sends the returned data back to the client, and that’s how Nginx+FastCGI works.

! [](https://pic2.zhimg.com/80/v2-4ed722bc197db1b73f27465b53939015_720w.jpg)

3, phP-FPM process management

Fastcgi is a protocol, not a process. Php-fpm implements this protocol and is the process manager for Fastcgi programs (php-CGI).

First, start a master, parse the configuration file, initialize the execution environment, and then start multiple workers. When a request comes in, the master passes it to a worker and is immediately ready to accept the next request. In this way, repetitive work is avoided and efficiency is naturally high. And when workers are not enough, the master can pre-start several workers according to the configuration and wait. Of course, when there are too many idle workers, some of them will be stopped, which improves performance and saves resources. This is how phP-FPM manages processes. Php-fpm is managed by php-CGI.

4. Php-fpm implements smooth restart

1. WorkerMan smoothly restarts

WorkerMan is divided into main process and sub-process. The main process is responsible for monitoring the sub-process, and the sub-process is responsible for receiving the connection and request data from the client, doing the corresponding processing and returning data to the client. When the business code is updated, we can update the code simply by updating the child process.

When the main WorkerMan process receives a smooth restart signal, it sends a safe exit signal to one of its children. When the process exits, the main process creates a new child that has loaded the new PHP code. The main process then sends a stop command to another old process, and the process is restarted one by one until all the old processes have been replaced.

! [](https://pic3.zhimg.com/80/v2-a4d354e9ded2d62100430b5f30c456da_720w.png)

2. Nginx restarts smoothly

Nginx processes are divided into the master master process and the work process. The master process mainly manages the event signal to receive and distribute, all requests are processed by the Work process and return results. Nginx smooth restart or reload configuration file upgrade, the first is to send restart or reload configuration file signal to the master. The master then tells all work processes to stop accepting new requests, starts a new work process, and finally tells the old work process to exit gracefully.

3. PHP-FPM smooth restart

The processing mechanism of phP-FPM is that the new worker uses the new configuration, and the existing worker can rest after processing the work in hand, so as to smooth the excess through this mechanism.

5. Php-fpm

Php-fpm (PHP FastCGI Process Manager) PHP FastCGI Process Manager, used to manage the PHP Process pool, used to accept requests from web servers.

Php-fpm provides a better way to manage PHP processes, effectively control memory and processes, and smooth out overloaded PHP configurations.

1. Why phP-FPM

FPM appears because php-fastCGI appears, in order to better manage php-FastCGI implementation of a program.

2. What is php-fastcgi

Php-fastcgi is just a CGI program that only parses PHP requests and returns results, not manages (hence php-fpm).

3. Why not php-CGI

There was a php-cgi before php-FastCGI existed, but it was inefficient and was replaced by php-Fastcgi.

4. What’s the difference between FastCGI and CGI

This is a big difference. When a service Web-server (Nginx) dispatches a request, it knows by matching the suffix that the request is a dynamic PHP request and forwards it to PHP.

In cgi s, conservative, always after coming over a request to read the PHP. The basic configuration information in ini, initialize the execution environment, every time to create a process, reading configuration, initialize the environment, return data, exit process, over time, start the process of work become boring I feel very tired.

When PHP came to the age of 5, people hated this way of working. People who wanted to be lazy thought desperately, can I have the CGI start a master process at a time, have it read the configuration only once, and then start multiple workers when a request comes in, Pass the master to the worker to avoid repeated work. Fastcgi was born.

5. Fastcgi is so good, what if the startup worker runs out

When the worker is not enough, the master will dynamically start the worker through the information in the configuration and retrieve the worker when it is idle

6. Still not sure what phP-FPM is?

Is to manage the start of a master process and multiple worker processes.

Php-fpm creates a main process that controls when and how HTTP requests are forwarded to one or more child processes for processing. The PHP-FPM main process also controls when PHP child processes are created (to handle more traffic from the Web application) and destroyed (when the child process has been running for too long or is no longer needed). Each process in the PHP-FPM process pool lasts longer than a single HTTP request and can handle 10, 50, 100, 500, or more HTTP requests.

Install phP-FPM

PHP has incorporated phP-FPM into PHP core code since 5.3.3. So phP-FPM does not require a separate download and installation. To support php-fpm in PHP, simply add –enable-fpm when compiling the PHP source code.

7. Php-fpm configuration

In Centos, the main configuration file of php-fpm is /etc/php7/php-fpm.conf.

Specifies a period of time when the specified child process is invalid, and php-fpm restarts:

! [](https://pic3.zhimg.com/80/v2-a7bc3b7b97a531bdf6b7238276b31dd6_720w.jpg)
! [](https://pic2.zhimg.com/80/v2-9e7e20c6982f58a44bb7894581d954d1_720w.jpg)
! [](https://pic1.zhimg.com/80/v2-51e38c874b9850f6901ede1dd0ed8f60_720w.png)

PHP: PHP open Source community, or visit: PHP open Source community

A collection of essential PHP technical articles – PHP framework

Microservices Architecture – a collection of essential PHP technical articles

Distributed Architecture is a collection of essential PHP technical articles

A collection of essential PHP technical articles – High Concurrency Scenarios

Elite PHP technical article collation collection – database