search
HomeBackend DevelopmentPHP TutorialWhat is high-performance optimization PHP-FPM

What is high-performance optimization PHP-FPM

[Related learning recommendations: php graphic tutorial]

What is high-performance optimization What is high-performance optimization PHP-FPM-FPM is ubiquitous and can be said to be the most used in Internet Web applications. A wide range of languages.

However, its high performance is not well known, especially when it comes to high-concurrency systems. That's why for such specific use cases, it's being taken over by languages ​​like Node (yes, I know, it's not a language), Go and Elixir.

That said, there are a lot of things you can do to improve What is high-performance optimization What is high-performance optimization PHP-FPM-FPM performance on your server. This article mainly focuses on php-fpm. If you use Nginx, this is the default configuration on the server.

If you know what php-fpm is, please jump directly to the optimization section.

What is php-fpm?

Many developers are not very interested in DevOps knowledge, and even those who are interested in it, few know its underlying layers. principle. Interestingly, when the browser sends a request to a server running What is high-performance optimization What is high-performance optimization PHP-FPM-FPM, What is high-performance optimization What is high-performance optimization PHP-FPM-FPM is not the first service to process the request; instead, HTTP servers, Apache and Nginx are the two most important ones. The "web server" decides how to communicate with What is high-performance optimization What is high-performance optimization PHP-FPM-FPM and then passes the request type, data and header information to the What is high-performance optimization What is high-performance optimization PHP-FPM-FPM process.

What is high-performance optimization What is high-performance optimization PHP-FPM-FPM

The above picture is the request-response life cycle of the What is high-performance optimization What is high-performance optimization PHP-FPM-FPM project (Picture source: ProinerTech)

In modern What is high-performance optimization What is high-performance optimization PHP-FPM-FPM applications, the "find file" part is is the index.php file, which is the proxy configured in the server configuration file to handle all requests.

Exactly how web servers connect to What is high-performance optimization What is high-performance optimization PHP-FPM-FPM is evolving these days, and if we were to delve into all the details, the length of this article would explode. But roughly speaking, during the time when Apache was the web server of choice, What is high-performance optimization What is high-performance optimization PHP-FPM-FPM was included as a module within the server.

So whenever a request is received, the server will start a new process, which will automatically contain What is high-performance optimization What is high-performance optimization PHP-FPM-FPM and execute the request. This method is called mod_php, short for "What is high-performance optimization What is high-performance optimization PHP-FPM-FPM as a module". This approach has its limitations, and Nginx and php-fpm overcome it.

In php-fpm, the responsibility for managing What is high-performance optimization What is high-performance optimization PHP-FPM-FPM lies with the What is high-performance optimization What is high-performance optimization PHP-FPM-FPM program inside the server. In other words, the web server (Nginx, in this case), doesn't care where or how What is high-performance optimization What is high-performance optimization PHP-FPM-FPM is running, as long as it knows how to send and receive data. If needed, in this case you can treat What is high-performance optimization What is high-performance optimization PHP-FPM-FPM as another server that manages some sub-What is high-performance optimization What is high-performance optimization PHP-FPM-FPM processes for incoming requests (so we send the request to the server, which is received by the server and passed to the server — That’s crazy! :-P).

If you have used Nginx, you will see these codes:

     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;
    }

For this line: fastcgi_pass unix:/run/php/php7.2 -fpm.sock;, which tells Nginx to communicate with the php process through the socket of php7.2-fpm.sock. So, for every incoming request, Nginx writes data through this file and after receiving the output, sends it back to the browser.

I must stress again that this is not the most complete or accurate as to how to run it, but it is completely accurate for most DevOps tasks.

With that out of the way, let's review what we've learned so far:

  • What is high-performance optimization What is high-performance optimization PHP-FPM-FPM does not directly receive requests sent by the browser. Web servers like Nginx will intercept it first.
  • The web server knows how to connect to the What is high-performance optimization What is high-performance optimization PHP-FPM-FPM process and pass all request data (paste everything) to What is high-performance optimization What is high-performance optimization PHP-FPM-FPM.
  • After What is high-performance optimization What is high-performance optimization PHP-FPM-FPM has completed its duties, it sends the response back to the web server, which then sends it back to the client (in most cases, the browser).

The flow chart is as follows:

What is high-performance optimization What is high-performance optimization PHP-FPM-FPM

How do What is high-performance optimization What is high-performance optimization PHP-FPM-FPM and Nginx work together? (Picture source: Data Dog)

So far so good, then the key question is: What exactly is What is high-performance optimization What is high-performance optimization PHP-FPM-FPM-FPM?

FPM in What is high-performance optimization What is high-performance optimization PHP-FPM-FPM stands for "Fast Process Manager", a fancy explanation means that the What is high-performance optimization What is high-performance optimization PHP-FPM-FPM running on the server is not a single process, but a number of What is high-performance optimization What is high-performance optimization PHP-FPM-FPM processes that are derived, controlled and terminated by this FPM process manager. It is this process manager that the web server passes the request to.

What is high-performance optimization What is high-performance optimization PHP-FPM-FPM-FPM is a whole rabbit hole in itself, so feel free to explore it if you want, but for our purposes, these explanations will suffice. ?

Why should we optimize php-fpm?

Generally, under normal operation conditions, why should we consider optimization? Why not leave things as they are.

Ironically, I generally provide words of advice for most use cases. If your setup works well and you don't have a special use case, use the default settings. However, squeezing the most processing power out of a single machine is essential if you're looking to expand beyond just one machine, as it can cut your server spend in half (or more!).

要说明的另一件事情是,Nginx是为处理巨大的工作负载而构建的。 它能够同时处理成千上万的连接,但是如果您的What is high-performance optimization What is high-performance optimization PHP-FPM-FPM设置不合理,那么您将浪费很多资源,因为Nginx必须等待What is high-performance optimization What is high-performance optimization PHP-FPM-FPM完成当前处理之后才可以接受下一个请求,最终Nginx不能为您的服务提供任何优势!

所以,接下来让我们看看尝试优化 php-fpm 时我们到底要优化什么。

如何优化 What is high-performance optimization What is high-performance optimization PHP-FPM-FPM-FPM ?

php-fpm 的配置文件在不同服务器上的位置可能不同,因此您需要做一些调查来确定它的位置。在 UNIX 上,你可以使用 find 命令。在我的 Ubuntu 上,它的路径是 /etc/php/7.2/fpm/php-fpm.conf 。当然,7.2是我正在运行的 What is high-performance optimization What is high-performance optimization PHP-FPM-FPM 版本。

下面是这个文件的前几行代码:

;;;;;;;;;;;;;;;;;;;;;
; FPM Configuration ;
;;;;;;;;;;;;;;;;;;;;;

; All relative paths in this configuration file are relative to What is high-performance optimization What is high-performance optimization PHP-FPM-FPM'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

很明显:这一行 pid = /run/php/php7.2-fpm.pid 告诉我们哪个文件包含了 php-fpm 进程的进程 id。

我们还看到 /var/log/php7.2-fpm.logphp-fpm 存储日志的地方。

在这个文件中,像下面这样添加三个变量:

emergency_restart_threshold 10
emergency_restart_interval 1m
process_control_timeout 10s

前两个设置是警告性的,它们告诉 php-fpm 进程,如果10个子进程在一分钟内失败,主 php-fpm 进程应该重新启动自己。

这听起来可能不够稳健,但是 What is high-performance optimization What is high-performance optimization PHP-FPM-FPM 是一个短暂的进程,它会泄漏内存,所以在出现高故障时重新启动主进程可以解决很多问题。

第三个选项是 process_control_timeout,它告诉子进程在执行从父进程接收到的信号之前需要等待这么长的时间。这个设置是非常有用的。例如,当父进程发送终止信号时,子进程正在处理某些事情的时候。十秒的时间,他们会有一个更好的机会完成任务并且优雅地退出。

令人惊讶的是,这 不是 php-fpm 的核心配置!这是因为,为了 web 请求服务,php-fpm 创建了一个新的进程池,它将具有一个单独的配置。在我的例子中,进程池的名称是 www,我想编辑的文件是 /etc/php/7.2/fpm/pool.d/www.conf

让我们来看看文件的内容:

; 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

快速浏览一下上面代码片段的末尾,您就会明白为什么服务器进程以 www-data 的形式运行了。如果您在设置网站时遇到文件权限问题,您可能要将目录的所有者或组更改为 www-data,从而允许What is high-performance optimization What is high-performance optimization PHP-FPM-FPM进程写入日志文件和上传文档等。

最后,我们到达了问题的根源,流程管理器 (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.

由此可见,有三个可用值:

  • Static: 无论什么情况,都会保持一个固定的What is high-performance optimization What is high-performance optimization PHP-FPM-FPM进程数量。
  • Dynamic: 我们需要指定php-fpm在任何给定时间点会保持活动的最小以及最大进程数量。
  • ondemand: 按照需求创建和销毁进程。

那这些设置有什么影响呢?

简而言之,如果你有个小流量的网站,“dynamic”设置在大多数时间内都是一种资源的浪费。假设你的pm.min_spare_servers设置成了3,那会有三个What is high-performance optimization What is high-performance optimization PHP-FPM-FPM进程会被创建并保持运行,甚至是网站没有流量时。这种情况下,“ondemand” 就是个更好的选择, 可以让系统决定何时启动新的进程。

另一方面, 大流量 或者必须快速响应的网站将在这种情况下被惩罚。 最好避免创建新的 What is high-performance optimization What is high-performance optimization PHP-FPM-FPM 进程的额外开销,使其成为池的一部分并对其进行监控。

使用 pm = static 固定子进程的数量,使最大的系统资源用于服务请求而不是管理 What is high-performance optimization What is high-performance optimization PHP-FPM-FPM。假如你确定走这条路,注意它有其指导方针和陷阱.关于它的一篇相当密集但非常有用的文章是 这篇 。

写在最后

由于有关网络性能的文章可能会引发争论或使人们感到困惑,因此在结束本文之前,我觉得需要讲几句话。 性能调优既涉及系统知识,也涉及猜测和技巧。

即使您完全了解 php-fpm 的所有设置,也无法保证成功。 如果您不了解 php-fpm 的存在,那么您就不必浪费时间担心它。 继续做您已经在做的事情并继续下去。

At the same time, try not to make the results as dramatic as possible. Yes, you can get better performance by recompiling What is high-performance optimization What is high-performance optimization PHP-FPM-FPM from scratch and removing all unnecessary modules, but this approach is not sensible enough in a production environment. The whole idea of ​​optimizing something is to see if your needs differ from the defaults (they rarely do!) and make smaller changes if necessary.

Related learning recommendations: php programming (video)

The above is the detailed content of What is high-performance optimization PHP-FPM. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:learnku. If there is any infringement, please contact admin@php.cn delete
如何使用php-fpm进行高性能调优如何使用php-fpm进行高性能调优Jul 08, 2023 am 11:30 AM

如何使用php-fpm进行高性能调优PHP是一种非常流行的服务器端脚本语言,广泛用于开发网页应用和动态网站。然而,随着访问量的增加,PHP应用程序的性能可能会受到影响。为了解决这个问题,我们可以使用php-fpm(FastCGIProcessManager)来进行高性能调优。本文将介绍如何使用php-fpm来提升PHP应用程序的性能,并提供代码示例。一、

如何使用PHP-FPM优化提高WooCommerce应用的性能如何使用PHP-FPM优化提高WooCommerce应用的性能Oct 05, 2023 am 08:24 AM

如何使用PHP-FPM优化提高WooCommerce应用的性能概述WooCommerce是一个非常流行的电子商务插件,用于在WordPress网站上创建和管理在线商店。然而,随着商店的增长和流量的增加,WooCommerce应用可能会变得缓慢和不稳定。为了解决这个问题,我们可以使用PHP-FPM来优化和提高WooCommerce应用的性能。什么是PHP-FP

利用php-fpm连接池提升数据库访问性能利用php-fpm连接池提升数据库访问性能Jul 07, 2023 am 09:24 AM

利用php-fpm连接池提升数据库访问性能概述:在Web开发中,数据库的访问是非常频繁且耗时的操作之一。传统的方法是每次数据库操作都新建一个数据库连接,使用完毕后再关闭连接。这种方式会造成数据库连接的频繁建立和关闭,增加了系统的开销。为了解决这个问题,可以利用php-fpm连接池技术来提升数据库访问性能。连接池的原理:连接池是一种缓存技术,将一定数量的数据库

php-fpm调优方法详解php-fpm调优方法详解Jul 08, 2023 pm 04:31 PM

PHP-FPM是一种常用的PHP进程管理器,用于提供更好的PHP性能和稳定性。然而,在高负载环境下,PHP-FPM的默认配置可能无法满足需求,因此我们需要对其进行调优。本文将详细介绍PHP-FPM的调优方法,并给出一些代码示例。一、增加进程数默认情况下,PHP-FPM只启动少量的进程来处理请求。在高负载环境下,我们可以通过增加进程数来提高PHP-FPM的并发

什么是php-fpm?如何进行优化来提升性能?什么是php-fpm?如何进行优化来提升性能?May 13, 2022 pm 07:56 PM

什么是php-fpm?下面本篇带大家了解一下php-fpm,介绍一下优化 php-fpm 时我们到底要优化什么,希望对大家有所帮助!

ubuntu没有php-fpm怎么办ubuntu没有php-fpm怎么办Feb 03, 2023 am 10:51 AM

ubuntu没有php-fpm的解决办法:1、通过执行“sudo apt-get”命令添加php的源地址;2、查看有没有php7的包;3、通过“sudo apt-get install”命令安装PHP;4、修改配置监听9000端口来处理nginx的请求;5、通过“sudo service php7.2-fpm start”启动“php7.2-fpm”即可。

利用php-fpm进程管理实现负载均衡利用php-fpm进程管理实现负载均衡Jul 09, 2023 pm 01:07 PM

利用php-fpm进程管理实现负载均衡随着互联网应用的日益复杂和用户量的增加,负载均衡成为一个不可或缺的技术。负载均衡的目标是将流量分配到多个服务器上,以提高系统的稳定性和性能。在PHP应用中,php-fpm(PHPFastCGIProcessManager)是一种常见的进程管理工具,可以被用于实现负载均衡,并且提供了灵活的配置选项。本文将介绍如何利用

php-fpm性能监控与调优策略php-fpm性能监控与调优策略Jul 07, 2023 am 08:39 AM

php-fpm性能监控与调优策略引言:随着互联网的发展,PHP作为一种高效的服务器端脚本语言,被广泛应用于Web开发领域。而php-fpm作为php运行环境的一种解决方案,具有较高的并发处理能力。然而,在高并发的情况下,php-fpm会面临性能瓶颈的问题。本文将介绍php-fpm的性能监控与调优策略,旨在提高php-fpm的性能和稳定性。一、php-fpm性

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!