search
HomeBackend DevelopmentPHP TutorialPHP中的FPM是做什么的

FPM是FastCGI Process Manager的缩写,由全称我们可以知道其和FastCGI有关,就是一个FastCGI的进程管理器。对于FastCGI我们可以理解成一个协议,儿FPM是其在PHP中的一个实现。

FPM的出现实现了PHP应用的分布式部署,这样使得PHP和web服务器可以在不同的机器上。当然与其说是FPM不如说是FastCGI的出现。最初FPM并没有被PHP的官方正式应用于PHP中,只是作为PHP的一个补丁应用。但是自从PHP5.3.3版本以后FPM被正式捆绑进PHP中,作为了PHP的一部分。这样我们配置起来比较方便,只需在PHP编译的时候添加—enable-fpm选项即可

php-5.6.9]# ./configure –enable-fpm

Fpm 的简单配置

Fpm在php编译的时候添加—enable-fpm选项即可启动fpm,此外在编译的时候还有两个选项分别是 –with-fpm-user=USER 和—with-fpm-group=GROUP,用来设定fpm所属的用户和用户组。如果不指定这两项,那默认的用户和用户组都是nobody。当然这两项也可以在fpm的配置文件php-fpm.conf(其所在目录为PHP安装目录/etc/php-fpm.conf)中修改。

user = nobody

//所属用户

group = nobody

//所属组

listen = 127.0.0.1:9000 

//fpm所在服务器的ip地址和监听的端口号,默认为9000

pm = dynamic 

//设置进程管理器是如何管理子进程的,dynamic动态管理至少会有一个子进程被创建,其数量有个最大值由pm.max_children来设定,而创建的数量由pm.start_servers来设定;static 静态管理设置固定数量的子进程随着服务启动而被创建;ondemand 在服务启动的时候并不创建子进程只是当有请求的时候才根据情况创建。

pm.max_children = 10

//当pm设置为static的时候,此值表示随着服务的启动创建的子进程的数量;当pm设置为dynamic或者ondemand的时候,此值表示创建的子进程最多不能超过此数量

pm.start_servers = 2

//表示随着服务启动创建的子进程(注意这里是子进程而不是线程)的数量,此选项只有在pm 设置为dynamic的时候才有效。并且这个值默认设置为 min_spare_servers + (max_spare_servers – min_spare_servers)/2,并且如果此值设为0,那么创建的子进程的数量也是由上述公式决定。

pm.min_spare_servers = 1

//要求闲置的服务进程的数量的最小值

pm.max_spare_servers = 3

//闲置的服务进程的数量的最大值

pm.process_idle_timeout = 10s

//进程的闲置时间,以秒为单位,超过这个时间该进程将会被杀死

Fpm 的应用

下面我们来看一下如何管理fastcgi服务,首先我们可以进入php安装目录

~]# cd /usr/local/php5

php5]# ./sbin/php-fpm

//开启fastcgi服务,开启服务以后会在/usr/local/php5/var/run/php-fpm.pid中有fastcgi主进程id

php5]# kill –INT `cat /usr/local/php5/var/run/php-fpm.pid`

//关闭fastcgi服务

php5]# kill –USR2 ` cat /usr/local/php5/var/run/php-fpm.pid`

//重启fastcgi服务

Fpm 使用说明

在fpm简单配置中我们提到pm=dynamic和pm.start_servers =2。当开启fastcgi服务以后首先我们查看 php-fpm.pid

php5]# cat /usr/local/php5/var/run/php-fpm.pid //其结果为

32407

php5]# ps x | grep php-fpm   //接着我们使用此命令查看其主进程情况

32407 ?        Ss     0:00 php-fpm: master process (/usr/local/php5/etc/php-fpm.conf)

php5]# ps –ef | grep php-fpm         //然后再使用该命令查看其所有进程情况

root  32407 1 0 13:46 ? 00:00:00 php-fpm: master process (/usr/local/php5/etc/php-fpm.conf)

nobody   32408 32407  0 13:46 ?        00:00:00 php-fpm: pool www          

nobody   32409 32407  0 13:46 ?        00:00:00 php-fpm: pool www

在这里我们看到了三条信息,第一条是主进程,由系统创建,其id为32407,父进程id为1。剩余两条是其子进程,因为在pm.start_servers = 2 我们设置的为2,所以随着服务的启动会创建两个子进程。这两个子进程的用户都是nobody(user=nobody),其进程id分别是 32408、32409,第三项是这两个子进程的父进程的id 32407。

当然fpm至少会创建一个子进程,因为如果start_servers 设置为0 那么其会根据上面我们说的那个公式计算出子进程的数量。当然如果我们设置min_spare_servers 和max_spare_servers都为0,那子进程的数量为0,这样的话是不能启动服务的(这些设置有效的前提是pm设为dynamic)。因为fpm使用用户为nobody的子进程来处理请求的,那个由系统创建的主进程——id为32407,所属用户为root——是不能处理请求的。当然我们可以根据我们服务器的实际情况(例如:内存大小)来优化我们这里的进程数量。

以上只是简单的介绍了fpm的配置与使用,目的就是为了说明fpm的作用。

Statement
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
How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

Simple Guide: Sending Email with PHP ScriptSimple Guide: Sending Email with PHP ScriptMay 12, 2025 am 12:02 AM

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP Performance: Identifying and Fixing BottlenecksPHP Performance: Identifying and Fixing BottlenecksMay 11, 2025 am 12:13 AM

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

Dependency Injection for PHP: a quick summaryDependency Injection for PHP: a quick summaryMay 11, 2025 am 12:09 AM

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

Increase PHP Performance: Caching Strategies & TechniquesIncrease PHP Performance: Caching Strategies & TechniquesMay 11, 2025 am 12:08 AM

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc

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

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools