Home > Backend Development > PHP7 > body text

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

coldplay.xixi
Release: 2023-02-17 15:50:01
forward
3981 people have browsed it

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

php.ini is the core configuration file for running PHP. The following are some common configurations

extension_dir=""

  • Set PHP extension library path

expose_php = Off

  • Avoid PHP information being exposed in http headers

display_errors = Off

  • Avoid exposing error messages when PHP calls mysql

##log_errors = On

    Enable 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 PHP's opcache and mysql dynamic library

date.timezone = PRC

    Set PHP time zone

opcache.enable=1

    Open 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 -The configuration file of the fpm process service. The following are some common configurations

error_log = /usr/local/php/logs/php-fpm.log

    Set the path of the error log

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

    Introduction Configuration in the www.conf file (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, the default is var/log/php-fpm.log

log_level = notice

    Error level. Available levels are: alert (must be processed immediately), error (error situation), warning (warning situation), notice (general important information), debug (debug information) ). Default: notice.

emergency_restart_threshold = 60

emergency_restart_interval = 60s

    means in If the number of php-cgi processes with 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

    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

    The number of backlogs, -1 means no limit, which is 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 other hosts nginx can also access this FPM process, and the listen location must be set to a local accessible IP. 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

    The account and group that started 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 started in static mode, in In dynamic mode, it limits 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

    The number of initial php-fpm processes in dynamic mode.

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

  • 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 pm.max_children parameter 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.
  • Maximum number of requests processed It means that a php-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. Will be used by munin monitoring

ping.path = /ping

  • The ping URL of the FPM monitoring page. If it is not set, the ping page cannot be accessed. This page uses Externally check whether the FPM is alive and can respond to requests. Please note that it must start with a slash (/).

ping.response = pong

  • # is used to define the return response of the ping request. The return is HTTP 200 text/plain format text. Default Value: pong.

request_terminate_timeout = 0

  • Set the timeout abort time for a single request. This option may affect the php.ini settings 'max_execution_time' is not useful for scripts that are not aborted for some special reasons. Setting it to '0' means 'Off'. You can try changing this option when 502 errors occur frequently.

request_slowlog_timeout = 10s

  • #When a request sets the timeout period, the corresponding PHP call stack information will be completely written to In the slow log. Set to '0' to mean 'Off'

slowlog = log/$pool.log.slow

  • slow request Record logs and use request_slowlog_timeout

rlimit_files = 1024

  • to set the rlimit limit of the file open descriptor. Default value: System-defined value can be used by default The open handle is 1024, which can be viewed using ulimit -n and modified with ulimit -n 2048.

rlimit_core = 0

  • Set the maximum core rlimit limit value. Available values: 'unlimited', 0 or positive integer. Default value: system Define value.

chroot =

  • Chroot directory at startup. The defined directory needs to be an absolute path. If not set, chroot Not used.

chdir =

  • Set the startup directory, which will be automatically Chdired to during startup. The defined directory needs to be absolute Path. Default value: current directory, or / directory (when chrooting)

catch_workers_output = yes

  • Redirect stdout and stderr to the main error log file. If not set, stdout and stderr will be redirected to /dev/null according to FastCGI's rules. Default value: empty.`

clear_env = no

##Common errors and solutions
  • The request timeout abort time is not set

If the value of request_terminate_timeout is set to 0 or too long, it may cause The PHP script will continue to execute. 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. set a Maximum execution time of PHP scripts is necessary, however, treating 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 PHP_FCGI_MAX_REQUESTS Environment variable. Default value: 0. The meaning of this configuration is 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 more or less use some third-party libraries of PHP. These third-party libraries often have memory leak problems. If the PHP-CGI process is not restarted regularly, It is bound to cause the memory usage to continue to grow. 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 /var/log/www.slow.log can see the php process that is executed 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.

    Recommended tutorial: "php tutorial"

The above is the detailed content of Detailed explanation of php.ini, php-fpm and www.conf configuration in PHP7. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:juejin.im
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!