Home  >  Article  >  Backend Development  >  Detailed explanation of php-fpm startup parameters and important configurations

Detailed explanation of php-fpm startup parameters and important configurations

WBOY
WBOYOriginal
2016-07-30 13:30:53980browse

Detailed explanation of php-fpm startup parameters and important configurations

Agree several directories

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

One, the startup parameters of php-fpm

1

2

3

4

5

6

7

8

9

10

11

12

13

#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-fpm

kill -INT `cat/usr/local/php/var/run/php-fpm.pid`

#Restart php-fpm

kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`

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

1

2

3

4

5

6

7

8

9

2 2

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

7 2

73

74

75

76

77

78

79

pid = run

/php-fpm

.pid

#pid setting, the default is var/run/php-fpm.pid in the installation directory, it is recommended to turn on

error_log = log

/php-fpm

.log

#Error log, default is var/log/php-fpm.log in the installation directory

log_level = notice#Error level. Available levels are: alert (must be handled immediately), error (error situation), warning (warning situation), notice (general important information), debug (debugging information). Default: notice.

emergency_restart_threshold = 60

emergency_restart_interval = 60s#Indicates that the number of php-cgi processes that have SIGSEGV or SIGBUS errors within the value set by emergency_restart_interval exceeds emergency_restart_threshold, php-fpm will restart gracefully. These two options generally remain at their default values.

process_control_timeout = 0

#Set the timeout for the child process to accept the main process reuse signal. Available units: s (seconds), m (minutes), h (hours), or d (days) Default unit: s (seconds). Default value: 0.

daemonize =

yes

#Execute fpm in the background, the default value is yes, it can be changed to no for debugging. In FPM, it is possible to run multiple process pools with different settings. These settings can be set individually for each process pool.

listen = 127.0.0.1:9000

#fpm listening port, which is the address processed by php in nginx. Generally, the default value is sufficient. Available formats are: 'ip:port', 'port', '/path/to/unix/socket'. Each process pool needs to be set.

listen.backlog = -1

#Backlog number, -1 means unlimited, determined by the operating system, just comment out this line. Backlog meaning reference: http://www.3gyou.cc/?p=41

listen.allowed_clients = 127.0.0.1

# Allow access to the IP of the FastCGI process. Set any to unrestricted IP. If you want to set nginx of other hosts to also access this FPM process, the listen must be set to a local IP that can be accessed. The default value is any. Each address is separated by commas. If not set or empty, any server requesting a connection is allowed

listen.owner = www

listen.group = www

listen.mode = 0666

#unix Socket setting options, if accessed using tcp, just comment here.

user = www

group = www

#The account and group that started the process

pm = dynamic #For dedicated servers, pm can be set to static.

#How to control the child process, the options are static and dynamic. If static is selected, a fixed number of child processes is specified by pm.max_children. If dynamic is selected, it is determined by the following parameters:

pm.max_children #, the maximum number of child processes

pm.start_servers #, the number of processes at startup

pm.min_spare_servers #, guarantee the minimum number of idle processes. If the idle processes are less than this value, create a new child process

pm.max_spare_servers #, guarantee the maximum number of idle processes. If the idle processes are greater than this value, this will be cleaned up

pm.max_requests = 1000

#Set the number of requests served before each child process is reborn. Very useful for third-party modules that may have memory leaks. If set to '0', requests will always be accepted. Equivalent to the PHP_FCGI_MAX_REQUESTS environment variable. Default value: 0.

pm.status_path = /status

#The URL of the FPM status page. If not set, the status page cannot be accessed. Default value: none. Munin monitoring will use

ping.path = /ping

#The ping URL of the FPM monitoring page. If not set, the ping page cannot be accessed. This page is used to externally detect whether the FPM is alive and can respond to requests. Note that it must start with a slash (/).

ping.response = pong

# Used to define the return response of the ping request. Returns text/plain formatted text as HTTP 200. Default value: pong.

request_terminate_timeout = 0

#Set the timeout for a single request. This option may be useful if the 'max_execution_time' in the php.ini setting does not abort the running script for some special reason. Setting it to '0' means 'Off'. You can try changing this option when 502 errors occur frequently.

request_slowlog_timeout = 10s

#When a request reaches the set timeout, the corresponding PHP call stack information will be completely written to the slow log. Set to '0' for 'Off'

slowlog = log/$pool.log.slow

#Slow request logging, used with request_slowlog_timeout

rlimit_files = 1024

#Set the rlimit limit of the file open descriptor. Default value: The system-defined value of the default openable handle is 1024, which can be viewed using ulimit -n and modified with ulimit -n 2048.

rlimit_core = 0

#Set the maximum limit value of core rlimit. Available values: 'unlimited', 0 or positive integer. Default value: system defined value.

chroot =

#Chroot directory at startup. The defined directory needs to be an absolute path. If not set, chroot will not be used.

chdir =

#Set the startup directory, which will be automatically Chdired to during startup. The defined directory needs to be an absolute path. Default value: current directory, or /directory (when chrooting)

catch_workers_output = yes

#Redirect stdout and stderr during the running process to the main error log file. If not set, stdout and stderr will be redirected to /dev/null according to FastCGI rules. Default value: empty.

Three, common errors and solutions

1. Resource problems caused by request_terminate_timeout

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

If the remote resource requested by file_get_contents responds too slowly, file_get_contents will always be stuck there without timing out. We know that max_execution_time in php.ini can set the maximum execution time of PHP scripts, but in php-cgi (php-fpm), this parameter will not take effect. What can really control the maximum execution time of PHP scripts is the request_terminate_timeout parameter in the php-fpm.conf configuration file.

The default value of request_terminate_timeout is 0 seconds, which means that the PHP script will continue to execute. In this way, when all php-cgi processes are stuck in the file_get_contents() function, this Nginx+PHP WebServer can no longer handle new PHP requests, and Nginx will return "502 Bad Gateway" to the user. Modifying this parameter is necessary to set the maximum execution time of a PHP script, but it only treats the symptoms rather than the root cause. For example, change it to 30s, if file_get_contents() occurs When retrieving web page content is slow, this means that 150 php-cgi processes can only handle 5 requests per second. It is also difficult for WebServer to avoid "502 Bad Gateway". The solution is to set request_terminate_timeout to 10s or a reasonable value, or add a timeout parameter to file_get_contents.

1

2

3

4

5

6

7

$ctx = stream_context_create(array(

) 'http' => array(

                                                                                             10 //Set a timeout in seconds

)

));

file_get_contents($str, 0, $ctx);

2

pm.max_requests = 1000

Set the number of requests served before each child process is reborn. This is very useful for third-party modules that may have memory leaks. If set to '0', requests will always be accepted. Equivalent to the PHP_FCGI_MAX_REQUESTS environment variable. Default value: 0.

This configuration means that when the number of requests processed by a PHP-CGI process accumulates to 500, the process will be automatically restarted. But why restart the process?
Generally in projects, we will use some third-party libraries of PHP to some extent. These third-party libraries often have memory leak problems. If the PHP-CGI process is not restarted regularly, the memory usage will inevitably increase. Therefore, PHP-FPM, as the manager of PHP-CGI, provides such a monitoring function to restart the PHP-CGI process that has requested a specified number of times to ensure that the memory usage does not increase.
It is precisely because of this mechanism that 502 errors are often caused in highly concurrent sites. I guess the reason is that PHP-FPM does not handle the request queue coming from NGINX well. However, I am still using PHP 5.3.2. I don’t know if this problem still exists in PHP 5.3.3.

Our current solution is to set this value as large as possible to reduce the number of times PHP-CGI re-SPAWNs as much as possible, while also improving overall performance. In our own actual production environment, we found that the memory leak was not obvious, so we set this value very large (204800). Everyone should set this value according to their actual situation and cannot blindly increase it.

Having said that, the purpose of this mechanism is only to ensure that PHP-CGI does not occupy excessive memory. Why not deal with it by detecting memory? I very much agree with what Gao Chunhui said, restarting the PHP-CGI process by setting the peak intrinsic usage of the process would be a better solution.

3, php-fpm's slow log, debug and exception troubleshooting artifact:

request_slowlog_timeout sets a timeout parameter, slowlog sets the storage location of the slow log

1

tail -f

/var/log/www.slow.log

The above introduces the detailed explanation of php-fpm startup parameters and important configuration, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn