Home > Article > Backend Development > php.ini, php-fpm and www.conf configuration in PHP7
PHP7Configuration of php.ini, php-fpm and www.conf
php.ini is php running Core configuration file, the following are some common configurations
##extension_dir=""
expose_php = Off
display_errors = Off
log_errors = On
zend_extension=opcache.so extension=mysqli.so extension=pdo_mysql.so
date.timezone = PRC
opcache.enable=1
open_basedir = / usr/share/nginx/html;
php-fpm.conf is php-fpm The configuration file of the process service, the following are some common configurations
error_log = /usr/local/php/logs/php-fpm.log
include=/usr/local/php7/etc/php-fpm.d/*.conf
php-fpm.conf and main configuration information of www.conf
pid = run/php-fpm.pid
error_log = log/php-fpm.log
log_level = notice
emergency_restart_threshold = 60
emergency_restart_interval = 60s
process_control_timeout = 0
daemonize = yes
listen = 127.0.0.1:9000
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = www listen.group = www listen.mode = 0666
user = www group = www
php-fpm process pool optimization method
pm = dynamic
pm.max_children
pm.start_servers
pm.min_spare_servers
pm.max_spare_servers
● If dm is set to static, then only the parameter pm.max_children will take effect. The system will open the number of php-fpm processes set by the parameters. A php-fpm process will probably occupy 20m-40m of memory, so its numerical size setting should be set according to the size of your physical memory. You should also pay attention to other memory occupations, such as databases, system processes, etc., to determine The setting values of the above 4 parameters!
● If dm is set to dynamic, all four parameters will take effect. The system will start pm.start_servers php-fpm processes when php-fpm starts running, and then dynamically adjust the number of php-fpm processes between pm.min_spare_servers and pm.max_spare_servers according to the needs of the system. The parameter requires the value of pm.start_servers to be between pm.min_spare_servers and pm.max_spare_servers.
pm.max_requests = 1000
● Set the number of requests served before each child process is reborn.
● The maximum number of requests processed refers to a php -The fpm worker process will be terminated after processing a number of requests, and the master process will respawn a new one.
● The main purpose of this configuration is to avoid memory leaks caused by the php interpreter or third-party libraries referenced by the program.
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 the 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 not abort due to some special reasons for the 'max_execution_time' in the php.ini setting Useful for running scripts. Set to '0' for 'Off'. You can try changing this option when 502 errors occur frequently.
request_slowlog_timeout = 10s
● When a request exceeds the set timeout, the corresponding PHP call stack information will be completely written to the slow log. Settings '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 open handle is 1024, which can be viewed using ulimit -n, ulimit -n 2048 modified.
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 based on FastCGI The rules are redirected to /dev/null . Default value: empty.`
clear_env = no
● Clean up the environment
variables_order
The variables_order parameter is explained in detail in another articlehttps://www.php.cn/php-weizijiaocheng-429277.html
Common errors and solutions
The request timeout is not set
● If the value of request_terminate_timeout is set to 0 or too long, it may Causes the PHP script to continue executing. In this way, when all php-cgi processes are stuck in the file_get_contents() function, the Nginx PHP WebServer can no longer process new PHP requests, and Nginx will return "502 Bad Gateway" to the user. Setting the maximum execution time of a
PHP script is necessary, but it 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!
Improper configuration of the max_requests parameter
● Improper configuration of the max_requests parameter may cause intermittent 502 errors:
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 do we need to 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 memory usage. Growing. 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.
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, tail -f / You can see the php process that executes too slowly in var/log/www.slow.log.
You can see the common problems of excessive network reading and slow Mysql query. If you troubleshoot the problem according to the prompt information, you will have a clear direction.
The above is the detailed content of php.ini, php-fpm and www.conf configuration in PHP7. For more information, please follow other related articles on the PHP Chinese website!