Sample code for php-fpm interpretation

Source: Internet
Author: User
Tags http 200
Conventions for directory usrlocalphpsbinphp-fpmusrlocalphpetcphp-fpm.confusrlocalphpetcphp.ini


Specify directories
  • /Usr/local/php/sbin/php-fpm

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

  • /Usr/local/php/etc/php. ini

I. startup parameters of php-fpm
# Test 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'
2. detailed explanation of important parameters of php-fpm.conf
Pid = run/php-fpm.pid # pid settings, default var/run/php-fpm.pid in the installation directory, it is recommended to enable error_log = log/php-fpm.log # Error log, var/log/php-fpm.loglog_level = notice # Error level in the installation directory by default. available level: alert (must be handled immediately), error (error), warning (warning), notice (important information), debug (debugging information ). default value: notice. expiration = 60emergency_restart_interval = 60 s # indicates that the number of php-cgi processes with SIGSEGV or SIGBUS errors within the value set by emergency_restart_restart_interval exceeds that of emergency_restart_threshold, and P-fpm will be restarted elegantly. These two options are generally kept by default. Process_control_timeout = 0 # set the timeout time for the sub-process to receive the multiplexing signal of the main process. available unit: s (seconds), m (minutes), h (hours), or d (days) default unit: s (seconds ). default value: 0. daemonize = yes # fpm is executed in the background. the default value is yes. you can change it to no for debugging. In FPM, you can use different settings to run multiple process pools. These settings can be set separately for each process pool. Listen = 127.0.0.1: 9000 # fpm listening port, that is, the address processed by php in nginx. Generally, the default value is enough. Available Formats: 'Ip: port', 'port', '/path/to/unix/socket '. each process pool needs to be set. listen. backlog =-1 # Number of backlogs.-1 indicates no limit. this parameter is determined by the operating system and can be commented out. For details about backlog, refer: http://www.3gyou.cc/?p=41listen.allowed_clients = 127.0.0.1 # allow access to the IP address of the FastCGI process. set any to an unlimited IP address. if you want to set nginx of other hosts, you can also access this FPM process, set the IP address that can be accessed at a cost in listen. The default value is any. Each address is separated by a comma. if it is not set or empty, any server is allowed to request connection to listen. owner = wwwlisten. group = wwwlisten. mode = 0666 # unix socket setting options. if you use tcp access, comment it out here. User = wwwgroup = www # account for starting the process and group pm = dynamic # for dedicated servers, pm can be set to static. # The options for controlling sub-processes include static and dynamic. If static is selected, a fixed number of sub-processes is specified by pm. max_children. If dynamic is selected, it is determined by the following parameters: pm. max_children #, maximum number of child processes pm. start_servers #, number of processes at startup pm. min_spare_servers # ensures the minimum number of idle processes. if the number of idle processes is smaller than this value, a new sub-process pm is created. max_spare_servers # ensures the maximum number of idle processes. if the number of idle processes exceeds this value, clean pm. max_requests = 1000 # set the number of service requests before each sub-process is reborn. it is very useful for third-party modules that may have memory leaks. if it is set to '0', the request is always accepted. equivalent to the PHP_FCGI_MAX_REQUESTS environment variable. default value: 0. pm. status_path =/status # FPM status page URL. if not set, the status page cannot be accessed. default value: none. munin monitoring uses ping. path =/ping # FPM monitoring The ping URL of the page. if not set, you cannot access the ping page. this page is used to check whether FPM is alive and can respond to requests. Note that it must start with a slash (/). Ping. response = pong # defines the response of ping requests. returns the text in the text/plain format of HTTP 200. default value: pong. request_terminate_timeout = 0 # set the timeout duration of a single request. this option may apply to php. the 'max _ execution_time 'in ini settings is not useful for stopping the running script for some special reasons. request_slowlog_timeout = 10 s # When a request times out, the corresponding PHP call stack information is completely written to the slow log. set '0' to 'off' slowlog = log/$ pool. log. slow # slow Request Logging, used with request_slowlog_timeout to use rlimit_files = 1024 # set the rlimit limit of the file opening descriptor. default value: the default value is 1024. Use ulimit-n to view and modify ulimit-n 2048. Rlimit_core = 0 # set the maximum limit value of the core rlimit. available value: 'limited', 0, or positive integer. default value: System defined value. chroot = # The Chroot directory at startup. the defined directory must be an absolute path. if not set, chroot is not used. chdir = # set the Startup directory, which will be automatically Chdir to the directory at startup. the defined directory must be an absolute path. default value: the current directory or/Directory (for chroot) catch_workers_output = yes # Redirects stdout and stderr during running to the main error log file. if not set, stdout and stderr will be redirected to/dev/null according to FastCGI rules. default value: Null.
III. common errors and solutions

1. resource problems caused by request_terminate_timeout

If the value of request_terminate_timeout is set to 0 or is too long, it may cause resource problems of file_get_contents.

If the response to 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 max_execution_time in php. ini can set the maximum execution time of PHP scripts. However, this parameter does not take effect in php-cgi (php-fpm. The request_terminate_timeout parameter in the php-fpm.conf configuration file is really able to control the maximum execution time of PHP scripts.

The default value of request_terminate_timeout is 0 seconds. that is to say, the PHP script will continue to be executed. In this way, when all php-cgi processes are stuck in the file_get_contents () function, the Nginx + PHP WebServer can no longer process new PHP requests, nginx returns "502 Bad Gateway" to the user ". Modify this parameter to set the maximum execution time of a PHP script. For example, if file_get_contents () is changed to 30 s, and the webpage content is slow to be obtained, this means that 150 php-cgi processes can only process 5 requests per second, webServer is also difficult to avoid "502 Bad Gateway ".
The solution is to set request_terminate_timeout to 10 s or a reasonable value, or add a timeout parameter to file_get_contents.

$ Ctx = stream_context_create (array ('http' => array ('timeout' => 10 // set a timeout value, in seconds); file_get_contents ($ str, 0, $ ctx );

2. if the max_requests parameter is improperly configured, an intermittent 502 error may occur:

pm.max_requests = 1000

Sets the number of service requests before each sub-process is reborn. it is very useful for third-party modules that may have memory leaks. if it is 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 do we need to restart the process?
Generally in the project, we will use more or less some PHP third-party libraries, these third-party libraries often have memory leakage problems, if occasionally restart the PHP-CGI process, will inevitably cause memory usage continues to grow. As a result, the PHP-FPM, as the manager of the PHP-CGI, provides such a monitoring function to restart the PHP-CGI process that requests reach a specified number of times to ensure that memory usage does not grow.

It is precisely because of this mechanism that often leads to 502 errors in highly concurrent sites, I guess the reason is that the PHP-FPM did not handle the request queue from NGINX. However, I am still using PHP 5.3.2. I don't know if this problem still exists in PHP 5.3.3.

At present, our solution is to set this value as much as possible, as much as possible to reduce the number of PHP-CGI re-SPAWN, but also can improve the overall performance. In our actual production environment, we found that the memory leakage was not obvious, so we set this value to a very large value (204800 ). You must set this value based on your actual situation and do not blindly increase it.

Back then, the purpose of this mechanism is only to ensure that the PHP-CGI is not fully occupied by memory, why not by detecting the memory to deal with it? I agree with Gao Chunhui's saying that restarting the PHP-CGI process by setting the internal usage of the process's peak will be a better solution.

3. php-fpm slow logs, debugging and troubleshooting artifacts:
Request_slowlog_timeout sets a timeout parameter, and slowlog sets the storage location of slow logs.

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

The preceding command shows the php execution process that is too slow.
As you can see, frequent problems such as excessive network reads and slow MySQL queries exist. you can find a clear direction to troubleshoot the problem based on the prompt.

Php-fpm 5.3 + How do I disable restart?

Php-fpm in php 5.3 + does not support commands such as/usr/local/php/sbin/php-fpm (start | stop | reload) in php + versions, signal control is required:

The master process can understand the following signals:

INT, TERM immediately terminate QUIT smoothly terminate USR1 re-open the log file USR2 smoothly reload all worker processes and re-load the configuration and binary module

Example:

Disable php-fpm:

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

Php-fpm restart:

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

View the number of php-fpm processes:

Ps aux | grep-c php-fpm

8. execute php under the command line and prompt that the command cannot be 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 and exit

Source/etc/profile

The above is the details shared by the sample code explained by php-fpm. For more information, see other related articles in the first PHP community!

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.