Solution to high load of PHP-FPM

Source: Internet
Author: User

This article only introduces the optimization method of PHP-FPM. However, when used in combination with nginx, optimizing one of them separately does not play a particularly important role, at the same time, nginx needs to be optimized. for nginx practices and methods, refer to deployment. causes nginx
502 Bad Gateway PHP-CGI (FastCGI)

The error of nginx frequently exploding 502 Bad Gateway is still not completely solved after reading the online tutorial.

Currently, I have summarized the following methods to solve the 502 Bad Gateway problem:

1. Depending on the server performance, add max_children value in the php-fmp.conf, I currently use 15.

2. Use the reload parameter to regularly reload PHP-FPM. This is mainly because PHP scripts are executed for a long time. This problem can be eliminated by reloading PHP-FPM. It is worth further exploring how to completely solve the 502 error caused by a large amount of memory occupied by the PHP-CGI script. Currently, this method is a good solution.

The specific method is to use crontab to smoothly restart PHP-FPM without affecting the running of PHP scripts.

*/10 */usr/local/PHP/sbin/PHP-FPM reload

When you running a highload website with PHP-FPM via FastCGI, the following tips may be useful to you :)
These tips may be useful to you if your high-load website manages FastCGI using a PHP-FPM :)

1. Compile PHP's modules as less as possible, the simple the best (FAST );
1. Install as few PHP modules as possible. The simplest is the best (FAST ).

2. increas PHP FastCGI child number to 100 and even more. Sometime, 200 is OK! (On 4 GB memory server );
2. Adjust the number of PHP FastCGI sub-processes to 100 or above and 200 on the 4G memory server.
Note: For my 1g Testing Machine, 64 is the best. We recommend that you use stress testing to obtain the best value.

3. Using socket PHP FastCGI, and put into/dev/SHM on Linux;
3. Use SOCKET to connect to FastCGI. the Linux operating system can be stored in/dev/SHM.
Note: Set <value name = "listen_address">/tmp/nginx in the php-fpm.cnf. socket </value> can connect to FastCGI through socket./dev/SHM is the memory file system, and it will be faster to put it in the memory.

4. Increase Linux "Max open files", using the following command (must be root ):

# Echo 'ulimit-HSN 65536 '>/etc/profile

# Echo 'ulimit-HSN 65536>/etc/rc. Local

# Source/etc/profile
4. Increase the number of files opened in the Linux kernel. You can use these commands (the root account must be used)

Echo 'ulimit-HSN 65536 '>/etc/profile

Echo 'ulimit-HSN 65536 '>/etc/rc. Local

Source/etc/profile
Note: I modified/etc/rc. Local and added it to ulimit-shn 51200.

5. Increase PHP-FPM open file description rlimit:

# Vi/path/to/php-fpm.conf

Find "<value name =" rlimit_files "> 1024 </value>"

Change 1024 to 4096 or higher number.

Restart PHP-FPM.
5. added restrictions on opening file descriptors for PHP-FPM:

# Vi/path/to/php-fpm.conf

Find "<value name =" rlimit_files "> 1024 </value>"

Change 1024 to 4096 or higher. restart PHP-FPM. 6. using PHP code accelerator, E. g eaccelerator, xcache. and set "cache_dir" to/dev/SHM on Linux. 6. use a PHP code accelerator, such
Eaccelerator and xcache. On the Linux platform, you can point 'cache _ dir' to/dev/SHM.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.