您的位置:首页 > 编程语言 > PHP开发

php-fpm优化方法 pm.max_children、pm.min_spare_servers、pm.max_spare_servers

2016-04-28 22:21 513 查看
php-fpm 进程池优化方法

php-fpm进程池开启进程有两种方式,一种是static,直接开启指定数量的php-fpm进程,不再增加或者减少;

另一种则是dynamic,开始时开启一定数量的php-fpm进程,当请求量变大时,动态的增加php-fpm进程数到上限,当空闲时自动释放空闲的进程数到一个下限。

这两种不同的执行方式,可以根据服务器的实际需求来进行调整。

要用到的一些参数,分别是pm、pm.max_children、pm.start_servers、pm.min_spare_servers和pm.max_spare_servers。

pm表示使用那种方式,有两个值可以选择,就是static(静态)或者dynamic(动态)。

下面4个参数的意思分别为:

pm.max_children:静态方式下开启的php-fpm进程数量,在动态方式下他限定php-fpm的最大进程数(这里要注意pm.max_spare_servers的值只能小于等于pm.max_children)

pm.start_servers:动态方式下的起始php-fpm进程数量。

pm.min_spare_servers:动态方式空闲状态下的最小php-fpm进程数量。

pm.max_spare_servers:动态方式空闲状态下的最大php-fpm进程数量。

如果dm设置为static,那么其实只有pm.max_children这个参数生效。系统会开启参数设置数量的php-fpm进程。

如果dm设置为dynamic,4个参数都生效。系统会在php-fpm运行开始时启动pm.start_servers个php-fpm进程,然后根据系统的需求动态在pm.min_spare_servers和pm.max_spare_servers之间调整php-fpm进程数。

PS.

pm.min_spare_servers、pm.max_spare_servers这2个参数一开始我以为是指空闲进程,但是后来服务器给我报了一个错误:

pm.start_servers(70) must not be less than pm.min_spare_servers(15) and not greater than pm.max_spare_servers(60)

要求pm.start_servers的值在pm.min_spare_servers和pm.max_spare_servers之间,经过测试,得出上述结论。

查看php-fpm.log发现如下提示:

you may need to increase pm.start_servers, or pm.min/max_spare_servers

server reached pm.max_children setting (50), consider raising it

查看PHP-FPM内存占用的几个有用小命令,记录如下:

1.查看每个FPM的内存占用:

[vb] view
plain copy







ps -ylC php-fpm --sort:rss
当然,在后后面加 | wc -l可查看系统当前FPM总进程数,我的目前在45个左右。

PHP官方的建议设置值:

pm.max_children = Total RAM dedicated to the web server / Max child process size

2.查看FPM在你的机子上的平均内存占用:

[python] view
plain copy







ps --no-headers -o "rss,cmd" -C php-fpm | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'

It is a tough cookie because there could be numerous factors involved. The first problem with your config is the max_children is ridiculously high. If each child process is using 50MB then 50 x 32768 would easily deplete 16GB.

A better way to determine max_children is to find out how much each child process uses, then factor in the maximum RAM you would like php-fpm to use and then divide the values. E.g. If I have a 16GB server, I can run the following command to determine how much
ram each php-fpm child consumes:
ps -ylC php-fpm --sort:rss


You are on the look out for the RSS column; it states resident memory and is measured in KB. If I have an average of 50MB per process and I want to use a maximum of 10GB for php-fpm processes, then all I do is 10000MB
\ 50MB = 200. So, on that basis I can use 200 children for my stated memory consumption.

Now, with regards to to the servers, you will want to set the max_spare_servers to x2 or x4 the number of cores. So if you have an 8 core CPU then you can start off with a value of 16 for max_spare_servers and go up to 32.

The start_servers value should be around half of the max_spare_servers value.

You should also consider dropping the max_requests to around 500.

Also, in addition to dynamic, the pm value can also be set to static or ondemand. Static will always have a fixed number of servers running at any given time. This is good if you have a consistent amount of users or you want to guarantee you don't breach the
max memory. Ondemand will only start processes when there is a need for them. The downside is obviously having to constantly start/kill processes which will usually translate into a very slight delay in request handling. The the upside, you only use resources
when you need them. "Dynamic" always starts X amount of servers specified in the start_servers option and creates additional processes on an as-need basis.

If you are still experiencing issues with memory then consider changing pm to ondemand.

This is a general guideline, your settings may need further tweaking. It is really a case of playing with the settings and running benchmarks for maximum performance and optimal resource usage. It is somewhat tedious but it is the best way to determine these
types of settings because each setup is different.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: