Introduction of PHP-FPM Starting parameters and important configuration in LNMP

Source: Internet
Author: User
Tags fpm http 200 memory usage mysql query php script socket

To agree on several directories


yusr/local/php/sbin/php-fpm
/usr/local/php/etc/php-fpm.conf
/usr/local/php/etc/php.ini

Startup parameters for PHP-FPM


#测试php-FPM Configuration
/usr/local/php/sbin/php-fpm-t
/usr/local/php/sbin/php-fpm-c/usr/local/php/etc/php.ini-y/usr/local/php/etc/php-fpm.conf-t

#启动php-FPM
/usr/local/php/sbin/php-fpm
/usr/local/php/sbin/php-fpm-c/usr/local/php/etc/php.ini-y/usr/local/php/etc/php-fpm.conf

#关闭php-FPM
Kill-int ' Cat/usr/local/php/var/run/php-fpm.pid '

#重启php-FPM
KILL-USR2 ' Cat/usr/local/php/var/run/php-fpm.pid '

Php-fpm.conf Important Parameter Detailed explanation


PID = Run/php-fpm.pid
#pid设置, the default var/run/php-fpm.pid in the installation directory, recommended to open

Error_log = Log/php-fpm.log
#错误日志, default Var/log/php-fpm.log in the installation directory

Log_level = Notice
#错误级别. Available levels are: alert (must be processed immediately), error (Errors), warning (warning condition), notice (general important information), debug (debug information). Default: Notice.

Emergency_restart_threshold = 60
Emergency_restart_interval = 60s
#表示在emergency_restart_interval所设值内出现SIGSEGV或者SIGBUS错误的php-cgi Number of processes if more than Emergency_restart_threshold, PHP-FPM will gracefully restart. These two options generally keep the default values.

Process_control_timeout = 0
#设置子进程接受主进程复用信号的超时时间. Available units: s (seconds), M (minutes), H (Hours), or D (day) default units: S (sec). Default value: 0.

Daemonize = yes
#后台执行fpm, the default value is yes, if you want to change to no for debugging. In FPM, you can use different settings to run multiple process pools. These settings can be set individually for each process pool.

Listen = 127.0.0.1:9000
#fpm监听端口, that is, the address of PHP processing in Nginx, the general default value can be. Available formats are: ' Ip:port ', ' Port ', '/path/to/unix/socket '. Each process pool needs to be set.

Listen.backlog =-1
#backlog数, 1 means no limit, and the operating system decides that this row is commented out. Backlog meaning reference: http://www.3gyou.cc/?p=41

Listen.allowed_clients = 127.0.0.1
#允许访问FastCGI进程的IP, set any to not restrict IP, if you want to set other host Nginx also can access this FPM process, listen to set the cost of access to IP. The default value is any. Each address is separated by commas. Allow any server to request a connection if it is not set or is empty

Listen.owner = www
Listen.group = www
Listen.mode = 0666
#unix socket setting option, if you are using TCP access, Comment here.

user = www
Group = www
#启动进程的帐户和组

PM = Dynamic #对于专用服务器, PM can be set to static.
#如何控制子进程, the options are static and dynamic. If you select static, Pm.max_children specifies the number of fixed child processes. If dynamic is selected, it is determined by the next open parameter:
Pm.max_children #, maximum number of child processes
Pm.start_servers #, number of processes at startup
Pm.min_spare_servers #, guarantees the minimum number of idle processes, and creates a new child process if the idle process is less than this value
Pm.max_spare_servers #, guarantees the maximum number of idle processes, and cleans if the idle process is greater than this value

pm.max_requests = 1000
#设置每个子进程重生之前服务的请求数. is useful for Third-party modules that may have memory leaks. If set to ' 0 ', the request is always accepted. Equivalent to php_fcgi_max_requests environment variables. Default value: 0.

Pm.status_path =/status
#FPM状态页面的网址. If it is not set, the status page cannot be accessed. Default value: None. Munin monitoring will use the

Ping.path =/ping
#FPM监控页面的ping网址. If it is not set, the ping page cannot be accessed. This page is used to externally detect if FPM is alive and can respond to requests. Please note that you must start with a slash (/).

Ping.response = Pong
#用于定义ping请求的返回相应. Returns the Text/plain-formatted text for HTTP 200. Default value: Pong.

Request_terminate_timeout = 0
#设置单个请求的超时中止时间. This option may be useful for the ' max_execution_time ' in the php.ini setting that does not abort the script for some special reason. Set to ' 0 ' means ' off '. You can try to change this option when a 502 error occurs frequently.

Request_slowlog_timeout = 10s
#当一个请求该设置的超时时间后, the corresponding PHP call stack information is fully written to the slow log. Set to ' 0 ' means ' off '

Slowlog = log/$pool. Log.slow
#慢请求的记录日志, use with Request_slowlog_timeout

Rlimit_files = 1024
#设置文件打开描述符的rlimit限制. Default value: The system-defined value default open handle is 1024, you can use Ulimit-n view, ulimit-n 2048 modify.

Rlimit_core = 0
#设置核心rlimit最大限制值. Available values: ' Unlimited ', 0, or positive integer. Default value: System-defined value.

Chroot =
#启动时的Chroot目录. The defined directory needs to be an absolute path. If it is not set, then chroot is not used.

ChDir =
#设置启动目录, it is automatically chdir to the directory when it is started. The defined directory needs to be an absolute path. Default value: Current directory, or/directory (chroot)

Catch_workers_output = yes
#重定向运行过程中的stdout和stderr到主要的错误日志文件中. If there are no settings, stdout and stderr will be redirected to/dev/null according to the FASTCGI rules. Default value: null.
Common Mistakes and solution sorting

A 1,request_terminate_timeout value that is set to 0 or too long may cause a file_get_contents resource problem.

If the remote resource requested by file_get_contents if the response is slow, file_get_contents will be stuck there will not time out, we know php.ini inside Max_execution_time can set the maximum PHP script execution times , however, in php-cgi (PHP-FPM), the parameter is not valid. The real ability to control the maximum execution time of a PHP script is the request_terminate_timeout parameter in the php-fpm.conf configuration file.

The default value for Request_terminate_timeout is 0 seconds, which means that the PHP script will continue to execute. In this way, when all the php-cgi processes are stuck in the file_get_contents () function, the WebServer of this nginx+php is no longer able to process the new PHP request, and Nginx will return "502 bad Gateway" to the user. Modify this parameter to set a PHP script maximum execution time is necessary, but the symptom does not cure the root causes. For example, to 30s, if file_get_contents () to get the content of the Web page is slow, which means that 150 php-cgi process, only 5 requests per second, WebServer also difficult to avoid "502 bad Gateway." The solution is to set the request_terminate_timeout to 10s or a reasonable value, or to add a timeout parameter to the file_get_contents.

$ctx = stream_context_create (Array (
' http ' => array (
' Timeout ' => 10//Set a timeout in seconds
)
)
);
file_get_contents ($str, 0, $ctx);


improper configuration of the 2,max_requests parameter may cause intermittent 502 errors:

http://hily.me/blog/2011/01/nginx-php-fpm-502/

pm.max_requests = 1000
#设置每个子进程重生之前服务的请求数. is useful for Third-party modules that may have memory leaks. If set to ' 0 ', the request is always accepted. Equivalent to php_fcgi_max_requests environment variables. Default value: 0.
This configuration means that the process is automatically restarted when the number of requests processed by a php-cgi process accumulates to 500.

But why restart the process?

Generally in the project, we will use more or less the Third-party library of PHP, these third-party libraries often have memory leaks, if not periodically restart the php-cgi process, it is bound to cause memory usage continues to grow. Therefore, as a php-cgi manager, PHP-FPM provides such a monitoring function to restart the php-cgi process with a specified number of requests to ensure that the amount of memory is not increased.

Precisely because of this mechanism, in the high concurrency of the site, often led to 502 errors, I guess the reason is php-fpm to the request from the NGINX queue did not deal well. However, I am currently using PHP 5.3.2, do not know if there is still this problem in the PHP 5.3.3.

Our solution now is to set this value as large as possible, to minimize the number of php-cgi SPAWN and to improve overall performance. In our own actual production environment, we found that the memory leak is not obvious, so we set this value very large (204800). We should set this value according to their actual situation, not blindly increase.

In other words, the purpose of this mechanism is only to ensure that the php-cgi not excessive memory, why not through the detection of memory methods to deal with it? I very much agree with Gao Chunhui that it is a better solution to restart the php-cgi process by setting the peak intrinsic footprint of the process.

3,PHP-FPM slow log, debug and abnormal troubleshooting artifacts:

Request_slowlog_timeout set a timeout parameter, Slowlog set the location of the slow log, tail-f/var/log/www.slow.log can see the slow execution of the PHP process.

We can see the frequent network read more than, the MySQL query too slow problem, according to the information and then troubleshoot the problem has a very clear direction

Upload a large file setting

Large file upload should pay attention to several links
1, upload files need to spend a long time to upload and processing execution time, you need to set Nginx upload time, the extension of PHP execution timeout time
2, large file processing needs to occupy a large memory, the need to increase the PHP memory pool, given the multiple file upload processing concurrency, this memory proposal based on concurrent multiplication

The following are the related configurations for each application

Modification of Nginx
Send_timeout 60;
Fastcgi_connect_timeout 300;
Fastcgi_send_timeout 300;
Fastcgi_read_timeout 300;
Client_max_body_size 30m;

Changes to PHP


Upload_max_filesize 500M
Post_max_size 500M
Max_input_time 300
Max_execution_time 300

PHP-FPM Note Parameters
Request_terminate_timeout
Request_slowlog_timeout
These two parameters, if set too small, cause the file to transfer a part and then the connection is closed.

Examples are as follows:

2014/07/08 23:00:40 [ERROR] 56794#0: *10350 upstream timed out (60:operation timed out) while reading response header fro M upstream, client:127.0.0.1, server:www.111cn.net, Request: "post/uploadurl/http/1.1", Upstream: "fastcgi://127.0.0." 1:9000 ", Host:

Extended fastcgi_read_timeout time due to back-end execution time timeout (Operation timed out) exceeding Nginx request time

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.