Nginx and php-fpm running process
- Nginx view the nginx.conf configuration file
- Load nginx's fast-cgi module
- php-fpm listens on 127.0.0.1:9000
- php-fpm receives the request and enables the worker process to process the request
- php-fpm processes the request and returns it to nginx
- nginx returns the result to the browser via http
Nginx and php-fpm communication mechanism
www.test.com | | Nginx | | route to www.test.com/index.php | | load nginx of fast-cgi module | | fast-cgi Monitor 127.0.0.1:9000 address | | www.test.com/index.php request reached 127.0.0.1:9000 | | waiting to be processed...
The combination of Nginx and php-fpm
On Linux, nginx communicates with php-fpm in two ways: tcp socket and unix socket.
The advantage of tcp socket is that it can be used across servers, which can only be used when nginx and php-fpm are not on the same machine.
Unix socket is also called IPC(inter-process communication) socket, which is used to realize inter-process communication on the same host. In this way, you need to fill in the socket file location of php-fpm in the nginx configuration file.
The data transmission process of the two methods is shown in the following figure:
See these two articles for details on the two ways of communication transmission:
TCP protocol send/receive data implementation
UDP protocol send/receive data implementation
php-fpm process management
php-fpm adopts the process mode of master-worker. in,
- The master is responsible for listening to the port and waiting for the link; secondly, the registration signal can be managed by the master through the information
- The worker is responsible for handling the specific logic
View php-fpm log information - Default path - /usr/local/php/var/log
php-fpm optimization
php.ini optimization
//By default, the server has a limit on the size of the uploaded file. If you want to modify the limit of the uploaded file, you can modify the php.ini file file_uploads = On; //Whether to allow uploading of files upload_max_filesize = 1024M; //Maximum limit for uploading files post_max_size = 1024M; //Most data submitted via post max_execution_time = 300; //The longest script execution time in seconds max_input_time = 30000; //Time limit for receiving submitted data in seconds memory_limit = 256M; //Maximum memory used per script; in safe mode, you cannot change this setting at runtime with ini_set().
php-fpm.conf optimization
(1)Process number setting pm = dynamic pm.max_children = 15 //Number of php-fpm processes started in static mode pm.start_servers = 5 //Number of starting php-fpm processes in dynamic mode pm.min_spare_servers = 5 //The php-fpm process with the smallest idle time in dynamic mode pm.max_spare_servers = 5 //The php-fpm process with the largest idle time in dynamic mode (2)Maximum number of requests processed The maximum number of processing requests refers to a php-fpm of worker The process terminates after processing how many requests, master process will restart respawn new. This configuration can avoid php Caused by the interpreter itself or by the program memory leaks. The default value is 500, pm.max_requests = 1024 Such a plan, 1 second Maximum number of requests: 15*1024=15360 Minimum number of requests: 5*1024=7120
How to avoid program hang
- Method 1: Set the timeout period of php-fpm execution to a fixed value
vi php-fpm.conf change into request_terminate_timeout = 60
- Method 2: reload php-fpm regularly
Periodically reload php-fpm on a server with high load
Reload can restart smoothly without affecting the php script running of the production system, reload every 15 minutes0-59/15 * * * * /usr/local/php/sbin/php-fpm reload
- Method 3: Optimize the process pool configuration
php-fpm dynamically creates subprocesses to process requests according to the content of the configuration file and the actual situation. It can only block when the maximum value that can be created is reached. Execute one by one. - Process optimization
pm = dynamic pm.max_children = 300 pm.start_servers = 20 pm.min_spare_servers = 5 pm.max_spare_servers = 35
- Process optimization
- Maximum number of requests optimization
Tip: This is used to deal with memory leaks caused by the PHP parser or referenced third-party libraries.pm.max_requests = 10240
Maximum number of requests: Refers to how many requests a php-fpm worker process will terminate after processing.
- Maximum number of requests optimization
- Maximum execution time optimization (php.ini)
Tip: This is used to deal with the 502 error reported because the PHP execution time is too long.request_terminate_timeout = 20
This duration configuration can be configured in php.ini (max_execution_time) or php-fpm.conf. In order not to affect the global configuration, crontab timing tasks can be implemented in php-fpm.conf to restart php-fpm smoothly. This way It is to use the crontab timing task to regularly query whether the website is 502. If it is 502, restart php-fpm smoothly
First create a script under the /root/ directory
Then give execute permission to this scriptvim restart-php-fpm.sh
Then it is to write the content of the script, on the codechmod +x /root/restart-php-fpm.sh
Note: I restarted using /etc/init.d/php7.2-fpm restart#!/bin/bash MY_URL="http://www.****.com/" RESULT=`curl -I $MY_URL|grep "HTTP/1.1 502"` if [ -n "$RESULT" ]; then /etc/init.d/php7.2-fpm restart fi
Then write a scheduled task
Use crontab -l to view the list of scheduled taskscrontab -e /root/restart-php-fpm.sh
- Maximum execution time optimization (php.ini)