PHP-FPM Performance Optimization Reference detailed

Source: Internet
Author: User
Tags curl fpm ini json php script time limit python script

Php-fpm.conf has two critical parameters: one is "Max_children" and the other is "request_terminate_timeout".

The value of my two settings is "40″, one is" 900″, but this value is not universal, but it needs to be calculated by itself.

The method of calculation is as follows:

If your server performance is good enough and your broadband resources are sufficient, PHP scripts do not have loops or bugs, you can set the "request_terminate_timeout" to 0s directly. The meaning of 0s is to allow php-cgi to carry on without any time limit. And if you can't do this, which means that your php-cgi may be a bug, or that your broadband is not enough or that other reasons cause your php-cgi to be suspended, then it's recommended that you assign a value to "Request_terminate_timeout", This value can be set according to the performance of your server. Generally the better performance you can set the higher, 20 minutes-30 minutes can be. Because my server PHP script needs to run for a long time, and some may be more than 10 minutes so I set 900 seconds, so that will not cause php-cgi to die and appear 502 Bad Gateway this error.

And how is the value of "Max_children" calculated? This value is in principle the bigger the better, the php-cgi process will be processed more quickly, the queue of requests will be very little. Set "Max_children" also need to set according to the performance of the server, generally a server under normal circumstances of each php-cgi memory consumed in about 20M, so my "Max_children" I set to 40, 20m*40= 800M means that at peak time all php-cgi are consumed within 800M, less than my valid memory 1Gb. And if my "max_children" set smaller, such as 5-10, then php-cgi will be "very tired", processing speed is also very slow, waiting for a long time. If a long time did not get processing requests will appear 504 Gateway time-out this error, and is processing a very tired of those php-cgi if encountered problems will appear 502 bad Gateway this error.

Max_requests means that each process exceeds this number (which is a little bit related to the PHP process and does not matter much) and automatically kills. I'm supposed to set 512 here, but don't bother with the pressure test, set a larger, but also do not set too large, is a structure, not tested, close to 8K to 9K size. Easily set 100k on the Internet, a bit of waste of memory. A process that wastes size close to 1M. In 128 processes that are commonly used on the web, Probably a waste of about 100M. Well, I admit that 100M is the price of cabbage, but don't waste it. = =

Max_children is basically the number of processes, with the nginx process is not as big as imagined, because FPM will own the process of management (to be verified, at least I simply browsed the source code, that is the meaning). The parameters should not be set too large, it takes up a lot of memory, and the process consumes more than I can say.

Max_children better set the way to set according to REQ/S, if the program is the processing capacity of req/s. The maximum concurrency is 10K, then set 100 is better, this is dynamic to adjust.

However, if you use PHP 5.3, you can also set the style to Apache-like, then set start_servers,min_spare_servers,max_spare_servers three parameters can automatically adjust


PHP 5.4 began to integrate the PHP-FPM, that is, when compiling PHP, as long as--ENABLE-FPM installed the PHP-FPM.

I. Installation of PHP-FPM


Shell >/configure--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php--with-mysql=/usr/local/mysql/\
--with-mysqli=/usr/local/mysql/bin/mysql_config--with-gd--with-xsl--with-bz2 \
--with-zlib--with-curl--with-pear--without-iconv--with-mcrypt \
--with-gettext--with-openssl--with-libxml-dir--with-png-dir--with-jpeg-dir--with-freetype-dir \
--with-libdir=lib64--enable-ftp--enable-fpm--enable-opcache--enable-exif--enable-soap-- Enable-calendar \
--enable-sockets--enable-mbstring--enable-gd-native-ttf--disable-rpath--disable-debug

# # See above this heap of parameters No, this is in compiling PHP, which has a parameter is--ENABLE-FPM Yes, this is the Enable PHP-FPM extension.

Shell > make; Make install

Second, configure PHP-FPM

Shell > Cp/usr/local/src/php-5.6.17/php.ini-production/usr/local/php/php.ini # This is the PHP configuration file
Shell > CP/USR/LOCAL/SRC/PHP-5.6.17/SAPI/FPM/INIT.D.PHP-FPM/ETC/INIT.D/PHP-FPM # This is PHP-FPM's startup script.
Shell > cd/usr/local/php/etc/
Shell > CP php-fpm.conf.default php-fpm.conf # Copy a configuration file
Shell > Vim php-fpm.conf

[Global]

PID = run/php-fpm.pid # pid
Rlimit_files = 65535 # Open number of files limit

[www] # process Pool

user = Nginx # runs as Nginx
Group = Nginx

Listen = 127.0.0.1:9000 # listens to 9000 ports on this machine

; listen =/dev/shm/php-cgi.sock; # Listen for UNIX sockets, and put the socket in the memory space, faster (Nginx also need to modify accordingly)!
; listen.backlog = 10240 # UNIX sockets are highly concurrent and somewhat unstable, which is used to mitigate (SOCKET wait queue Length)

; listen.owner = permissions for Nginx # UNIX sockets
; listen.group = Nginx
; Listen.mode = 0660

PM = dynamic # The way the process is created, dynamically created
Pm.max_children = 32 # Maximum number of processes (can not only look at the memory to create, to see the specific usage, and sometimes enough memory, most of the process, resulting in frequent CPU context switching, load will be very high)
Pm.start_servers = 5 # Number of initial processes
pm.min_spare_servers = 5 # Minimum number of idle processes
Pm.max_spare_servers = 10 # Maximum number of idle processes

Pm.status_path =/php_status # php-fpm State monitoring (Nginx to set access rights)

Shell > Service php-fpm Start

Third, monitoring php-fpm


Shell > vim/usr/local/nginx/conf/nginx.conf

Location ~/php_status {# Create a separate server or add configuration directly to server {}

Access_log off;

Allow 127.0.0.1;
Allow 36.110.41.194; # do the right
Deny all;

Fastcgi_pass 127.0.0.1:9000; # If it is the way of UNIX sockets, write like this: Fastcgi_pass Unix:/dev/shm/php-cgi.sock;
Fastcgi_param script_filename $fastcgi _script_name;
Include Fastcgi_params;
}

Shell > Kill-hup ' cat/usr/local/nginx/logs/nginx.pid '

Shell > Curl Http://127.0.0.1/php_status # Access the path to get the following data
POOL:WWW # Process Pool Name
Process Manager:dynamic # Processes management approach
Start time:22/jan/2016:15:49:00 +0800 # start time
Start since:375 # run time long
Accepted Conn:7 # Number of requests received by the current process pool
Listen queue:0 # Request waiting queues, if not 0, means that the FPM process is insufficient and needs to be increased
Max Listen queue:0 # max wait queue number
Listen Queue len:1024 # SOCKET Wait Queue Length
Idle Processes:4 # Idle Process Count
Active Processes:1 # dynamic number of processes
Total Processes:5 # Process Count
Max Active Processes:1 # Maximum activity number of processes
Max Children reached:0 # reaches the maximum number of processes, if not 0, which means that the maximum number of processes is insufficient and needs to be increased
Slow requests:0 # Slow request number, need to set slow log

Shell > Curl Http://127.0.0.1/php_status # Here are a number of parameters to choose from, such as http://127.0.0.1/php_status?html,? JSON,? xml,???

# I think it's best to use a Python script as a monitor, a JSON format.

Very simple, look at the configuration file, such settings, in the high load and complex PHP program will save a little, after all, testing req/s is hateful physical work.

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.