Introduction to php-fpm parameter configuration and parameter optimization instructions under Linux_php skills

韦小宝
Release: 2023-03-17 18:18:01
Original
1238 people have browsed it

This article mainly explains the Chinese detailed description of some important parameters of php-fpmunder Linux, and introduces in detail the parameter optimization of php-fpmin terms of performance. Let us Let’s take a look at the performance introduction of php-fpm

detailed explanation of important parameters of php-fpm.conf

##pid = run/php-fpm.pid#pid setting, the default is var/run/php-fpm.pid in the
installationdirectory, it is recommended to enable it

error_log= log/php-fpm.log
#Error log, the default is var/log/php-fpm.log
## in the installation directory

#log_level = notice

#Error level. Available levels are: alert (must be processed immediately), error (error situation), warning (warning situation), notice (general important information), debug (debugging) Information). Default: notice.

emergency_restart_threshold = 60

emergency_restart_interval = 60s
#Indicates the number of php-cgi processes that have SIGSEGV or SIGBUS errors within the value set by emergency_restart_interval If emergency_restart_threshold is exceeded, php-fpm will restart gracefully. These two options generally remain at their default values.

process_control_timeout = 0

#Set the timeout for the child process to accept the main process multiplexing signal. Available units: s (seconds), m (minutes), h (hours), Or d (days) Default unit: s (seconds). Default value: 0.

daemonize = yes

#Execute fpm in the background, the default value is yes, if it is for debugging, it can Change to no. In FPM, it is possible to run multiple process pools with different settings. These settings can be set individually for each process pool.

listen = 127.0.0.1:9000

#fpm listening port, which is the address processed by php in nginx. Generally, the default value is sufficient. Available formats are: 'ip:port', 'port', '/path/to/unix/socket'. Each process pool needs to be set.

listen.backlog = -1

#The number of backlogs, -1 means no limit, is determined by the operating system, justcomment
off this line.

listen.allowed_clients = 127.0.0.1

#Allow access to the IP of the FastCGI process. Set any to unrestricted IP. If you want to set nginx of other hosts to also access this FPM Process and listen must be set to a local IP that can be accessed. The default value is any. Each address is separated by a comma. If not set or empty, any server requesting a connection is allowed
##listen.owner = www

listen.group = www

listen.mode = 0666
#Unix socket setting options, if you use tcp to access, just comment here.

user = www

group = www

#The account and group that started the process

pm = dynamic

# For dedicated servers, pm can be set to static.#How to control the child process, 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 #,
Maximum number of child processes
pm.start_servers#, Number of processes at startup
pm.min_spare_servers#, ensure the minimum number of idle processes. If the idle process is less than this value, create a new child process
pm.max_spare_servers#, ensure the number of idle processes Maximum value, if the idle process is larger than this value, this will be cleaned up
pm.max_requests = 1000

#Set the number of requests served before each child process is reborn. For possible memory leaks It is very useful for third-party modules. If set to '0', requests are always accepted. Equivalent to the PHP_FCGI_MAX_REQUESTS environment variable. Default value: 0.
pm.status_path = /status

#The URL of the FPM status page. If not set, the status page cannot be accessed. Default value: none. Munin monitoring will use
ping.path = /ping

#The ping URL of the FPM monitoring page. If it is not set, the ping page cannot be accessed. This page is used to externally detect whether FPM is alive and can respond to requests. Please note that it must start with a slash (/).
ping.response = pong

# is used to define the return response of the ping request. The returned text/plain format text is HTTP 200. Default value: pong.
request_terminate_timeout = 0

#Set the timeout abort time for a single request. This option may be useful for scripts where the 'max_execution_time' in the php.ini setting does not abort running scripts for some special reasons. Setting '0' means 'Off'. You can try changing this option when 502 errors occur frequently.

request_slowlog_timeout = 10s
#When a request is set for the timeout period, the corresponding PHP call stack information will be completely written to the slow log. Setting it to '0' means ' Off'

slowlog = log/$pool.log.slow
#Slow request log, used with request_slowlog_timeout

rlimit_files = 1024
#Set the rlimit limit of the file open descriptor. Default value: The system-defined value of the default open handle is 1024, which can be viewed using ulimit -n and modified with ulimit -n 2048.

rlimit_core = 0
#Set the maximum core rlimit limit value. Available values: 'unlimited', 0 or positiveinteger. Default value: system-defined value .

chroot=
#Chroot directory at startup. The defined directory needs to be an absolute path. If not set, chroot will not be used.

chdir =
#Set the startup directory. Chdir will be automatically directed to this directory during startup. The defined directory needs to be an absolute path. Default value: current directory, or /directory ( chroot)

catch_workers_output = yes
#Redirect stdout and stderr during the running process to the main error log file. If not set, stdout and stderr will be based on FastCGI The rules are redirected to /dev/null. Default value: empty.

php-fpm parameter tuning

pm = dynamic;

Indicates which process number management method is used

dynamicIndicates that the number of php-fpm processes is dynamic , initially it is the number specified by pm.start_servers. If there are more requests, it will be automatically increased to ensure that the number of idle processes is not less than pm.min_spare_servers. If the number of processes is large, it will also be cleaned up accordingly to ensure that the number of excess processes is No more than pm.max_spare_servers

staticmeans that the number of php-fpm processes isstatic. The number of processes is always the number specified by pm.max_children and will not increase. Or reduce

pm.max_children = 300;The number of php-fpm processes started in static mode
pm.start_servers = 20;Start in dynamic mode Number of initial php-fpm processes
pm.min_spare_servers = 5;Minimum number of php-fpm processes in dynamic mode
pm.max_spare_servers = 35;In dynamic mode Maximum number of php-fpm processes

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

If pm is dynamic, then the pm.max_children parameter will be invalid and the next three parameters will take effect. The system will start pm.start_servers php-fpm processes when php-fpm starts running, and then dynamically adjust the number of php-fpm processes between pm.min_spare_servers and pm.max_spare_servers according to the needs of the system.

Then, For our server, which PM method is better? In fact, like Apache, the running PHP program will more or less have memory leaks after execution. This is also the reason why a php-fpm process only occupies about 3M of memory at the beginning, and it will increase to 20-30M after running for a period of time.

For servers with large memory (such as 8G or more), it is actually more appropriate to specify static max_children, because this does not require additional process number control and will improve efficiency. Because frequent switching of the php-fpm process will cause lag, so if the memory is large enough, the static effect will be better. The quantity 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 to 2G-3G. If the memory is slightly smaller, such as 1G, then specifying a static number of processes is more conducive to the stability of the server. This can ensure that php-fpm only obtains enough memory, and allocates a small amount of memory to other applications for use, which will make the system run more smoothly.

For servers with small memory, such as a VPS with 256M memory, even if it is calculated based on a 20M memory, 10 php-cgi processes will consume 200M of memory, and the system crash should be very serious. Normal. Therefore, you should try to control the number of php-fpm processes as much as possible. After roughly clarifying the memory occupied by other applications, assigning it a static small number will make the system more stable. Or use dynamic mode, because dynamic mode will end redundant processes and can recycle and release 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 pm.min_spare_servers, it is recommended to set it according to the load of the server. A more suitable value is between 5 and 10.

On a server with 4G memory, 200 is enough (for my 1G test machine, 64 is the best. It is recommended to use stress testing to obtain the best value)

pm.max_requests = 10240 ;

#nginx The biggest problem in the php-fpm configuration process is internal leakage: the load on the server is not large, but the memory usage increases rapidly, and the memory is quickly consumed and then the swap partition is consumed, and the system is very Hang up! In fact, according to the official introduction, php-cgi does not have memory leaks. After each request is completed, php-cgi will reclaim the memory, but will not release it to the operating system. This will cause a large amount of memory to be occupied by php-cgi.

The official solution is to lower the value of PHP_FCGI_MAX_REQUESTS. If you are using php-fpm, the corresponding php-fpm.conf is max_requests. This value means how many requests will be sent before the thread will be restarted. , we need to lower this value appropriately to allow php-fpm to automatically release memory. It is not 51200 and so on as most people say on the Internet. In fact, there is another value max_children associated with it. This is every time php-fpm How many processes will be created, so that the actual memory consumption is max_children*max_requests*memory used by each request. Based on this, we can estimate the memory usage, so there is no need to write a script to kill.

request_terminate_timeout = 30;

The maximum execution time can also be configured in php.ini (max_execution_time)

request_slowlog_timeout = 2 ;Enable slow log
slowlog = log/$pool.log.slow;Slow log path

rlimit_files = 1024;Add php-fpm Restrictions on opening file descriptors

The parameters of php-fpm.conf are clearly stated. You can probably remember them as long as you read them a few times. As for the php-fpm performance plan, it should be determined based on the actual situation and tested several times. Get the best configuration solution

Related recommendations:

Detailed explanation of the php-FPM process pool exploration

Exploring the PHP-FPM process pool

Introducing solutions to reduce PHP-FPM memory usage

The above is the detailed content of Introduction to php-fpm parameter configuration and parameter optimization instructions under Linux_php skills. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!