Home  >  Article  >  Backend Development  >  php-fpm.conf configuration file Chinese and important parameter descriptions

php-fpm.conf configuration file Chinese and important parameter descriptions

coldplay.xixi
coldplay.xixiforward
2020-08-08 16:47:112848browse

php-fpm.conf configuration file Chinese and important parameter descriptions

php-fpm workflow

php-fpm The full name is PHP FastCGI Process Manager

After php-fpm is started, it will first read php.ini, and then read the corresponding conf configuration file. The conf configuration can override the php.ini configuration.

After starting php-fpm, a master process will be created to listen to port 9000 (configurable). The master process will create several sub-processes based on fpm.conf/www.conf. The sub-processes are used to handle actual Business.

When a client (such as nginx) connects to port 9000, the idle child process will accept it by itself. If all child processes are busy, the new connection to be accepted will be put into the queue by the master. , waiting for the fpm sub-process to be idle;

The length of the queue that stores semi-connections to be accepted is configured by listen.backlog.

Related learning recommendations: php programming (video)

All relative paths in the configuration are relative to the installation of php path.

In addition to the php-fpm.conf configuration file, there are usually other *.conf configuration files (or not, configure directly in php-fpm.conf) for configuring the process pool and different processes. Pools can be executed by different users, listen to different ports, and handle different tasks; multiple process pools share a global configuration.

include=/opt/remi/php56/root/etc/php-fpm.d/*.conf Load other configuration files.

php-fpm global configuration parameters Chinese description:

pid = /opt/remi/php56/root/var/run/php-fpm/php-fpm.pid
#pid process file, default is none.

error_log = /opt/remi/php56/root/var/log/php-fpm/error.log
#Error log location, default: installation path #INSTALL_PREFIX#/log/php-fpm. log. If set to syslog, the log will be sent to the syslogd service without being written to the file.

syslog.facility = daemon
#Write the log into the system log. Linux is not familiar enough yet, so ignore it for now.

syslog.ident = php-fpm
#System log identifier. If multiple fpm processes are running, you need to use this to distinguish whose log it belongs to.

log_level = notice
#Log level, default notice, optional: alert, error, warning, notice, debug

emergency_restart_threshold = 60
#With the following emergency_restart_interval parameter

emergency_restart_interval = 60s
#If the number of child processes with SIGSEGV or SIGBUS exceeds the value set by the emergency_restart_threshold parameter within the time set by this parameter, then fpm will restart gracefully. A value of 0 means off this function. , available units are: s seconds, m minutes, h hours, d days.

process_control_timeout = 0
#Set the timeout for the child process to accept the main process multiplexing signal. I understand this every day. After this time, it cannot be reused?

process.max = 128
#When dynamically managing child processes, fpm can fork the most number of processes. 0 means no limit. This is the total number of child processes that can be started by all process pools. Use with caution.

process.priority = -19
# Set the priority of the child process, which is effective when the master process is started as the root user; if not set, the child process will inherit the priority of the master process, and the value range is - 19 (highest) to 20 (lowest), not set by default.

daemonize = yes
#Set to no for debugging bugs, the default is yes.

rlimit_files = 1024
#Set the maximum number of files that the master process can open. The default is the system value.

rlimit_core = 0
#Master process core rlimit limit value; optional unlimited or an integer >=0, the default is the system value.

events.mechanism = epoll
#Event processing mechanism, automatic detection by default, optional values: select, poll, epoll (linux>=2.5.44), kqueue, /dev/poll, port

systemd_interval = 10s
#When fpm is set as a system service, how often the status is reported to the server, the units are s, m, h.

php-fpm process pool configuration pool Definitions:

You can run anything under different listening ports and different management options. There is no limit to the number of pools;

The name of the pool is used for logs and stats.

user = apache

group = apache

#Run pool fpm with the permissions of which user and group.

Use apache to access certain directories like httpd service

listen = 127.0.0.1:9000
#The listening ip and port can be /path/to/unix/socket To monitor unix socket, the performance is better.

listen.backlog = 65535
#The size of the unaccepted socket queue, -1 on FreeBSD and OpenBSD, the default is 65535 on other platforms, important when concurrency is high, reasonable settings will process queued requests in a timely manner; too There is too much backlog in the conference. After processing, nginx waits for timeout and disconnects the socket connection with fpm, which is a failure. Do not use -1, it is recommended to be above 1024, preferably a power of 2.

#A pool shares a backlog queue, and all pool processes go to this queue to accept connections.

#The maximum number is limited by the system configuration cat /proc/sys/net/core/somaxconn, system configuration modification: vim /etc/sysctl.conf, add net.core.somaxconn = 2000, the maximum is 2000, and then php The maximum backlog can be up to 2000.

listen.owner = apache

listen.group = apache

listen.mode = 0660

#When using the socket connection method, specify the unix socket The user with permissions is the same as the running user by default; when using tcp connection, you can comment out

listen.allowed_clients = 127.0.0.1
#Set the address that is allowed to connect to fpm, for example, nginx will connect, multiple The addresses are separated by commas. If not configured, any address can be connected by default.

process.priority = -19
#The permissions of the pool process also require the master process to be the root user. It is the same as the global one. If not set, the priority of the master process will be inherited.

pm = dynamic
# Child process management method at startup, optional values: static (specified number created at startup), dynamic (created according to the situation at startup, at least one), ondemand (start Child processes are not created at this time, and will be created only when needed)

pm.max_children = 5
#This pool can have up to 5 processes at the same time, and all three management methods must be configured

pm. start_servers = 2
#Create 2 child processes when fpm starts, only applicable to dynamic dynamic management method

pm.min_spare_servers = 2
#At least 2 child processes will be maintained when the server is idle. If this number is not enough, it will Create, only applicable to dynamic dynamic management method

pm.max_spare_servers = 3
#There must be at most a few servers when they are idle, if there are too many, they will be killed, only applicable to dynamic dynamic management method

pm .process_idle_timeout = 10s
#The child process will be killed after being idle for 10s.

pm.max_requests = 500
#Each child process will be recycled after processing a maximum of 500 requests, which can prevent memory leaks.

pm.status_path string

#The URL of the FPM status page. If not set, the status page cannot be accessed. Default: None.

ping.path string

#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 able to respond to requests. Note that it must start with a slash (/).

ping.response string
#Used to define the return response of the ping request. Returns text/plain formatted text as HTTP 200. Default value: pong.

process.priority int
#Set the worker's nice(2) priority (if set). The value ranges from -19 (highest priority) to 20 (lower priority). Default value: Do not set

prefix string
#The prefix used when detecting the path

access.log = var/log/$pool.access.log
#Access file log , is of little use. For example, yii2 records access to index.php every time, but only records the real PHP file.

slowlog = var/log/$pool.log.slow
#The log of PHP file execution that is too slow will accurately record which line of code is too slow. This is very useful when setting the time. Take effect.

request_slowlog_timeout = 2s
#Slow log will be written if this running time is exceeded

request_terminate_timeout = 3s
#The timeout of a single request, sometimes the maximum execution set by php.ini If the time has not taken effect, this will kill the request that took too long to execute.

rlimit_files = 1024
#The maximum number of open handles, the default is the system value.

rlimit_core = 0
#The maximum number of cores used, the default is system allocation.

chroot = /path
#The path must be an absolute path. Changing the directory of the child process can isolate the process's reading and writing of the file system from the actual operating system file system, which is good for security.

chdir = /var/www

#Change the current working directory, you can use a relative path, the default is the current directory or chroot.

catch_workers_output = yes
#Redirect standard output stdout and standard error stderr to the main error log. If not set, these two logs will be directed to /dev/null. Under high load conditions, This configuration will cause the page to be delayed for several milliseconds and is not enabled by default.

clear_env = no
#Whether to clear the environment variables when creating the work process, if yes, then the child process getenv() will not be able to access $_ENV and $_SERVER.

security.limit_extensions = .php .php3 .php4 .php5
#For security, limit the script suffix that can be executed

#Specify additional php.ini configuration for the current pool, such as Specify where the error log of the current pool is written

php_value/php_flag
#The content of php.ini can be set and can be overridden by ini_set

php_admin_value/php_admin_flag
#This is the same as above , but will not be overwritten by ini_set.

#When flag is set, the value can only be on, off, 1, 0, true, false, yes or no. Other types of values ​​need to use value.

php_flag[display_errors] = off

php_admin_value[error_log] = /var/log/fpm-php.www.log

php_admin_flag[log_errors] = on

php_admin_value[memory_limit] = 32M

#When setting `disable_functions` and `disable_classes` in this method, the settings of php.ini will not be overwritten, but will only be appended.

Note: PHP configuration values ​​​​are set through php_value or php_flag, and will overwrite the previous value.
Disable_functions or disable_classes The values ​​defined in php.ini will not be overwritten, but the new settings will be appended to the original values.
The value defined using php_admin_value or php_admin_flag cannot be overridden by ini_set() in PHP code.

Since 5.3.3, PHP settings can also be set through the web server.

nginx communicates with php-fpm through unixsock:

Applicable scenarios: nginx and php-fpm are on the same server. In this case, unixsocket inter-process communication can be used directly , without using TCP port communication, you can save time to create a connection, thereby improving performance.

1. Set the listen of php-fpm to /opt/remi/php56/root/var/run/php-fpm/php567-fpm.sock (relative path can be used), and then restart fpm and it will automatically Create the php567-fpm.sock file

2. Change the fastcgi_pass parameter of nginx to unix:/opt/remi/php56/root/var/run/php-fpm/php567-fpm.sock; through php567- To communicate with fpm, the fpm.sock file needs to ensure that nginx has permission to access the php567-fpm.sock file.

Summary:

The sock file can be created anywhere, as long as fpm has the permission to write the file in that directory and nginx has the permission to read it. The tcp connection will be more stable because the TCP protocol ensures the correctness of the data, but sock has fewer data copies and context switches, and consumes less resources. However, sock can only be used when nginx and fpm are on the same machine.

php-fpm process status monitoring

1. nginx configuration: when encountering a status request, forward it directly to php

location ~^/status$ {

fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;

include fastcgi_params;

fastcgi_pass 127.0.0.1:9000;

}

2. Fpm configuration: pm.status_path = /status

3. Then restart fpm and nginx and access it in the browser to see:

The results are displayed in text/plain by default. You can pass the parameters ?json/html/xml to get the results in json and other formats respectively; the parameter full can view the details of each sub-process

pool Process pool name

process manager Process management method

start time When did the process start

start since How many seconds has the process been running

accepted conn How many connections has the pool accepted in total

listen queue The number of connections waiting for accept

max listen queue After fpm is started, the highest number of connections waiting for accept in history

listen queue len The maximum length of the configured listening queue is limited by `listen.backlog` and the system `cat /proc/sys/net/core/somaxconn`, whichever is the smallest value

idle processes Idle Number of processes

active process The number of working processes (with restrictions, it is the total number of child processes)

total processes The total number of child processes

max active processes After fpm is started, the maximum number of processes working simultaneously in history

max children reached When the process management mode is 'dynamic' and 'ondemand', this value is the number of times the master creates more child processes when there are not enough child processes.

slow requests The number of slow requests

Under the full parameter

pid child process ID;

state child process status (Idle, Running, ... );

start time The time when the child process started;

start since How many seconds has it been running since the child process started;

requests How many requests has the current child process processed in total;

request duration The number of nanoseconds the request takes;

request method request method (GET, POST, ...);

request URI request parameter;

content length POST request, the requested content length;

user - the user (PHP_AUTH_USER) (or '-' if not set);

script which php file is requested;

last request cpu The cpu resources consumed by the last request

last request memory The peak memory consumed by the last request

If the process is idle, then this information is recorded The relevant data of this request, otherwise it is the relevant data of this current request.

Backlog configuration issue

An fpm sub-process can only handle one request at the same time. If the backlog is set too large, requests initiated by clients such as nginx will never have an fpm sub-process. When accepting, nginx will directly disconnect the connection. When fpm is busy and then accept, it will be found that it is disconnected, so an error will be reported. If the backlog is set too small, when the amount of access is large, all fpm sub-processes will be busy, and the backlog will be full, and new connections will be rejected. At this time, if nginx requests again, it will be directly rejected. Therefore, backlog parameters need to be set appropriately.

As long as the system defaults are used for most of the parameters, we only need to know a few more important parameter settings. When they are used, we can look back for relevant instructions

Related recommendations: Programming Video Course

The above is the detailed content of php-fpm.conf configuration file Chinese and important parameter descriptions. For more information, please follow other related articles on the PHP Chinese website!

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