Home >Backend Development >PHP Tutorial >php-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 modeDifference: 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 |