php-fpm优化

释放双眼,带上耳机,听听看~!

php-fpm优化

1.通常情况我们修改/etc/php.ini文件,仅会修改错误日志与文件上传


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
1#;;;;;;;;;;;;;;;;;
2# Error logging ;
3#;;;;;;;;;;;;;;;;;
4expose_php = Off # 关闭php版本信息
5display_error = Off # 屏幕不显示错误日志
6error_reporting = E_WARNING & E_ERROR # 记录php错误日志至后台
7log_errors = On # 开启日志
8error_log = /var/log/php_error.log # 错误日志记录的位置
9date.timezone = PRC # 时区调整,默认PRC, 建议调整为Asia/Shanghai
10
11#;;;;;;;;;;;;;;;
12# File Uploads ;
13#;;;;;;;;;;;;;;;
14file_uploads = On # 开启文件上传功能,默认启动
15upload_max_filesize = 300M # 允许上传文件的最大大小
16post_max_size = 300M # 允许客户端单个POST请求发送的最大数据
17max_file_uploads = 20 # 允许同时上传的文件的最大数量
18memory_limit = 128M # 每个脚本执行最大内存
19
20#/etc/php.ini优化配置如下
21sql.safe_mode = Off
22post_max_size = 300M
23upload_max_filesize = 300M
24max_file_uploads = 20
25memory_limit = 128M
26date.timezone = Asia/Shanghai
27
28expose_php = Off
29display_error = Off
30error_reporting = E_WARNING & E_ERROR
31log_errors = On
32error_log = /var/log/php_error.log
33
34

2. php-fpm主配置文件/etc/php-fpm.conf调整


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
1#第一部分,fpm配置
2;include=etc/fpm.d/*.conf
3
4#第二部分,全局配置
5[global]
6;pid = /var/log/php-fpm/php-fpm.pid #pid文件存放的位置
7;error_log = /var/log/php-fpm/php-fpm.log #错误日志存放的位置
8;log_level = error #日志级别, alert, error, warning, notice, debug
9rlimit_files = 65535 #php-fpm进程能打开的文件数
10events.mechanism = epoll #使用epoll事件模型处理请求
11
12#第三部分,进程池定义
13[www] #池名称
14user = www #进程运行的用户
15group = www #进程运行的组
16;listen = /dev/shm/php-fpm.sock #监听在本地socket文件
17listen = 127.0.0.1:9000 #监听在本地tcp的9000端口
18;listen.allowed_clients = 127.0.0.1 #允许访问FastCGI进程的IP,any不限制
19
20; Choose how the process manager will control the number of child processes.
21; Possible Values:
22; static - a fixed number (pm.max_children) of child processes;
23; dynamic - the number of child processes are set dynamically based on the
24; following directives:
25; pm.max_children - the maximum number of children that can
26; be alive at the same time.
27; pm.start_servers - the number of children created on startup.
28; pm.min_spare_servers - the minimum number of children in 'idle'
29; state (waiting to process). If the number
30; of 'idle' processes is less than this
31; number then some children will be created.
32; pm.max_spare_servers - the maximum number of children in 'idle'
33; state (waiting to process). If the number
34; of 'idle' processes is greater than this
35; number then some children will be killed.
36; Note: This value is mandatory.
37
38pm = dynamic #
39pm.max_children = 512 #最大启动的php-fpm进程数
40pm.start_servers = 32 #初始启动的php-fpm进程数
41pm.min_spare_servers = 32 #最少的空闲php-fpm进程数
42pm.max_spare_servers = 64 #最大的空闲php-fpm进程数
43pm.max_requests = 1500 #每一个进程能响应的请求数
44pm.process_idle_timeout = 15s;
45
46# 错误日志
47php_flag[display_errors] = off
48php_admin_value[error_log] = /soft/log/php/php-www_error.log
49php_admin_flag[log_errors] = on
50
51# 将查询超过5s的连接记录至慢查询日志中
52request_slowlog_timeout = 5s
53slowlog = /var/log/php/slow.log
54
55

3.php-fpm状态模块,用于监控php-fpm状态使用


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
1[root@nginx ~]# vim /etc/php-fpm.d/www.conf
2# 开启php的状态页面
3pm.status_path = /phpfpm_status
4
5#
6[root@nginx conf.d]# cat /etc/nginx/conf.d/fpm.conf
7server {
8listen 80;
9server_name php.qls.com;
10location / {
11root /code;
12index index.php;
13}
14location /phpfpm_status {
15fastcgi_pass 127.0.0.1:9000;
16fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
17include fastcgi_params;
18}
19location ~ \.php$ {
20fastcgi_pass 127.0.0.1:9000;
21fastcgi_index index.php;
22fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
23include fastcgi_params;
24}
25}
26
27

4. 访问测试phpfpm_status状态页面


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1[root@nginx ~]# curl http://127.0.0.1/phpfpm_status
2pool: www
3process manager: dynamic
4start time: 05/Jul/2016:15:30:56 +0800
5start since: 409
6accepted conn: 22
7listen queue: 0
8max listen queue: 0
9listen queue len: 128
10idle processes: 4
11active processes: 1
12total processes: 5
13max active processes: 2
14max children reached: 0
15
16#PHP-FPM状态解释:
17pool #fpm池名称,大多数为www
18process manager #进程管理方式dynamic或者static
19start time #启动日志,如果reload了fpm,时间会更新
20start since #运行时间
21accepted conn #当前池接受的请求数
22listen queue #请求等待队列,如果这个值不为0,那么需要增加FPM的进程数量
23max listen queue #请求等待队列最高的数量
24listen queue len #socket等待队列长度
25idle processes #空闲进程数量
26active processes #活跃进程数量
27total processes #总进程数量
28max active processes #最大的活跃进程数量(FPM启动开始计算)
29max children reached #程最大数量限制的次数,如果这个数量不为0,那说明你的最大进程数量过小,可以适当调整。
30
31

5. PHP-FPM配置文件


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
1[root@nginx ~]# cat /etc/php-fpm.d/www.conf
2[global]
3pid = /var/run/php-fpm.pid
4
5error_log = /var/log/php/php-fpm.log
6log_level = warning
7rlimit_files = 655350
8events.mechanism = epoll
9
10[www]
11user = nginx
12group = nginx
13listen = 127.0.0.1:9000
14listen.owner = www
15listen.group = www
16listen.mode = 0660
17
18listen.allowed_clients = 127.0.0.1
19pm = dynamic
20pm.max_children = 512
21pm.start_servers = 10
22pm.min_spare_servers = 10
23pm.max_spare_servers = 30
24pm.process_idle_timeout = 15s;
25pm.max_requests = 2048
26pm.status_path = /phpfpm_status
27
28#php-www模块错误日志
29php_flag[display_errors] = off
30php_admin_value[error_log] = /var/log/php/php-www.log
31php_admin_flag[log_errors] = on
32
33#php慢查询日志
34request_slowlog_timeout = 5s
35slowlog = /var/log/php/php-slow.log
36
37

给TA打赏
共{{data.count}}人
人已打赏
安全技术

c++ list, vector, map, set 区别与用法比较

2022-1-11 12:36:11

安全运维

实战hadoop海量数据处理系列03 :数据仓库的设计

2021-12-12 17:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索