亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
What is php-fpm?
Why should we optimize php-fpm?
如何優(yōu)化 What is php-fpm? How to optimize to improve performance?-FPM ?
Home Backend Development PHP Tutorial What is php-fpm? How to optimize to improve performance?

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

May 13, 2022 pm 07:56 PM
php php-fpm

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.

具有諷刺意味的是,一般我為大多數(shù)用例提供建議的話。 如果您的設(shè)置運(yùn)行良好,并且沒有特殊用例,請(qǐng)使用默認(rèn)設(shè)置。 但是,如果您希望擴(kuò)展一臺(tái)機(jī)器之外的能力,那么從一臺(tái)機(jī)器中擠出最大的處理能力是必不可少的,因?yàn)樗梢詫⒛?wù)器的花費(fèi)減少一半(甚至更多?。?/p>

要說明的另一件事情是,Nginx是為處理巨大的工作負(fù)載而構(gòu)建的。 它能夠同時(shí)處理成千上萬的連接,但是如果您的What is php-fpm? How to optimize to improve performance?設(shè)置不合理,那么您將浪費(fèi)很多資源,因?yàn)镹ginx必須等待What is php-fpm? How to optimize to improve performance?完成當(dāng)前處理之后才可以接受下一個(gè)請(qǐng)求,最終Nginx不能為您的服務(wù)提供任何優(yōu)勢(shì)!

所以,接下來讓我們看看嘗試優(yōu)化 php-fpm 時(shí)我們到底要優(yōu)化什么。

如何優(yōu)化 What is php-fpm? How to optimize to improve performance?-FPM ?

php-fpm 的配置文件在不同服務(wù)器上的位置可能不同,因此您需要做一些調(diào)查來確定它的位置。在 UNIX 上,你可以使用 find 命令。在我的 Ubuntu 上,它的路徑是 /etc/php/7.2/fpm/php-fpm.conf 。當(dāng)然,7.2是我正在運(yùn)行的 What is php-fpm? How to optimize to improve performance? 版本。

下面是這個(gè)文件的前幾行代碼:

;;;;;;;;;;;;;;;;;;;;;
;?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 告訴我們哪個(gè)文件包含了 php-fpm 進(jìn)程的進(jìn)程 id。

我們還看到 /var/log/php7.2-fpm.logphp-fpm 存儲(chǔ)日志的地方。

在這個(gè)文件中,像下面這樣添加三個(gè)變量:

emergency_restart_threshold?10
emergency_restart_interval?1m
process_control_timeout?10s

前兩個(gè)設(shè)置是警告性的,它們告訴 php-fpm 進(jìn)程,如果10個(gè)子進(jìn)程在一分鐘內(nèi)失敗,主 php-fpm 進(jìn)程應(yīng)該重新啟動(dòng)自己。

這聽起來可能不夠穩(wěn)健,但是 What is php-fpm? How to optimize to improve performance? 是一個(gè)短暫的進(jìn)程,它會(huì)泄漏內(nèi)存,所以在出現(xiàn)高故障時(shí)重新啟動(dòng)主進(jìn)程可以解決很多問題。

第三個(gè)選項(xiàng)是 process_control_timeout,它告訴子進(jìn)程在執(zhí)行從父進(jìn)程接收到的信號(hào)之前需要等待這么長(zhǎng)的時(shí)間。這個(gè)設(shè)置是非常有用的。例如,當(dāng)父進(jìn)程發(fā)送終止信號(hào)時(shí),子進(jìn)程正在處理某些事情的時(shí)候。十秒的時(shí)間,他們會(huì)有一個(gè)更好的機(jī)會(huì)完成任務(wù)并且優(yōu)雅地退出。

令人驚訝的是,這 不是 php-fpm 的核心配置!這是因?yàn)?,為?web 請(qǐng)求服務(wù),php-fpm 創(chuàng)建了一個(gè)新的進(jìn)程池,它將具有一個(gè)單獨(dú)的配置。在我的例子中,進(jìn)程池的名稱是 www,我想編輯的文件是 /etc/php/7.2/fpm/pool.d/www.conf

讓我們來看看文件的內(nèi)容:

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

快速瀏覽一下上面代碼片段的末尾,您就會(huì)明白為什么服務(wù)器進(jìn)程以 www-data 的形式運(yùn)行了。如果您在設(shè)置網(wǎng)站時(shí)遇到文件權(quán)限問題,您可能要將目錄的所有者或組更改為 www-data,從而允許What is php-fpm? How to optimize to improve performance?進(jìn)程寫入日志文件和上傳文檔等。

最后,我們到達(dá)了問題的根源,流程管理器 (pm) 設(shè)置。一般情況下,默認(rèn)值是這樣的:

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(動(dòng)態(tài))」是什么意思呢?我認(rèn)為官方文檔最好地解釋了這一點(diǎn)(我的意思是,這應(yīng)該已經(jīng)是您正在編輯的文件的一部分,但是我在這里復(fù)制了它,以防它不是):

;?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.

由此可見,有三個(gè)可用值:

  • Static: 無論什么情況,都會(huì)保持一個(gè)固定的What is php-fpm? How to optimize to improve performance?進(jìn)程數(shù)量。
  • Dynamic: 我們需要指定php-fpm在任何給定時(shí)間點(diǎn)會(huì)保持活動(dòng)的最小以及最大進(jìn)程數(shù)量。
  • ondemand: 按照需求創(chuàng)建和銷毀進(jìn)程。

那這些設(shè)置有什么影響呢?

簡(jiǎn)而言之,如果你有個(gè)小流量的網(wǎng)站,“dynamic”設(shè)置在大多數(shù)時(shí)間內(nèi)都是一種資源的浪費(fèi)。假設(shè)你的pm.min_spare_servers設(shè)置成了3,那會(huì)有三個(gè)What is php-fpm? How to optimize to improve performance?進(jìn)程會(huì)被創(chuàng)建并保持運(yùn)行,甚至是網(wǎng)站沒有流量時(shí)。這種情況下,“ondemand” 就是個(gè)更好的選擇, 可以讓系統(tǒng)決定何時(shí)啟動(dòng)新的進(jìn)程。

另一方面, 大流量 或者必須快速響應(yīng)的網(wǎng)站將在這種情況下被懲罰。 最好避免創(chuàng)建新的 What is php-fpm? How to optimize to improve performance? 進(jìn)程的額外開銷,使其成為池的一部分并對(duì)其進(jìn)行監(jiān)控。

使用?pm = static?固定子進(jìn)程的數(shù)量,使最大的系統(tǒng)資源用于服務(wù)請(qǐng)求而不是管理 What is php-fpm? How to optimize to improve performance?。假如你確定走這條路,注意它有其指導(dǎo)方針和陷阱.關(guān)于它的一篇相當(dāng)密集但非常有用的文章是?這篇 。

寫在最后

由于有關(guān)網(wǎng)絡(luò)性能的文章可能會(huì)引發(fā)爭(zhēng)論或使人們感到困惑,因此在結(jié)束本文之前,我覺得需要講幾句話。 性能調(diào)優(yōu)既涉及系統(tǒng)知識(shí),也涉及猜測(cè)和技巧。

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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use PHP to build social sharing functions PHP sharing interface integration practice How to use PHP to build social sharing functions PHP sharing interface integration practice Jul 25, 2025 pm 08:51 PM

The core method of building social sharing functions in PHP is to dynamically generate sharing links that meet the requirements of each platform. 1. First get the current page or specified URL and article information; 2. Use urlencode to encode the parameters; 3. Splice and generate sharing links according to the protocols of each platform; 4. Display links on the front end for users to click and share; 5. Dynamically generate OG tags on the page to optimize sharing content display; 6. Be sure to escape user input to prevent XSS attacks. This method does not require complex authentication, has low maintenance costs, and is suitable for most content sharing needs.

PHP creates a blog comment system to monetize PHP comment review and anti-brush strategy PHP creates a blog comment system to monetize PHP comment review and anti-brush strategy Jul 25, 2025 pm 08:27 PM

1. Maximizing the commercial value of the comment system requires combining native advertising precise delivery, user paid value-added services (such as uploading pictures, top-up comments), influence incentive mechanism based on comment quality, and compliance anonymous data insight monetization; 2. The audit strategy should adopt a combination of pre-audit dynamic keyword filtering and user reporting mechanisms, supplemented by comment quality rating to achieve content hierarchical exposure; 3. Anti-brushing requires the construction of multi-layer defense: reCAPTCHAv3 sensorless verification, Honeypot honeypot field recognition robot, IP and timestamp frequency limit prevents watering, and content pattern recognition marks suspicious comments, and continuously iterate to deal with attacks.

How to use PHP combined with AI to achieve text error correction PHP syntax detection and optimization How to use PHP combined with AI to achieve text error correction PHP syntax detection and optimization Jul 25, 2025 pm 08:57 PM

To realize text error correction and syntax optimization with AI, you need to follow the following steps: 1. Select a suitable AI model or API, such as Baidu, Tencent API or open source NLP library; 2. Call the API through PHP's curl or Guzzle and process the return results; 3. Display error correction information in the application and allow users to choose whether to adopt it; 4. Use php-l and PHP_CodeSniffer for syntax detection and code optimization; 5. Continuously collect feedback and update the model or rules to improve the effect. When choosing AIAPI, focus on evaluating accuracy, response speed, price and support for PHP. Code optimization should follow PSR specifications, use cache reasonably, avoid circular queries, review code regularly, and use X

PHP calls AI intelligent voice assistant PHP voice interaction system construction PHP calls AI intelligent voice assistant PHP voice interaction system construction Jul 25, 2025 pm 08:45 PM

User voice input is captured and sent to the PHP backend through the MediaRecorder API of the front-end JavaScript; 2. PHP saves the audio as a temporary file and calls STTAPI (such as Google or Baidu voice recognition) to convert it into text; 3. PHP sends the text to an AI service (such as OpenAIGPT) to obtain intelligent reply; 4. PHP then calls TTSAPI (such as Baidu or Google voice synthesis) to convert the reply to a voice file; 5. PHP streams the voice file back to the front-end to play, completing interaction. The entire process is dominated by PHP to ensure seamless connection between all links.

How to use PHP to combine AI to generate image. PHP automatically generates art works How to use PHP to combine AI to generate image. PHP automatically generates art works Jul 25, 2025 pm 07:21 PM

PHP does not directly perform AI image processing, but integrates through APIs, because it is good at web development rather than computing-intensive tasks. API integration can achieve professional division of labor, reduce costs, and improve efficiency; 2. Integrating key technologies include using Guzzle or cURL to send HTTP requests, JSON data encoding and decoding, API key security authentication, asynchronous queue processing time-consuming tasks, robust error handling and retry mechanism, image storage and display; 3. Common challenges include API cost out of control, uncontrollable generation results, poor user experience, security risks and difficult data management. The response strategies are setting user quotas and caches, providing propt guidance and multi-picture selection, asynchronous notifications and progress prompts, key environment variable storage and content audit, and cloud storage.

PHP realizes commodity inventory management and monetization PHP inventory synchronization and alarm mechanism PHP realizes commodity inventory management and monetization PHP inventory synchronization and alarm mechanism Jul 25, 2025 pm 08:30 PM

PHP ensures inventory deduction atomicity through database transactions and FORUPDATE row locks to prevent high concurrent overselling; 2. Multi-platform inventory consistency depends on centralized management and event-driven synchronization, combining API/Webhook notifications and message queues to ensure reliable data transmission; 3. The alarm mechanism should set low inventory, zero/negative inventory, unsalable sales, replenishment cycles and abnormal fluctuations strategies in different scenarios, and select DingTalk, SMS or Email Responsible Persons according to the urgency, and the alarm information must be complete and clear to achieve business adaptation and rapid response.

Beyond the LAMP Stack: PHP's Role in Modern Enterprise Architecture Beyond the LAMP Stack: PHP's Role in Modern Enterprise Architecture Jul 27, 2025 am 04:31 AM

PHPisstillrelevantinmodernenterpriseenvironments.1.ModernPHP(7.xand8.x)offersperformancegains,stricttyping,JITcompilation,andmodernsyntax,makingitsuitableforlarge-scaleapplications.2.PHPintegrateseffectivelyinhybridarchitectures,servingasanAPIgateway

How to use PHP to develop AI-driven advertising delivery PHP advertising performance optimization solution How to use PHP to develop AI-driven advertising delivery PHP advertising performance optimization solution Jul 25, 2025 pm 06:12 PM

PHP provides an input basis for AI models by collecting user data (such as browsing history, geographical location) and pre-processing; 2. Use curl or gRPC to connect with AI models to obtain click-through rate and conversion rate prediction results; 3. Dynamically adjust advertising display frequency, target population and other strategies based on predictions; 4. Test different advertising variants through A/B and record data, and combine statistical analysis to optimize the effect; 5. Use PHP to monitor traffic sources and user behaviors and integrate with third-party APIs such as GoogleAds to achieve automated delivery and continuous feedback optimization, ultimately improving CTR and CVR and reducing CPC, and fully implementing the closed loop of AI-driven advertising system.

See all articles