Home Backend Development PHP Tutorial Detailed explanation of php-fpm startup parameters and important configurations

Detailed explanation of php-fpm startup parameters and important configurations

Jul 30, 2016 pm 01:30 PM
cgi fpm nbsp php

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

See all articles