Home Backend Development PHP Tutorial Detailed explanation of php configuration php-fpm startup parameters and configuration_PHP tutorial

Detailed explanation of php configuration php-fpm startup parameters and configuration_PHP tutorial

Jul 13, 2016 am 10:25 AM
php php-fpm

Agree several directories

/usr/local/php/sbin/php-fpm
/usr/local/php/etc/php-fpm.conf
/usr/local/php/etc/php.ini
1. Startup parameters of php-fpm

Copy code The code is as follows:

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

2. Detailed explanation of important parameters of php-fpm.conf
Copy codeThe code is as follows:

pid = run/php-fpm.pid
#pid setting, the default is 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.log in the installation directory by default

log_level = notice
#Error level. Available levels are: alert (required Process 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 if the number of php-cgi processes with SIGSEGV or SIGBUS errors exceeds emergency_restart_threshold within the value set by emergency_restart_interval, 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 reuse signal of the main process. 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, if it is for debugging, it can be changed to no. 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 not restrict IP, if To set up nginx on 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 a comma. If not set or empty, any server is allowed to request a connection

listen.owner = www
listen.group = www
listen.mode = 0666
#unix socket setting options, if you use tcp to access, just comment here.

user = www
group = www
# Account and group to start 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 #, to ensure the minimum number of idle processes , if the idle process is less than this value, create a new sub-process
pm.max_spare_servers # to ensure the maximum number of idle processes. If the idle process is greater than this value, clean it up

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.

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 FPM is alive and can respond to requests. Please note that it must start with a slash ( /).

ping.response = pong
# Used to define the return response of the ping request. The returned text/plain format text is HTTP 200. Default value: pong.

request_terminate_timeout = 0
#Set the timeout abort time for a single request. This option may be useful for scripts where the 'max_execution_time' in the php.ini setting does not abort running scripts for some special reasons. Set to '0' to mean 'Off'. When it occurs often You can try changing this option when receiving a 502 error.

request_slowlog_timeout = 10s
#When a request is set for the timeout period, the corresponding PHP call stack information will be completely written to the slow log. Setting it to '0' means '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, and it will be automatically Chdir to this directory at 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.

3. 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 with 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, if it is changed to 30s, if file_get_contents() is slow to obtain web page content, this means that 150 php-cgi processes can only handle 5 requests per second, and 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.

Copy code The code is as follows:

$ctx = stream_context_create(array(
'http' => array(
'timeout' => 10 //Set a timeout in seconds
)
));

file_get_contents($str, 0, $ctx);

2. Improper configuration of the max_requests parameter may cause intermittent 502 errors:
Copy code The code is as follows:

pm.max_requests = 1000

Set the number of requests served before each child process is reborn. Useful for third-party modules that may have memory leaks. If set to '0' Then it will always accept requests. It is 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, it will inevitably cause continuous memory usage. 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 high-concurrency 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

Copy the code The code is as follows:

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

The above command is enough See a php process that is executing too slowly.
You can see the common problems of excessive network reading and slow Mysql query. If you follow the prompt information to troubleshoot the problem, you will have a clear direction.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825014.htmlTechArticleAgree several directories/usr/local/php/sbin/php-fpm /usr/local/php/etc /php-fpm.conf /usr/local/php/etc/php.ini 1. Copy the code for the startup parameters of php-fpm as follows: #Test the php-fpm configuration...
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)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24
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 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,

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.

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

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.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 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: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

See all articles