PHP-FPM 配置

什么是php-fpm

php-fpm(fastcgi process manager)是一种可选的php fastcgi实现,具有一些对任何规模的站点,特别是更繁忙的站点都有用的附加功能。

这些功能包括:

  1. 自适应进程生成(新!)
  2. 基本统计(ala apache的mod_状态)(新!)
  3. 先进的流程管理,优雅的停止/启动
  4. 能够使用不同的uid/gid/chroot/environment和不同的php.ini启动工人(取代安全模式)
  5. stdout和stderr日志记录
  6. 操作码缓存意外破坏时紧急重启
  7. 加速上传支持
  8. 支持“SlowLog”
  9. 对fastcgi的增强,例如fastcgi_finish_request()——一个完成请求和刷新所有数据的特殊功能,同时继续做一些耗时的事情(视频转换、统计处理等)。

……还有更多。

它的设计并没有考虑到虚拟主机(大量池),但是它可以适应任何使用模型。

配置文件参数意义解析

配置文件例子

pm = dynamic
pm.max_children = 2
pm.start_servers = 1
pm.min_spare_servers = 1
pm.max_spare_servers = 2
pm.max_requests = 1024

参数解释

pm 参数配置怎样的进程管理 这里有dynamic, static , ondemand 

static :固定子进程管理

dynamic :子进程数的管理依赖于pm.max_children,pm.start_servers,pm.min_spare_servers,pm.max_spare_servers 这些指令;在这种模式下,至少有一个子进程

pm.max_children  同时存在的最大子进程数

pm.start_servers  php-fpm启动时开始创建的子进程数

pm.min_spare_servers 最少空闲进程数,空闲进程数小于这个设定的值时,php-fpm会自动增加空闲进程

pm.max_spare_servers 最大空闲进程数,空闲进程数大于这个设定的值时,php-fpm会自动杀死多余空闲进程  

ondemand :php-fpm不会创建进程数,只有当请求连接时才会创建,这个需要通过pm.max_children和pm.process_idle_timeout 这两个指令来控制;

pm.max_children  同时存在的最大子进程数

pm.process_idle_timeout 多少秒后空闲进程会被杀死

========================================================


#每个子进程在重启之前应该执行的请求数 
pm.max_requests = 500 

#设置一个超时时间,在超过该时间后会把php的调用栈的保存到文件里 
request_slowlog_timeout = 10s 

#设置一个超时时时,当超过该时间后会杀死请求,这个会用于当出于一些原因php.ini中的max_execution_time这个指今设置的时间没有停止脚本时会用到目前这个指令; 
request_terminate_timeout = 180s 

#捕捉工作进程的标准输出及标准错误输出放入主错误文件中;如果为no 调动这些输出会重定向到/dev/null;如果开启了这个,会在一定程序上使页面延迟; 
catch_workers_output = yes 

#限制php-fpm会去解析的脚本扩展名,在安全考虑上尽量php-fpm去解析.php扩展的,其它的不要去解析 
security.limit_extensions = .php .php3 .php4 .php5 .html .js .css .jpg .jpeg .gif .png .htm 

#开启错误显示 
php_flag[display_errors] = on

#开启错误日志 
php_admin_flag[log_errors] = on 

#指定错误日志路径 
php_admin_value[error_log] = /usr/local/php/var/log/php-php-error-log.log 

#限制了每个 PHP 进程的内存占用上限 
php_admin_value[memory_limit] = 128M 


如何来配置PHP-FPM合理的子进程数

经验仅供参考,具体需要不断调优以适合你的服务器配置。

假设你的服务器内存是1Gib即1000Mb,一个PHP-FPM占用的内存大概30Mb(平均)。那么你可以理论上开20个进程,也就意味着PHP-FPM占用总内存为20*30Mb=600M,内存占用百分比为60%。仅仅为理论值,要保证服务器其他进程占用总内内存百分比要小于40%,否则可能需要调整低于20个或者更低。

以上场景仅仅是内存方面,未考虑CPU,磁盘,网络等等。所以可能pm.max_children会小于20或者更低。当我们设置合理的子进程数是其实要考虑多个方面,保证服务器的负载在一个合适的水平,这样才能最大程度发挥PHP-FPM的功力。

总结

了解PHP-FPM的工作机制会让你更加深入的理解PHP是如何和Web服务器沟通的,大多数情况下会采用Linux,Nginx,Mysql,PHP。Nginx服务器和PHP如何沟通就需要PHP-FPM去管理。那就这些了,希望能帮到你。


Donate

Buy me a coffee, if you thought this blog does help you. Click the link below.

My Personal PayPal.

19条评论

  1. Just want to say your article is as astonishing.
    The clearness in your post is just spectacular and i could assume you are an expert on this subject.
    Well with your permission allow me to grab your RSS feed to keep up to date with forthcoming post.
    Thanks a million and please continue the gratifying
    work.

  2. I wanted to check up and let you know how really I liked discovering your blog today.

  3. I intended to create you a little note just to give thanks as before for the extraordinary solutions you have featured in this case. It was really shockingly open-handed with you to grant publicly just what many of us might have offered for an ebook to generate some dough for themselves, mostly now that you might have tried it in case you desired. These secrets as well served to be a good way to fully grasp that other people online have the identical fervor much like my personal own to know the truth more regarding this matter. I am sure there are numerous more pleasant periods in the future for many who read your site.

  4. Thank you for every one of your hard work on this web page. My aunt really loves making time for investigations and it’s really obvious why. Most of us learn all of the lively mode you offer priceless suggestions through this web site and even increase participation from some other people on the matter so our favorite girl is now starting to learn a lot of things. Take advantage of the rest of the year. You are always doing a remarkable job.

  5. I intended to create you one little note just to give thanks as before for the extraordinary information you’ve discussed at this time. It has been certainly incredibly generous of people like you to allow extensively all that a lot of folks could possibly have distributed as an e-book to earn some cash for their own end, especially considering the fact that you could have done it if you considered necessary. The good tips also acted like a easy way to be sure that most people have a similar dreams just like mine to grasp a lot more in respect of this condition. I’m certain there are lots of more enjoyable moments ahead for individuals that find out your blog.

  6. A lot of thanks for each of your hard work on this web page. Kim really likes going through investigation and it’s really easy to understand why. Many of us know all of the dynamic method you make powerful steps on this web blog and as well as improve participation from people on that issue so our girl is in fact learning a lot of things. Have fun with the remaining portion of the new year. You are conducting a pretty cool job.

  7. Thanks for your own work on this website. My mum takes pleasure in setting aside time for research and it is simple to grasp why. We notice all relating to the powerful way you provide very important tips and hints via your website and therefore strongly encourage response from visitors on this theme while our own simple princess is without a doubt understanding so much. Take pleasure in the rest of the year. Your performing a useful job.

  8. Thanks for your whole work on this website. My niece takes pleasure in working on research and it is simple to grasp why. We all notice all relating to the powerful ways you render vital tricks via your website and therefore welcome response from website visitors on this topic while our own simple princess is without question understanding so much. Take pleasure in the rest of the year. Your performing a wonderful job.

  9. I am just writing to make you understand what a notable experience my wife’s girl had using your web site. She mastered so many pieces, which included what it is like to possess an excellent helping nature to let other people with ease learn some problematic things. You truly surpassed people’s expected results. Thank you for producing these precious, trusted, informative and in addition fun tips about this topic to Lizeth.

  10. I’m just commenting to let you be aware of of the exceptional discovery my cousin’s child enjoyed going through your blog. She discovered a wide variety of details, most notably how it is like to have a very effective coaching heart to get many more effortlessly fully understand certain extremely tough matters. You actually did more than my desires. I appreciate you for displaying such helpful, healthy, educational and also cool guidance on that topic to Evelyn.

  11. Thank you for every one of your labor on this web site. My daughter really loves managing investigations and it’s really obvious why. Most people learn all regarding the lively mode you present rewarding techniques through this web site and even inspire participation from some others on the point then our favorite princess is really starting to learn a whole lot. Take advantage of the rest of the year. You are always doing a splendid job.

  12. I am glad for writing to make you know what a magnificent encounter my friend’s girl gained studying your web site. She learned several pieces, which include what it’s like to possess an awesome helping nature to let other folks very easily know just exactly several multifaceted subject matter. You truly exceeded people’s expectations. Thank you for presenting these powerful, safe, explanatory and in addition fun tips about this topic to Kate.

  13. I’m also commenting to let you be aware of of the brilliant discovery my child encountered checking your blog. She came to understand a lot of details, including how it is like to have a marvelous coaching heart to get folks easily fully grasp a variety of complex issues. You actually did more than my desires. I appreciate you for delivering such good, dependable, edifying and also cool guidance on that topic to Ethel.

  14. Thank you for each of your hard work on this web page. My aunt really likes going through investigations and it’s really obvious why. Most of us learn all of the lively method you offer practical suggestions through this web site and even increase participation from some other people on the issue so our favorite girl is in fact learning a lot of things. Take advantage of the rest of the year. You are always doing a really great job.

  15. I simply wished to thank you very much yet again. I do not know what I would have worked on without those tricks shown by you relating to this topic. This was the troublesome situation in my view, nevertheless witnessing your well-written way you treated the issue took me to weep with joy. Now i am thankful for your work as well as wish you recognize what an amazing job your are undertaking training the rest with the aid of your websites. Probably you haven’t met any of us.

  16. Valuable information. Lucky me I discovered your website by accident, and I’m shocked why this accident didn’t took place earlier! I bookmarked it. https://colitismedi.com Colitis medication for sale

  17. Magnificent website. Plenty of helpful info here. I am sending it to several friends ans additionally sharing in delicious. And certainly, thanks to your sweat! https://schizophreniamedi.com Medications Used To Treat Schizophrenia

  18. Hi there, just became alert to your blog through Google, and found that it’s truly informative. I am going to watch out for brussels. I’ll be grateful if you continue this in future. Numerous people will be benefited from your writing. Cheers! https://hypothyroidismmed.com/ best thyroid medication

发表评论

邮箱地址不会被公开。 必填项已用*标注