简体   繁体   中英

PHP-FPM improve performance

PHP version 7.2 + NGINX

Hello. I have a question - how to improve php-fpm performance by adjusting settings. Machine CPU 8c/16t, ram 64GB.

Current settings:
pm dynamic
max_children 64
max_request 0
tcp socket

I often have problems with reaching max_children settings and what happens is 100% CPU load then. I do not have problems with RAM, always uses about 22GB ram(not only PHP, some other stuff there). I keep seeing in logs to consider setting higher children pool but does it make sense when I have already 100% CPU load with 64 children? I found only ways how to calculate max children pool according to memory limits but what about the CPU?

ps. I have an HTTP caching server and I was able to reduce the load by improving caching rules but I just want to know if there are better settings than what I have currently.

does it make sense when I have already 100% CPU load with 64 children

Yes, it does. If your RAM really allows for more worker processes, by all means, increase them. This will reduce unnecessary process forking (and thus CPU use).

max_request 0

This is a bit dangerous, as it implies no memory leak in third party libraries (which can be). Safer is to set it to eg 10000 to recycle a worker process after 10k requests.

Generally, there is not much you can do to optimize PHP-FPM aside from tuning pm.max_children and pm.max_requests and pm type.

If you can go with static pm , then PHP-FPM will allocate a known number of worker processes, and there is no forking at all. This is the best way to configure PHP-FPM, especially eg if you have a server dedicated to PHP-FPM only (this requires a good amount of experimenting for the ideal value of max_children ).

Where you can greatly improve is with PHP OPCache. Make sure it's enabled. Then monitor whether you have allocated sufficient memory to it, using cachetool .

A good setting for OPCache is disabling validate_timestamps , so it will never re-check PHP files for changes. Why it's good, is because your scripts will run in "parsed" state, and "from" memory. So they will not incur slow disk read nor CPU impact from parsing them and run closer to how a binary program would run.

Disabling validation of timestamps implies that you employ clearing OPCache in your deployment strategy. Again, cachetool is helpful here ( cachetool opcache:reset in a git hook, if git is what you use for deployment anyway).

Finally, do you really need to tweak PHP-FPM while having such CPU use? Probably not. Such CPU use warrants configuring a cache, eg Varnish or NGINX Fastcgi Cache or, at least, NGINX microcache.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM