Home > Backend Development > PHP Tutorial > Why should you optimize php-fpm? How to optimize?

Why should you optimize php-fpm? How to optimize?

青灯夜游
Release: 2023-04-11 07:46:02
forward
2946 people have browsed it

Why should you optimize php-fpm? How to optimize?

Why should you optimize php-fpm? How to optimize? is ubiquitous and can be said to be the most widely used language for Internet web applications.

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 Why should you optimize php-fpm? How to optimize? 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 are extremely Few people know its underlying principles. Interestingly, when the browser sends a request to a server running Why should you optimize php-fpm? How to optimize?, Why should you optimize php-fpm? How to optimize? 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 Why should you optimize php-fpm? How to optimize? and then passes the request type, data and header information to the Why should you optimize php-fpm? How to optimize? process.

Why should you optimize php-fpm? How to optimize?

The above picture is the request-response life cycle of the Why should you optimize php-fpm? How to optimize? project (Picture source: ProinerTech)

In modern Why should you optimize php-fpm? How to optimize? 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 Why should you optimize php-fpm? How to optimize? 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, Why should you optimize php-fpm? How to optimize? 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 Why should you optimize php-fpm? How to optimize? and execute the request. This method is called mod_php, short for "Why should you optimize php-fpm? How to optimize? as a module". This approach has its limitations, and Nginx and php-fpm overcome it.

In php-fpm, the responsibility for managing Why should you optimize php-fpm? How to optimize? lies with the Why should you optimize php-fpm? How to optimize? program inside the server. In other words, the web server (Nginx, in this case), doesn't care where or how Why should you optimize php-fpm? How to optimize? is running, as long as it knows how to send and receive data. If needed, in this case you can treat Why should you optimize php-fpm? How to optimize? as another server that manages some sub-Why should you optimize php-fpm? How to optimize? 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;
    }
Copy after login

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:

  • Why should you optimize php-fpm? How to optimize? 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 Why should you optimize php-fpm? How to optimize? process and pass all request data (paste everything) to Why should you optimize php-fpm? How to optimize?.
  • After Why should you optimize php-fpm? How to optimize? 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:

Why should you optimize php-fpm? How to optimize?

How do Why should you optimize php-fpm? How to optimize? and Nginx work together? (Picture source: Data Dog)

So far so good, then the key question is: What exactly is Why should you optimize php-fpm? How to optimize?-FPM?

FPM in Why should you optimize php-fpm? How to optimize? stands for "Fast Process Manager", a fancy explanation means that the Why should you optimize php-fpm? How to optimize? running on the server is not a single process, but a number of Why should you optimize php-fpm? How to optimize? 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.

Why should you optimize php-fpm? How to optimize?-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是为处理巨大的工作负载而构建的。 它能够同时处理成千上万的连接,但是如果您的Why should you optimize php-fpm? How to optimize?设置不合理,那么您将浪费很多资源,因为Nginx必须等待Why should you optimize php-fpm? How to optimize?完成当前处理之后才可以接受下一个请求,最终Nginx不能为您的服务提供任何优势!

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

如何优化 Why should you optimize php-fpm? How to optimize?-FPM ?

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

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

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

; All relative paths in this configuration file are relative to Why should you optimize php-fpm? How to optimize?'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
Copy after login

很明显:这一行 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
Copy after login

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

这听起来可能不够稳健,但是 Why should you optimize php-fpm? How to optimize? 是一个短暂的进程,它会泄漏内存,所以在出现高故障时重新启动主进程可以解决很多问题。

第三个选项是 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
Copy after login

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

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

那么,这里的 「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.
Copy after login

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

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

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

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

另一方面, 大流量 或者必须快速响应的网站将在这种情况下被惩罚。 最好避免创建新的 Why should you optimize php-fpm? How to optimize? 进程的额外开销,使其成为池的一部分并对其进行监控。

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

写在最后

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

即使您完全了解 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 Why should you optimize php-fpm? How to optimize? 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.

English original address: https://geekflare.com/php-fpm-optimization/

Recommended learning: "Why should you optimize php-fpm? How to optimize? Video Tutorial"

The above is the detailed content of Why should you optimize php-fpm? How to optimize?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template