role

PHP – FPM (PHP FastCGI Process Manager) : PHP FastCGI Process Manager, to manage PHP software Process pool, to accept the request of the web server. Php-fpm provides better management of PHP processes, effective control of memory and processes, and smooth overloading of PHP configurations.

(1). Why does PHP-fpm appear

FPM is all about PHP-FastCGI. PHP – FastCGI for good management of a program

(2). What is PHP-fastCGI

Php-fastcgi is just a CGI program that only parses the PHP request and returns the result, not manages it (hence phP-fpm).

(3) Why not call it PHP-CGI

There was a phP-CGI before PHP-FastCGI, but it was inefficient and was replaced by PHP-FastCGI.

(4) What’s the difference between fastCGI and CGI?

This makes a big difference, folks. When a service web-server(nginx) distributes a request, it knows by matching the suffix that the request is a dynamic PHP request and forwards the request 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 5, people were disgusted with this way of working, and lazy people were desperately wondering, can I have the CGI start one master process at a time, let it read the configuration once, and then start multiple worker processes, when a request comes in, Pass the master to the worker so as to avoid repeated work. Hence fastCGI.

(5) FastCGI is so good, how to use up the startup worker? When the worker is insufficient, the master will dynamically start the worker through the information in the configuration, and can recall the worker when it is idle

(6) Still don’t understand what PHP-fpm is? It is to manage the program that starts 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 MAIN PHP-Fpm process also controls when PHP subprocesses are created (to handle more traffic for Web applications) and when they are destroyed (the subprocesses are running too long or are no longer needed). Each process in the PHP-Fpm process pool lives longer than a single HTTP request and can handle 10, 50, 100, 500, or more HTTP requests.

The installation

PHP has incorporated PHP-Fpm into the core PHP code since 5.3.3. So PHP-Fpm does not require a separate download and installation. For PHP to support PHP-fpm, you simply need to bring –enable-fpm with you when compiling the PHP source code.

Global configuration

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

Php-fpm restarts if the specified child process is invalid for a specified period of time:

# The main phP-fpm process will gracefully restart if more than this number of phP-fpm subprocesses fail within a specified period of time. # Set emergency_restart_threshold = 10. emergency_restart_interval = 1mCopy the code

Configuring a Process Pool

The rest of the PHP-fpm configuration file is an area called Pool Defintions. The configuration user in this area sets each PHP-Fpm process pool. The PHP-Fpm process pool is a set of related PHP child processes. Usually a PHP application has its own process pool ==.

Centos introduces the process pool definition file at the top of the phP-fpm main configuration file:

include=/etc/php7/php-fpm.d/*.conf
Copy the code

www.conf is the default configuration file for the PHP-fpm process pool.

User = nobody # System user who owns the processes in the PHP-fpm process pool. The user name that is not root to set this setting to. Group = nobody # The system user group that owns the processes in the PHP-fpm process pool. To set this setting to the name of the user group to which the non-root user of the application belongs. Listen =[::]]:9000 # PHp-fpm process pool listens to IP address and port number, php-fpm will only accept nginx incoming requests from here. Listen-allowed clients =127.0.0.1 # IP addresses (one or more) that can send requests to this PHP-fpm process pool. Pm.max children =51 # This setting sets the maximum number of processes in the PHP-fpm pool at any point in time. There is no absolute value for this setting. You should test your PHP application to determine how much memory each PHP process needs, and then set this setting to the total number of PHP processes available on the device. For most small to medium sized PHP applications, each PHP process uses between 5 and 15MB of memory (the amount may vary). Assuming that we use the device to allocate 512MB of free memory for this PHP-Fpm process pool, we can set this setting to (512MB total memory) /(10MB per process) = 51 processes. .Copy the code

Edit save, restart phP-fpm main process:

sudo systemctl restart php-fpm.service
Copy the code

For details about how to configure the PHP-fpm process pool, see php.net/manual/inst…

Refer to the Company development environment

The configuration of the test environment is as follows:

Group = nobody LISTEN = [::]:9000 # IP address and port number, Chdir = /app # select * from '/' where '/' = '/' where '/' = '/' where '/' = '/' where '/' = '/' where '/' = '/' where '/' = '/' Dynamic: The number of child processes is dynamic, and the number of child processes is dynamic. The number of servers is based on the following instruction (for dynamic) pm.max_children = 16 # Maximum number of child processes that can be stored at any one time pm.start_Servers = 4 # Number of child processes started at startup Pm. min_spare_Servers = 2 # Maximum number of idle child processes to be created if the number of idle processes is smaller than this value If the number of idle child processes exceeds this value, the corresponding child process will be killed. Catch_workers_output = Yes # redirects worker's standard output and error output to the main error log. If not set, the worker's standard output and error output will be redirected to /dev/nullCopy the code

Production environment configuration:

Forward the request to phP-fpm

Nginx, for example:

server { listen 83; server_name mobile.com; root /app/mobile/web/; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location / { index index.html index.htm index.php; # Redirect everything that isn't a real file to index.php try_files $uri $uri/ /index.php$is_args$args; # include fastcgi_params; # include fastcgi_params; # include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; Fastcgi_pass 192.168.33.30:9000; # fastcgi_index index. PHP; try_files $uri =404; #include fastcgi.conf; } location ~ /.(ht|svn|git) { deny all; } access_log /app/wwwlogs/access.log; error_log /app/wwwlogs/error.log; }Copy the code

The above content hope to help you, more free PHP large factory PDF, PHP advanced architecture video materials, PHP wonderful good article can be wechat search concern: PHP open source community

2021PHP big factory interview true question collection, must see!

Four years essence OF PHP technical articles collation collection – PHP framework

A collection of four years of quintessential PHP technology articles – Microservices Architecture

Four years of quintessential PHP technology – distributed architecture

Four years of quintessential PHP technology – high concurrency scenarios

Four years essence OF PHP technical articles collation collection – database