Home>Article>Backend Development> Detailed explanation of the installation and configuration of PHP-FPM

Detailed explanation of the installation and configuration of PHP-FPM

青灯夜游
青灯夜游 forward
2021-03-02 18:07:43 4263browse

This article will take you to learn more about PHP-FPM and introduce the installation and global configuration of PHP-FPM. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Detailed explanation of the installation and configuration of PHP-FPM

##[Recommended learning: "

PHP Video Tutorial"]

Function

PHP-FPM (PHP FastCGI Process Manager) means: PHP FastCGI process manager, software used to manage the PHP process pool and used to accept requests from the web server.

PHP-FPM provides a better PHP process management method, which can effectively control memory and processes, and smoothly reload PHP configuration.

(1). Why does php-fpm appear?

The appearance of fpm is all because of the appearance of php-fastcgi. A program implemented to manage php-fastcgi well

(2). What is php-fastcgi

php-fastcgi is just a cgi program that only parses php requests and returns As a result, it won't be managed (hence php-fpm).

(3)Why not call it php-cgi

In fact, there was a php-cgi before php-fastcgi appeared, but its execution efficiency was low, so it was replaced by php-fastcgi .

(4)What is the difference between fastcgi and cgi?

Dear friends, the difference is huge. When a service web-server (nginx) distributes a request, it knows that the request is a dynamic php request by matching the suffix, and will forward the request to php.

In the era of CGI, the thinking was relatively conservative. After a request came in, the basic configuration information in php.ini was read, the execution environment was initialized, and a process was created every time. Reading the configuration, initializing the environment, returning data, and exiting the process, over time, the work of starting the process becomes tedious and particularly tiring.

When PHP came to the era of 5, everyone was particularly disgusted with this way of working. People who wanted to be lazy would desperately think, can I let cgi start one main process (master) at a time and make it read-only? Get the configuration once and then start multiple worker processes. When a request comes, it is passed to the worker through the master to avoid duplication of work. So fastcgi was born.

(5) Fastcgi is so good, what should I do if the started workers run out?

When there are not enough workers, the master will dynamically start the workers through the information in the configuration, and can take back the workers when they are idle

(6) I 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 will create a main process to control when and how to forward HTTP requests 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 (the child process has run for too long or is no longer needed). Each process in the PHP-FPM process pool exists longer than a single HTTP request and can handle 10, 50, 100, 500 or more HTTP requests.


Installation

PHP has incorporated php-fpm into the core code of PHP after 5.3.3. So php-fpm does not require a separate download and installation.If you want php to support php-fpm, you only need to bring --enable-fpm when compiling the php source code.


Global configuration

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

The specified sub-process fails within a specified period of time, PHP-FPM restarts:

#在指定的一段时间内,如果失效的PHP-FPM子进程数超过这个值,PHP-FPM主进程将优雅重启。 emergency_restart_threshold = 10 #设定emergency_restart_interval 设置采用的时间跨度。 emergency_restart_interval = 1m

Configure the process pool

The remainder of the PHP-FPM configuration file is an area called Pool Definitions. This area configures user settings for each PHP-FPM process pool. The PHP-FPM process pool is a series of related PHP sub-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
www.conf is the default configuration file for the PHP-FPM process pool.

user= nobody #拥有这个 PHP-FPM进程池中子进程的系统用户。要把这个设置的值设用的非根用户的用户名。 group = nobody #拥有这个 PHP-FPM进程池中子进程的系统用户组。要把这个设置的值设应用的非根用户所属的用户组名。 listen=[::]]:9000 #PHP-FPM进程池监听的IP地址和端口号,让 PHP-FPM只接受 nginx从这里传入的请求。 listen. allowed clients =127.0.0.1 #可以向这个 PHP-FPM进程池发送请求的IP地址(一个或多个)。 pm.max children =51 #这个设置设定任何时间点 PHP-FPM进程池中最多能有多少个进程。这个设置没有绝对正确的值,你应该测试你的PHP应用,确定每个PHP进程需要使用多少内存,然后把这个设置设为设备可用内存能容纳的PHP进程总数。对大多数中小型PHP应用来说,每个PHP进程要使用5~15MB内存(具体用量可能有差异)。 假设我们使用设备为这个PHP-FPM进程池分配了512MB可用内存,那么可以把这个设置设为(512MB总内存)/(每个进程使用10MB) = 51个进程。 ...
Edit and save, restart the PHP-FPM main process:

sudo systemctl restart php-fpm.service
For the configuration details of the PHP-FPM process pool, please see http://php.net/manual/install.fpm.configuration.php

Reference Company development environment

The configuration of the test environment is as follows:

[www] user = nobody #进程的发起用户和用户组,用户user是必须设置,group不是 nobody 任意用户 group = nobody listen = [::]:9000 #监听ip和端口,[::] 代表任意ip chdir = /app #在程序启动时将会改变到指定的位置(这个是相对路径,相对当前路径或chroot后的“/”目录)  pm = dynamic #选择进程池管理器如何控制子进程的数量 static:  对于子进程的开启数路给定一个锁定的值(pm.max_children) dynamic:  子进程的数目为动态的,它的数目基于下面的指令的值(以下为dynamic适用参数) pm.max_children = 16 #同一时刻能够存货的最大子进程的数量 pm.start_servers = 4 #在启动时启动的子进程数量 pm.min_spare_servers = 2 #处于空闲"idle"状态的最小子进程,如果空闲进程数量小于这个值,那么相应的子进程会被创建 pm.max_spare_servers = 16 #最大空闲子进程数量,空闲子进程数量超过这个值,那么相应的子进程会被杀掉。 catch_workers_output = Yes #将worker的标准输出和错误输出重定向到主要的错误日志记录中,如果没有设置,根据FastCGI的指定,将会被重定向到/dev/null上
Production environment configuration:

Forward the request to PHP-FPM

nginx as an 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; } #把HTTP请求转发给PHP-FPM进程池处理 location ~ .*\.php include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass 192.168.33.30:9000; #监听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; }

Thinking

[x] Thinking questions:

What factors determine the concurrency of the server?

  • PHP-FPM maximum number of processes,
  • nginx concurrency
(nginx only acts as a proxy server), memory usage, cpu overhead

server QPS (number of requests processed per second) = average number of request connections * (1/response time);

Concurrency = effective time * QPS;

  • Solution:
  1. Add machine diversion
    • Calculate the need to add machines based on the amount of concurrency
  2. Add a single machine New features
    • Enable php opcache and increase memory space
    • Connect to the cache system
    • sql optimization
    • php optimization
      ...

For more programming-related knowledge, please visit:Programming Video! !

The above is the detailed content of Detailed explanation of the installation and configuration of PHP-FPM. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete