Home  >  Article  >  Backend Development  >  php.ini, php-fpm and www.conf configuration in PHP7

php.ini, php-fpm and www.conf configuration in PHP7

藏色散人
藏色散人forward
2019-08-23 14:07:195055browse

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=""

● Set the PHP extension library path

expose_php = Off

● Avoid exposing PHP information in http headers

display_errors = Off

● Avoid exposing PHP errors when calling mysql Information

log_errors = On

● Turn on the PHP error log after turning off display_errors (the path is configured in php-fpm.conf)

zend_extension=opcache.so extension=mysqli.so extension=pdo_mysql.so

● Set up PHP’s opcache and mysql dynamic libraries

date.timezone = PRC

● Set the time zone of PHP

opcache.enable=1

● Turn on opcache

open_basedir = / usr/share/nginx/html;

● Set the directory that PHP scripts are allowed to access (needs to be configured according to the actual situation)

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

● Settings The path of the error log

include=/usr/local/php7/etc/php-fpm.d/*.conf

●Introduced into the www.conf file Configuration (set by default)

php-fpm.conf and main configuration information of www.conf

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 (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 multiplexing 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, you can change it 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

● The listening port 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.

listen.allowed_clients = 127.0.0.1

● Allow access to the IP of the FastCGI process. Set any to not restrict the IP. If you want to set up nginx on other hosts, you can also access it. For this FPM process, the listen location 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 requesting a connection is allowed

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

php-fpm process pool optimization method

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 number of php-fpm processes opened in static mode is limited in dynamic mode The maximum number of processes for php-fpm (note here that the value of pm.max_spare_servers can only be less than or equal to pm.max_children)

pm.start_servers

● In dynamic mode Number of starting php-fpm processes.

pm.min_spare_servers

● Ensure the minimum number of idle processes. If the idle processes are less than this value, create a new child process

pm.max_spare_servers

● Ensure the maximum number of idle processes. If the idle processes are greater than this value, they will be cleaned up

● 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!

Statement:
This article is reproduced at:juejin.im. If there is any infringement, please contact admin@php.cn delete