This article is a mirror article of machine translation, please click here to jump to the original article.

View: 11218|Reply: 4

php-fpm parameter configuration and tuning

[Copy link]
Posted on 7/26/2017 9:20:54 AM | | |
php-fpm.conf important parameters are explained in detail

pid = run/php-fpm.pid
#pid设置, it is recommended to enable var/run/php-fpm.pid in the installation directory by default

error_log = log/php-fpm.log
#错误日志, the default is var/log/php-fpm.log in the installation directory

log_level = notice
#错误级别. The available levels are: alert, error, warning, notice, debug. Default: notice.

emergency_restart_threshold = 60
emergency_restart_interval = 60s
# indicates that if the number of php-cgi processes with SIGSEGV or SIGBUS errors exceeds emergency_restart_threshold within the emergency_restart_interval value, php-fpm will be gracefully restarted. These two options generally remain the default.

process_control_timeout = 0
#设置子进程接受主进程复用信号的超时时间. Available units: s (seconds), m (minutes), h (hours), or d (days) Default units: s (seconds). Default: 0.

daemonize = yes
#后台执行fpm, the default value is yes, and if you want to debug, you can change it to no. In FPM, different settings can be used to run multiple process pools. These settings can be set individually for each process pool.

listen = 127.0.0.1:9000
#fpm监听端口, that is, the address processed by PHP in nginx, is generally the default value. The available formats are: 'ip:port', 'port', '/path/to/unix/socket'. Each process pool needs to be set up.

listen.backlog = -1
#backlog数, -1 means unlimited, determined by the operating system, just comment out this line. Reference: http://www.3gyou.cc/?p=41

listen.allowed_clients = 127.0.0.1
#允许访问FastCGI进程的IP, set any to not restrict IP, if you want to set the nginx of other hosts to access this FPM process, you need to set the IP address that can be accessed at the cost of listen. The default value is any. Each address is separated by a comma. If it is not set or is empty, any server is allowed to request a connection

listen.owner = www
listen.group = www
listen.mode = 0666
#unix socket setting option, if you use TCP to access, you can comment here.

user = www
group = www
#启动进程的帐户和组

pm = dynamic #对于专用服务器, pm can be set to static.
#如何控制子进程, the options are static and dynamic. If static is selected, a fixed number of child processes is specified by pm.max_children. If dynamic is selected, it is determined by the following parameters:
pm.max_children #, the maximum number of child processes
pm.start_servers #, the number of processes at startup
pm.min_spare_servers #, guarantee a minimum number of idle processes, and create a new child process if the idle process is less than this value
pm.max_spare_servers#, ensure the maximum number of idle processes, if the idle process is greater than this value, this will be cleaned

pm.max_requests = 1000
#设置每个子进程重生之前服务的请求数. This is very useful for third-party modules that may have memory leaks. If set to '0', the request is always accepted. Equivalent to PHP_FCGI_MAX_REQUESTS environment variables. Default: 0.

pm.status_path = /status
#FPM状态页面的网址. If you don't have the settings, you can't access the status page. Default: none. munin monitoring will be used

ping.path = /ping
#FPM监控页面的ping网址. If you don't have the settings, you can't access the ping page. This page is used to externally detect whether the FPM is alive and can respond to requests. Note that it must start with a slash (/).

ping.response = pong
#用于定义ping请求的返回相应. Returns text in text/plain format for HTTP 200. Default: pong.

request_terminate_timeout = 0
#设置单个请求的超时中止时间. This option may be useful for scripts that are not aborted for some special reason php.ini 'max_execution_time' in the settings. Set to '0' means 'Off'. You can try changing this option when 502 errors are frequently encountered.

request_slowlog_timeout = 10s
#当一个请求该设置的超时时间后, the corresponding PHP call stack information will be written to the slow log. Set to '0' means 'Off'

slowlog = log/$pool.log.slow
#慢请求的记录日志, use with request_slowlog_timeout

rlimit_files = 1024
#设置文件打开描述符的rlimit限制. Default value: The default open handle is 1024, which can be viewed with ulimit -n and modified by ulimit -n 2048.

rlimit_core = 0
#设置核心rlimit最大限制值. Available values: 'unlimited', 0 or positive integers. Default value: System-defined value.

chroot =
#启动时的Chroot目录. The defined directory needs to be an absolute path. If there is no setting, the chroot is not used.

chdir =
#设置启动目录, Chdir is automatically sent to that directory when it starts. The defined directory needs to be an absolute path. Default: current directory, or/directory (when chrooted)

catch_workers_output = yes
#重定向运行过程中的stdout和stderr到主要的错误日志文件中. If not set, stdout and stderr will be redirected to /dev/null according to FastCGI's rules. Default: Null.



php-fpm parameter tuning

pm = dynamic; Indicates which process quantity management method is used

dynamic means that the number of php-fpm processes is dynamic, the number is pm.start_servers specified at the beginning, if there are more requests, it will automatically increase to ensure that the number of idle processes is not less than pm.min_spare_servers, and if the number of processes is large, it will also be cleaned up accordingly to ensure that the number of excess processes is not more than pm.max_spare_servers

static means that the number of processes in php-fpm is static, and the number of processes is always the number specified by pm.max_children, and does not increase or decrease

pm.max_children = 300; The number of php-fpm processes that are enabled in static mode
pm.start_servers = 20; The number of starting php-fpm processes in dynamic mode
pm.min_spare_servers = 5; The minimum number of php-fpm processes in dynamic mode
pm.max_spare_servers = 35; The maximum number of php-fpm processes in dynamic mode

If pm is static, then only the parameter pm.max_children is valid. The system will open a set number of php-fpm processes

If the PM is dynamic, then the pm.max_children parameter is invalid, and the last three parameters take effect. The system will start pm.start_servers php-fpm processes at the beginning of the php-fpm run, and then dynamically adjust the number of php-fpm processes between pm.min_spare_servers and pm.max_spare_servers according to the system's needs

So, which PM method is better to choose for our server? In fact, like Apache, running PHP programs will more or less have memory leakage problems after execution. This is why a php-fpm process only occupies about 3M memory at the beginning, and after running for a while, it will rise to 20-30M.

For servers with large memory (such as 8G or more), it is actually more appropriate to specify static max_children, as this does not require additional process control and improves efficiency. Because frequent switching of php-fpm processes will also lag time, it will be better to turn on static effects when the memory is large enough. The amount can also be obtained according to memory/30M, for example, 8GB memory can be set to 100, then the memory consumed by php-fpm can be controlled in 2G-3G. If the memory is slightly smaller, such as 1G, then specifying the number of static processes is more conducive to the stability of the server. This ensures that php-fpm only gets enough memory, and allocates a small amount of memory to other applications, which will make the system run more smoothly.

For a server with small memory, such as a VPS with 256M memory, even if it is calculated according to a 20M memory amount, 10 php-cgi processes will consume 200M memory, then the system crash should be normal. Therefore, you should try to control the number of php-fpm processes, and after roughly clarifying the memory occupied by other applications, specify a static small number for it, which will make the system more stable. Or use the dynamic method, because the dynamic method will end the redundant process and reclaim some memory, so it is recommended to use it on servers or VPS with less memory. The specific maximum amount is obtained based on memory/20M. For example, for a 512M VPS, it is recommended to set pm.max_spare_servers to 20. As for the pm.min_spare_servers, it is recommended to set it according to the load of the server, and the more appropriate value is between 5~10.

200 on a server with 4G memory is fine (my 1G test machine, 64 is the best, it is recommended to use stress test to get the best value)

pm.max_requests = 10240;

The biggest problem in the nginx php-fpm configuration process is the internal leakage problem: the load on the server is not large, but the memory occupation increases rapidly, quickly eating up the memory and then starting to eat the swap partition, and the system hangs up quickly! In fact, according to the official introduction, php-cgi does not have memory leakage, and php-cgi will reclaim memory after each request is completed, but will not be released to the operating system, which will cause a large amount of memory to be occupied by php-cgi.


The official solution is to lower the value of the PHP_FCGI_MAX_REQUESTS, if php-fpm is used, the corresponding php-fpm.conf is the max_requests, which means how many requests will be sent to restart the thread, we need to lower this value appropriately, so that php-fpm automatically frees up memory, not most of the 51200 on the Internet, etc., in fact, there is another value related to it max_ children, this is how many processes will be established each time php-fpm, so that the actual memory consumption is max_children*max_requests* Each request uses memory, according to this we can estimate the memory usage, so there is no need to write scripts to kill.

request_terminate_timeout = 30;

Maximum execution time, configurable in php.ini (max_execution_time)

request_slowlog_timeout = 2; Turn on slow logs
slowlog = log/$pool.log.slow; Slow log path

rlimit_files = 1024; Added a limit on php-fpm to open file descriptors





Previous:Alibaba Cloud SLB load balancing HTTP health check failure analysis
Next:dz x3.4 file verification missing files
 Landlord| Posted on 11/28/2021 1:42:00 PM |
[Practice] Check php-fpm status information through Nginx
https://www.itsvse.com/thread-10180-1-1.html
 Landlord| Posted on 12/19/2021 10:48:43 AM |
 Landlord| Posted on 1/16/2022 3:26:55 PM |
[root@old_server conf]# cat /alidata/server/php/etc/php-fpm.conf | grep max_spare_servers
;             pm.max_spare_servers - the maximum number of children in 'idle'
; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
pm.max_spare_servers = 35
[root@old_server conf]# cat /alidata/server/php/etc/php-fpm.conf | grep min_spare_servers
;             pm.min_spare_servers - the minimum number of children in 'idle'
; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
pm.min_spare_servers = 5
[root@old_server conf]# cat /alidata/server/php/etc/php-fpm.conf | grep start_servers
;             pm.start_servers     - the number of children created on startup.
pm.start_servers = 20
[root@old_server conf]# cat /alidata/server/php/etc/php-fpm.conf | grep max_requests
pm.max_requests = 10240
[root@old_server conf]# cat /alidata/server/php/etc/php-fpm.conf | grep request_slowlog_timeout
; Note: slowlog is mandatory if request_slowlog_timeout is set
; request_slowlog_timeout = 0
[root@old_server conf]# cat /alidata/server/php/etc/php-fpm.conf | grep slowlog
; - 'slowlog'
; Note: slowlog is mandatory if request_slowlog_timeout is set
slowlog = /alidata/log/php/$pool.log.slow
; dumped to the 'slowlog' file. A value of '0s' means 'off'.
; request_slowlog_timeout = 0
[root@old_server conf]# cat /alidata/server/php/etc/php-fpm.conf | grep rlimit
; Set open file descriptor rlimit for the master process.
; rlimit_files = 1024
; Set max core size rlimit for the master process.
; rlimit_core = 0
; Set open file descriptor rlimit.
; rlimit_files = 1024
; Set max core size rlimit.
; rlimit_core = 0
[root@old_server conf] #
 Landlord| Posted on 1/16/2022 4:54:04 PM |
request_terminate_timeout = 10s

The default value is 0 seconds, which means that the PHP script will continue to execute. In this way, when all the php-cgi processes are busy, the Nginx+PHP WebServer can no longer handle new PHP requests, and Nginx will return a "502 Bad Gateway" to the user. request_terminate_timeout = 30s can be used
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com