PHP是无处不在的,可以说是互联网Web应用上使用最广泛的语言。然而,它的高性能并不为人所知,尤其是在涉及到高并发系统时。,何为是高性能优化PHP-FPM
上图是 PHP 项目的请求-响应生命周期(图片来源: ProinerTech) 在现代 PHP 应用中,「find file」部分即为 如今,Web 服务器究竟如何连接 PHP 正在进化,如果我们要深入研究所有细节,这篇文章的长度将激增。但粗略来说, 在 Apache 作为 Web 服务器首选的时间段,PHP 是作为包含在服务器内部的模块。 所以每当一个请求被接收,服务器将开启一个新的进程, 它将自动包含 PHP 和执行请求。这个方法被称作 在 如果你用过 location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/run/php/php7.2-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } 对于这一行: 我必须再次强调,对于如何运行这不是最完整或者最准确的,但对于大多数 DevOps 任务是完全准确的。 除此之外,让我们回顾一下到目前为止所学到的东西:
流程图如下: PHP 和 Nginx 如何协同工作? (图片来源:数据狗) 到目前为止都不错, 那么关键问题来了:PHP-FPM到底是什么呢? PHP 中的 PHP-FPM 本身就是一个完整的兔子洞,所以如果您愿意,可以随意探索,但是对于我们的目的,这些解释就足够啦。 ? 为什么要优化php-fpm?一般在正常运行的情况下,为什么要考虑优化呢? 为什么不将事物保持原样。 具有讽刺意味的是,一般我为大多数用例提供建议的话。 如果您的设置运行良好,并且没有特殊用例,请使用默认设置。 但是,如果您希望扩展一台机器之外的能力,那么从一台机器中挤出最大的处理能力是必不可少的,因为它可以将您服务器的花费减少一半(甚至更多!)。 要说明的另一件事情是,Nginx是为处理巨大的工作负载而构建的。 它能够同时处理成千上万的连接,但是如果您的PHP设置不合理,那么您将浪费很多资源,因为Nginx必须等待PHP完成当前处理之后才可以接受下一个请求,最终Nginx不能为您的服务提供任何优势! 所以,接下来让我们看看尝试优化 如何优化 PHP-FPM ?
下面是这个文件的前几行代码: ;;;;;;;;;;;;;;;;;;;;; ; FPM Configuration ; ;;;;;;;;;;;;;;;;;;;;; ; All relative paths in this configuration file are relative to PHP's install ; prefix (/usr). This prefix can be dynamically changed by using the ; '-p' argument from the command line. ;;;;;;;;;;;;;;;;;; ; Global Options ; ;;;;;;;;;;;;;;;;;; [global] ; Pid file ; Note: the default prefix is /var ; Default Value: none pid = /run/php/php7.2-fpm.pid ; Error log file ; If it's set to "syslog", log is sent to syslogd instead of being written ; into a local file. ; Note: the default prefix is /var ; Default Value: log/php-fpm.log error_log = /var/log/php7.2-fpm.log 很明显:这一行 我们还看到 在这个文件中,像下面这样添加三个变量: emergency_restart_threshold 10 emergency_restart_interval 1m process_control_timeout 10s 前两个设置是警告性的,它们告诉 这听起来可能不够稳健,但是 PHP 是一个短暂的进程,它会泄漏内存,所以在出现高故障时重新启动主进程可以解决很多问题。 第三个选项是 令人惊讶的是,这 不是 php-fpm 的核心配置!这是因为,为了 web 请求服务, 让我们来看看文件的内容: ; Start a new pool named 'www'. ; the variable $pool can be used in any directive and will be replaced by the ; pool name ('www' here) [www] ; Per pool prefix ; It only applies on the following directives: ; - 'access.log' ; - 'slowlog' ; - 'listen' (unixsocket) ; - 'chroot' ; - 'chdir' ; - 'php_values' ; - 'php_admin_values' ; When not set, the global prefix (or /usr) applies instead. ; Note: This directive can also be relative to the global prefix. ; Default Value: none ;prefix = /path/to/pools/$pool ; Unix user/group of processes ; Note: The user is mandatory. If the group is not set, the default user's group ; will be used. user = www-data group = www-data 快速浏览一下上面代码片段的末尾,您就会明白为什么服务器进程以 最后,我们到达了问题的根源,流程管理器 (pm) 设置。一般情况下,默认值是这样的: pm = dynamic pm.max_children = 5 pm.start_servers = 3 pm.min_spare_servers = 2 pm.max_spare_servers = 4 pm.max_requests = 200 那么,这里的 「dynamic(动态)」是什么意思呢?我认为官方文档最好地解释了这一点(我的意思是,这应该已经是您正在编辑的文件的一部分,但是我在这里复制了它,以防它不是): ; 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. With this process management, there will be ; always at least 1 children. ; 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. ; 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. ; ondemand - no children are created at startup. Children will be forked when ; new requests will connect. The following parameter are used: ; pm.max_children - the maximum number of children that ; can be alive at the same time. ; pm.process_idle_timeout - The number of seconds after which ; an idle process will be killed. ; Note: This value is mandatory. 由此可见,有三个可用值:
那这些设置有什么影响呢? 简而言之,如果你有个小流量的网站,“dynamic”设置在大多数时间内都是一种资源的浪费。假设你的 另一方面, 大流量 或者必须快速响应的网站将在这种情况下被惩罚。 最好避免创建新的 PHP 进程的额外开销,使其成为池的一部分并对其进行监控。 使用 写在最后 由于有关网络性能的文章可能会引发争论或使人们感到困惑,因此在结束本文之前,我觉得需要讲几句话。 性能调优既涉及系统知识,也涉及猜测和技巧。 即使您完全了解 同时,尽可能不让结果变得很戏剧性。 是的,您可以通过从头开始重新编译 PHP 并删除所有不需要的模块来获得更好的性能,但是这种方法在生产环境中不够明智。 优化某些内容的整个想法是查看您的需求是否与默认值不同(它们很少这样做!),并根据需要进行较小的更改。
以上就是何为是高性能优化PHP-FPM的详细内容,更多请关注 模板之家(www.mb5.com.cn) 其它相关文章! |