search
HomeBackend DevelopmentPHP TutorialWhat is php-fpm? How to optimize to improve performance?

What is php-fpm? The following article will take you to understand php-fpm and introduce what we need to optimize when optimizing php-fpm. I hope it will be helpful to everyone!

What is php-fpm? How to optimize to improve performance?

#What is php-fpm? How to optimize to improve performance? 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 What is php-fpm? How to optimize to improve performance? 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 What is php-fpm? How to optimize to improve performance?, What is php-fpm? How to optimize to improve performance? 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 php-fpm? How to optimize to improve performance? and then passes the request type, data and header information to the What is php-fpm? How to optimize to improve performance? process.

What is php-fpm? How to optimize to improve performance?

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

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

In php-fpm, the responsibility for managing What is php-fpm? How to optimize to improve performance? lies with the What is php-fpm? How to optimize to improve performance? program inside the server. In other words, the web server (Nginx, in this case), doesn't care where or how What is php-fpm? How to optimize to improve performance? is running, as long as it knows how to send and receive data. If needed, in this case you can treat What is php-fpm? How to optimize to improve performance? as another server that manages some sub-What is php-fpm? How to optimize to improve performance? 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 php-fpm? How to optimize to improve performance? 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 php-fpm? How to optimize to improve performance? process and pass all request data (paste everything) to What is php-fpm? How to optimize to improve performance?.
  • After What is php-fpm? How to optimize to improve performance? 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 php-fpm? How to optimize to improve performance?

How do What is php-fpm? How to optimize to improve performance? and Nginx work together? (Picture source: Data Dog)

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

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

具有讽刺意味的是,一般我为大多数用例提供建议的话。 如果您的设置运行良好,并且没有特殊用例,请使用默认设置。 但是,如果您希望扩展一台机器之外的能力,那么从一台机器中挤出最大的处理能力是必不可少的,因为它可以将您服务器的花费减少一半(甚至更多!)。

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

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

如何优化 What is php-fpm? How to optimize to improve performance?-FPM ?

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

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

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

; All relative paths in this configuration file are relative to What is php-fpm? How to optimize to improve performance?'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 php-fpm? How to optimize to improve performance? 是一个短暂的进程,它会泄漏内存,所以在出现高故障时重新启动主进程可以解决很多问题。

第三个选项是 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 php-fpm? How to optimize to improve performance?进程写入日志文件和上传文档等。

最后,我们到达了问题的根源,流程管理器 (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 php-fpm? How to optimize to improve performance?进程数量。
  • Dynamic: 我们需要指定php-fpm在任何给定时间点会保持活动的最小以及最大进程数量。
  • ondemand: 按照需求创建和销毁进程。

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

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

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

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

写在最后

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

Even if you fully understand all settings of php-fpm, success is not guaranteed. If you don't know that php-fpm exists, then you don't have to waste your time worrying about it. Keep doing what you are already doing and keep going.

At the same time, try not to make the results as dramatic as possible. Yes, you can get better performance by recompiling What is php-fpm? How to optimize to improve performance? 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.

Original address: https://geekflare.com/php-fpm-optimization/

Translation address: https://learnku.com/php/t/34358

Recommended: "What is php-fpm? How to optimize to improve performance? Video Tutorial"

The above is the detailed content of What is php-fpm? How to optimize to improve performance?. 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怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

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.

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.