PHP-FPM 配置

phpman.song@gmail.com发布

什么是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去管理。那就这些了,希望能帮到你。

分类: LNMP阿里云

15 条评论

cialis buy online usa · 2020年6月9日 下午4:17

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.

mercedes sprinter key fob replacement · 2021年1月5日 下午6:05

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

cbd oil · 2021年1月7日 下午1:07

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.

generic viagra · 2021年1月7日 下午9:49

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.

buy viagra · 2021年1月7日 下午11:10

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.

发表评论

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