When setting these options consider the following:
- How long is your average request?
- What is the maximum number of simultaneous visitors the site(s) get?
- How much memory on average does each child process consume?
sudo grep max_children /var/log/php?.?-fpm.log.1 /var/log/php?.?-fpm.log
free -h
- All fpm processes:
ps -ylC php-fpm7.0 --sort:rss
- Average memory:
ps --no-headers -o "rss,cmd" -C php-fpm7.0 | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'
- All fpm processes memory:
ps -eo size,pid,user,command --sort -size | awk '{ hr=$1/1024 ; printf("%13.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }' | grep php-fpm
-
pm.max_children = Total RAM dedicated to the web server / Max child process size
-
System RAM: 2GB
-
Average Pool size: 85Mb
-
pm.max_children = 1500MB / 85MB = 17
max_children = (average PHP script execution time) * (PHP requests per second)
visitors = max_children * (seconds between page views) / (avg. execution time)
sudo vim /etc/php/7.0/fpm/pool.d/www.conf
pm.max_children = 17
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_request = 1000
; Choose how the process manager will control the number of child processes.
; Possible Values:
; static - a fixed number (pm.max_children) of child processes;
; dynamic - the number of child processes are set dynamically based on the
; following directives:
; pm.max_children - the maximum number of children that can
; be alive at the same time.
;
; pm.start_servers - the number of children created on startup.
; this value must not be less than min_spare_servers
; and not greater than max_spare_servers.
;
; pm.min_spare_servers - the minimum number of children in 'idle'
; state (waiting to process). If the number
; of 'idle' processes is less than this
; number then some children will be created.
;
; pm.max_spare_servers - the maximum number of children in 'idle'
; state (waiting to process). If the number
; of 'idle' processes is greater than this
; number then some children will be killed.
; Note: This value is mandatory.
Hi!
I'm trying to get more information on how PHP children work. I can't find any documentation, blog or anything which would give general answers. I was wondering if you could help me out.
Does one PHP worker equal to one CPU core? I mean having 64 workers on a 32 core VM is unnecessary since they can only use a maximum of 32 cores?
What does a request include (pm.max_requests)? An Nginx request like "index.php"?
When I check PHP status and see "active processes", what's that? Is that one request? One PHP function? A call to a MySQL DB? A PHP worker?
How does PHP handle the Nginx requests coming in regarding workers? I mean on a high level. Is there a queue? Does it depend on the functions, extensions in the PHP file? For example, having 2 PHP children and opening one "index.php" every second when the processing time is .9 seconds. What happens?
When does FPM log lines like the one below? When all the workers have stuff to do or when there's a request which can't be fulfilled since all workers are busy? As you see my questions above, I don't know how the requests(one dynamic file)->manual functions (calculate_cart_items_shipping_cost())->PHP functions(round(3.14159)) work in this regard.
Thank you!