search
HomeBackend DevelopmentPHP Tutorialphp-fpm process number optimization tutorial

Wordpress was recently migrated to Alibaba Cloud. Since my server is a cloud server, the hard disk and memory are relatively small, so the memory is often not enough. After checking through Linux commands, I found that there are more than 20 php-fpm processes started, occupying nearly 1G of memory, and the entire server is only 1.5 GB of memory, and finally solved this problem by optimizing the number of php-fpm processes. The server saved an additional 600M of memory. I will share the optimization method of php-fpm with everyone.

php-fpm optimization 1. Introduction to php-fpm optimization parameters They are: pm, pm.max_children, pm.start_servers, pm.min_spare_servers, pm.max_spare_servers.

pm: Indicates which method to use. There are two values ​​to choose from, static or dynamic. In older versions, dynamic was called apache-like. Please pay attention to the description of the configuration file for this.

The following four parameters mean:

pm.max_children: Number of php-fpm processes started in static mode pm.start_servers: Number of starting php-fpm processes in dynamic mode pm.min_spare_servers: Minimum number of php-fpm processes in dynamic mode pm.max_spare_servers: Maximum number of php-fpm processes in dynamic mode

Difference: If dm is set to static, then only the parameter pm.max_children takes effect. The system will start a set number of php-fpm processes. If dm is set to dynamic, then the pm.max_children parameter is invalid and the next three parameters take effect. The system will start pm.start_servers php-fpm processes when php-fpm starts running. Then dynamically adjust the number of php-fpm processes between pm.min_spare_servers and pm.max_spare_servers according to the needs of the system

2. Server specific configuration For our server, which execution method is better? In fact, like Apache, the running PHP program will more or less have memory leaks after execution. This is also the reason why a php-fpm process only occupies about 3M of memory at the beginning, and it will increase to 20-30M after running for a period of time.

For servers with large memory (such as 8G or above), it is actually more appropriate to specify static max_children, because this does not require additional process number control and will improve efficiency. Because frequent switching of the php-fpm process will cause lag, it will be better to turn on the static function if the memory is large enough. The quantity can also be obtained based on memory/30M. For example, 8GB memory can be set to 100. Then the memory consumed by php-fpm can be controlled at 2G-3G. If the memory is slightly smaller, such as 1G, then specifying a static number of processes is more conducive to the stability of the server. This can ensure that php-fpm only obtains enough memory, and allocates a small amount of memory to other applications for use, which will make the system run more smoothly.

For a server with small memory, such as a VPS with 256M memory, even if it is calculated based on a 20M memory, 10 php-cgi processes will consume 200M of memory, and the system crash should be normal. Therefore, you should try to control the number of php-fpm processes as much as possible. After roughly clarifying the memory occupied by other applications, assigning it a static small number will make the system more stable. Or use dynamic method, Because the dynamic method will end redundant processes and can recycle and release some memory, it is recommended to use it on servers or VPS with less memory. The specific maximum amount is obtained based on memory/20M. For example, for a 512M VPS, it is recommended to set pm.max_spare_servers to 20. As for pm.min_spare_servers, it is recommended to set it according to the load of the server. For example, if only the PHP environment is deployed on the server, the more appropriate value is between 5 and 10.

This server configuration 1. Basic server information: Hard disk: data disk 30G, system disk 20G Memory: 1.5G CPU: dual core System: CentOS 6.3 64-bit Bandwidth: Exclusive 2M

2. Deployed applications Git, SVN, Apache, Tomcat, PHP, Nginx, Mysql, JDK

3. Optimized parameters

pm = dynamic pm.start_servers = 5 pm.min_spare_servers = 2 pm.max_spare_servers = 8


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
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

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 Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor