search
HomeBackend DevelopmentPHP TutorialDetailed explanation of php-fpm startup parameters and important configurations

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
PHP's Purpose: Building Dynamic WebsitesPHP's Purpose: Building Dynamic WebsitesApr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP: Handling Databases and Server-Side LogicPHP: Handling Databases and Server-Side LogicApr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO)How do you prevent SQL Injection in PHP? (Prepared statements, PDO)Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python: Code Examples and ComparisonPHP and Python: Code Examples and ComparisonApr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 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.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment