Sample code sharing for PHP-FPM explanation

Source: Internet
Author: User
Tags http 200 mysql query


Contract several directories

    • /usr/local/php/sbin/php-fpm

    • /usr/local/php/etc/php-fpm.conf

    • /usr/local/php/etc/php.ini

One, the php-fpm start parameter

#测试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# Start 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# close Php-fpmkill-int ' cat/usr/local/php/var/run/php-fpm.pid ' # Restart PHP-FPMKILL-USR2 ' Cat/usr/local/php/var/run/php-fpm.pid '

Second, php-fpm.conf important parameters of the detailed

PID = run/php-fpm.pid#pid settings, default in the installation directory of Var/run/php-fpm.pid, it is recommended to turn on error_log = log/php-fpm.log# error log, default in the installation directory var/log/ Php-fpm.loglog_level = notice# error level. The available levels are: alert (which must be processed immediately), error (Error condition), warning (warning condition), notice (general important information), debug (debug information). Default: Notice.emergency_restart_threshold = 60emergency_restart_interval = 60s# is represented in Emergency_restart_ If the number of php-cgi processes with SIGSEGV or sigbus errors within the values set by interval is more than Emergency_restart_threshold, PHP-FPM will be gracefully restarted. These two options generally leave the default values. Process_control_timeout = 0# Sets the time-out period for the child process to accept the primary process multiplexing signal. Available units: s (seconds), M (min), H (Hours), or D (day) default units: s (seconds). Default value: 0.daemonize = yes# background performs fpm, default is yes, if for debugging you can change to No. 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 Listening port, that is, the address of PHP processing in Nginx, the general default value can be. The 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, the operating system determines that this line is commented out. Backlog meaning reference: http://www.3gyou.cc/?p=41listen.allowed_clients = 127.0.0.1# Allow access to the IP of the fastcgi process, set any to unlimited IP, If you want to set the nginx of another host to access this FPM process, listen to set the IP that can be accessed at a cost. The default value is any. Each address is separated by commas. Allow any server to request a connection if it is not set or empty Listen.owner = WwwlisteN.group = Wwwlisten.mode = 0666#unix Socket setting option, if accessed using TCP, Comment here. user = Wwwgroup = www# The account and group that started the process pm = Dynamic #对于专用服务器, PM can be set to static. #如何控制子进程, the options are static and dynamic. If Static is selected, the number of fixed child processes is specified by Pm.max_children. If dynamic is selected, it is determined by the following argument: Pm.max_children #, maximum number of child processes Pm.start_servers #, number of processes at startup Pm.min_spare_servers #, minimum number of idle processes guaranteed, If the idle process is less than this value, the new child process Pm.max_spare_servers # is created, the maximum number of idle processes is guaranteed, and if the idle process is greater than this value, this is cleaned pm.max_requests = 1000# Sets the number of requests for the service before each child process is reborn. is useful for third-party modules that may have a memory leak. If set to ' 0 ', the request is always accepted. Equivalent to the PHP_FCGI_MAX_REQUESTS environment variable. Default value: 0.pm.status_path = URL of the/STATUS#FPM status page. If not set, the status page cannot be accessed. Default value: None. The Munin monitor uses the ping URL to the Ping.path =/PING#FPM monitoring page. If not set, the ping page cannot be accessed. This page is used to externally detect whether FPM is alive and can respond to requests. Note that you must start with a slash (/). Ping.response = pong# is used to define the return of the ping request accordingly. Returns the Text/plain format text for HTTP 200. Default value: Pong.request_terminate_timeout = 0# Sets the time-out for a single request to abort. This option may be useful for scripts in php.ini settings where ' Max_execution_time ' is not aborted for some special reason. Request_slowlog_timeout = 10s# When a time-out is requested for this setting, the corresponding PHP call stack information is written to the slow log in its entirety. Set to ' 0 ' to indicate ' Off ' Slowlog = log/$pool. log.slow# Slow-Request logging, with Request_slowlog_Timeout uses rlimit_files = 1024# to set the Rlimit limit for the file opening descriptor. Default: System-defined value default open handle is 1024, can be viewed using ULIMIT-N, ulimit-n 2048 modified. Rlimit_core = 0# Sets the maximum limit value for the core 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 not set, Chroot is not used. ChDir = #设置启动目录, which is automatically chdir to the directory at startup. The defined directory needs to be an absolute path. Default: The current directory, or/directory (chroot) catch_workers_output = yes# redirect stdout and stderr into the primary error log file during the run. If not set, stdout and stderr will be redirected to/dev/null according to the FASTCGI rules. Default value: Empty.

Three, common mistakes and solutions to organize

Resource problems caused by 1,request_terminate_timeout

The value of Request_terminate_timeout, if set to 0 or too long, can cause file_get_contents resource problems.

If the remote resource requested by file_get_contents is too slow, file_get_contents will remain stuck there and will not time out. We know that php.ini inside Max_execution_time can set the maximum execution time for PHP scripts, but in php-cgi (PHP-FPM), this parameter does not work. 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, meaning that the PHP script will continue to execute. This way, when all the php-cgi processes are stuck in the file_get_contents () function, the nginx+php WebServer can no longer process the new PHP request, and Nginx will return "502 bad Gateway" to the user. To modify this parameter, it is necessary to set the maximum execution time for a PHP script, but the symptom is not a cure. For example, to 30s, if file_get_contents () to get a slow page content, which means that 150 php-cgi process, only 5 requests per second, WebServer also difficult to avoid "502 bad Gateway."
The workaround is to set the request_terminate_timeout to 10s or a reasonable value, or to add a timeout parameter to file_get_contents.

$ctx = stream_context_create (Array ('    http ' = = Array (       ' timeout ' =/    /Set a time-out, in seconds))    ; file_ Get_contents ($str, 0, $ctx);

Improper configuration of the 2,max_requests parameter may cause an intermittent 502 error:

pm.max_requests = 1000

Sets the number of requests for the service before each child process is reborn. is useful for third-party modules that may have a memory leak. If set to ' 0′, the request is always accepted. Equivalent to the PHP_FCGI_MAX_REQUESTS environment variable. 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 more or less use some PHP third-party libraries, these third-party libraries often have a memory leak problem, if you do not periodically restart the php-cgi process, it is bound to cause memory usage is increasing. So php-fpm, as the manager of PHP-CGI, provides a monitoring function that restarts the PHP-CGI process that requests a specified number of times to ensure that the amount of memory is not increased.

It is because of this mechanism, in high-concurrency sites, often lead to 502 of errors, I guess the reason is that php-fpm to the request from NGINX queue is not handled well. However, I am still using PHP 5.3.2, I do not know if there is a problem in PHP 5.3.3.

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

In other words, the purpose of this mechanism is only to ensure that the php-cgi does not take up too much memory, so why not handle it by detecting memory? I agree with Gao Chunhui that it would be a better solution to restart the php-cgi process by setting the peak intrinsic consumption of the process.

3,PHP-FPM slow log, Debug and exception troubleshooting artifact:
Request_slowlog_timeout set a time-out parameter, Slowlog set the slow log storage location

Tail-f/var/log/www.slow.log

The above command can see the slow-running PHP process.
You can see the frequent occurrence of network read more than, MySQL query too slow problem, according to the prompt information to troubleshoot the problem there is a clear direction.

PHP-FPM 5.3+ How do I turn off reboot?

PHP-FPM in PHP 5.3+ no longer supports commands such as/USR/LOCAL/PHP/SBIN/PHP-FPM (Start|stop|reload) that php-fpm previously had, and signal control is required:

The master process can understand the following signals

INT, term immediately terminate QUIT smooth terminate USR1 reopen log file USR2 Smooth reload all worker processes and reload the configuration and binary modules

Example:

PHP-FPM off:

Kill-intcat /usr/local/php/var/run/php-fpm.pid

PHP-FPM Restart:

Kill-usr2cat /usr/local/php/var/run/php-fpm.pid

To view the number of PHP-FPM processes:

PS aux | Grep-c PHP-FPM

8. Execute PHP at the command line, prompting for the command not found

-bash:/usr/bin/php:no such file or directory

Vi/etc/profile

Add a line of configuration at the bottom of the file
Export Path=/usr/local/php/bin: $PATH

Save exit

Source/etc/profile

Related Article

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.