Home > Article > Backend Development > Detailed explanation of the installation and configuration of PHP-FPM
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.
##[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.
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
It is to manage the program that starts a master process and multiple worker processes.
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.
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.
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 = 1mConfigure 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/*.confwww.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.serviceFor 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?server QPS (number of requests processed per second) = average number of request connections * (1/response time);
Concurrency = effective time * QPS;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!