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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
|
- 12.21 php-fpm的pool
- 12.22 php-fpm慢执行日志
- 12.23 open_basedir
- 12.24 php-fpm进程管理
# 12.21 php-fpm的pool
- 先进入
```
[root@www ~]
# cd /usr/local/php-fpm/etc/
[root@www etc]
# ls
pear.conf php-fpm.conf php-fpm.conf.default php.ini
[root@www etc]
# cat php-fpm.conf
[global]
pid =
/usr/local/php-fpm/var/run/php-fpm
.pid
error_log =
/usr/local/php-fpm/var/log/php-fpm
.log
[www]
listen =
/tmp/php-fcgi
.sock
#listen = 127.0.0.1:9000
listen.mode = 444
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
[root@www etc]
#
```
- 重新配置配置文件,重新添加一个pool池子 ,名字叫aming.com
- nginx有多个pool
```
[root@www etc]
# vi !$
vi
php-fpm.conf
[www]
listen =
/tmp/php-fcgi
.sock
#listen = 127.0.0.1:9000
listen.mode = 444
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
[aming.com]
listen =
/tmp/aming
.sock
#listen = 127.0.0.1:9000
listen.mode = 444
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
:wq
[root@www etc]
# /usr/local/php-fpm/sbin/php-fpm -t
[24-Oct-2017 20:58:41] NOTICE: PHP message: PHP Deprecated: Comments starting with
'#'
are deprecated
in
Unknown on line 1
in
Unknown on line 0
[24-Oct-2017 20:58:41] NOTICE: PHP message: PHP Deprecated: Comments starting with
'#'
are deprecated
in
Unknown on line 1
in
Unknown on line 0
[24-Oct-2017 20:58:41] NOTICE: configuration
file
/usr/local/php-fpm/etc/php-fpm
.conf
test
is successful
[root@www etc]
# /etc/init.d/php-fpm reload
Reload service php-fpm
done
```
- 检查下,重新加载
- 最右侧多了一个aming.com 池子,
```
[root@www etc]
# ps aux |grep php-fpm
root 2410 0.0 0.4 227216 4960 ? Ss 21:02 0:00 php-fpm: master process (
/usr/local/php-fpm/etc/php-fpm
.conf)
php-fpm 2411 0.0 0.4 227156 4716 ? S 21:02 0:00 php-fpm: pool www
php-fpm 2412 0.0 0.4 227156 4716 ? S 21:02 0:00 php-fpm: pool www
php-fpm 2413 0.0 0.4 227156 4716 ? S 21:02 0:00 php-fpm: pool www
php-fpm 2414 0.0 0.4 227156 4716 ? S 21:02 0:00 php-fpm: pool www
php-fpm 2415 0.0 0.4 227156 4720 ? S 21:02 0:00 php-fpm: pool www
php-fpm 2416 0.0 0.4 227156 4720 ? S 21:02 0:00 php-fpm: pool www
php-fpm 2417 0.0 0.4 227156 4720 ? S 21:02 0:00 php-fpm: pool www
php-fpm 2418 0.0 0.4 227156 4720 ? S 21:02 0:00 php-fpm: pool www
php-fpm 2419 0.0 0.4 227156 4720 ? S 21:02 0:00 php-fpm: pool www
php-fpm 2420 0.0 0.4 227156 4724 ? S 21:02 0:00 php-fpm: pool www
php-fpm 2421 0.0 0.4 227156 4724 ? S 21:02 0:00 php-fpm: pool www
php-fpm 2422 0.0 0.4 227156 4724 ? S 21:02 0:00 php-fpm: pool www
php-fpm 2423 0.0 0.4 227156 4724 ? S 21:02 0:00 php-fpm: pool www
php-fpm 2424 0.0 0.4 227156 4724 ? S 21:02 0:00 php-fpm: pool www
php-fpm 2425 0.0 0.4 227156 4724 ? S 21:02 0:00 php-fpm: pool www
php-fpm 2426 0.0 0.4 227156 4724 ? S 21:02 0:00 php-fpm: pool www
php-fpm 2427 0.0 0.4 227156 4724 ? S 21:02 0:00 php-fpm: pool www
php-fpm 2428 0.0 0.4 227156 4724 ? S 21:02 0:00 php-fpm: pool www
php-fpm 2429 0.0 0.4 227156 4724 ? S 21:02 0:00 php-fpm: pool www
php-fpm 2430 0.0 0.4 227156 4724 ? S 21:02 0:00 php-fpm: pool www
php-fpm 2431 0.0 0.4 227156 4720 ? S 21:02 0:00 php-fpm: pool aming.com
php-fpm 2432 0.0 0.4 227156 4720 ? S 21:02 0:00 php-fpm: pool aming.com
php-fpm 2433 0.0 0.4 227156 4720 ? S 21:02 0:00 php-fpm: pool aming.com
php-fpm 2434 0.0 0.4 227156 4720 ? S 21:02 0:00 php-fpm: pool aming.com
php-fpm 2435 0.0 0.4 227156 4724 ? S 21:02 0:00 php-fpm: pool aming.com
php-fpm 2436 0.0 0.4 227156 4728 ? S 21:02 0:00 php-fpm: pool aming.com
php-fpm 2437 0.0 0.4 227156 4728 ? S 21:02 0:00 php-fpm: pool aming.com
php-fpm 2438 0.0 0.4 227156 4728 ? S 21:02 0:00 php-fpm: pool aming.com
php-fpm 2439 0.0 0.4 227156 4728 ? S 21:02 0:00 php-fpm: pool aming.com
php-fpm 2440 0.0 0.4 227156 4728 ? S 21:02 0:00 php-fpm: pool aming.com
php-fpm 2441 0.0 0.4 227156 4728 ? S 21:02 0:00 php-fpm: pool aming.com
php-fpm 2442 0.0 0.4 227156 4728 ? S 21:02 0:00 php-fpm: pool aming.com
php-fpm 2443 0.0 0.4 227156 4728 ? S 21:02 0:00 php-fpm: pool aming.com
php-fpm 2444 0.0 0.4 227156 4728 ? S 21:02 0:00 php-fpm: pool aming.com
php-fpm 2445 0.0 0.4 227156 4728 ? S 21:02 0:00 php-fpm: pool aming.com
php-fpm 2446 0.0 0.4 227156 4728 ? S 21:02 0:00 php-fpm: pool aming.com
php-fpm 2447 0.0 0.4 227156 4728 ? S 21:02 0:00 php-fpm: pool aming.com
php-fpm 2448 0.0 0.4 227156 4728 ? S 21:02 0:00 php-fpm: pool aming.com
php-fpm 2449 0.0 0.4 227156 4728 ? S 21:02 0:00 php-fpm: pool aming.com
php-fpm 2450 0.0 0.4 227156 4728 ? S 21:02 0:00 php-fpm: pool aming.com
root 2476 0.0 0.0 112680 980 pts
/0
R+ 21:04 0:00
grep
--color=auto php-fpm
[root@www etc]
#
```
- 现在怎么去用它们呢
- 为什么要定义多个池子,加入
test
.com 请求很大,最终把进程耗尽了php-fpm ,最多启动50个,结果启动完50个 所有的都在忙,又有新的请求的时候,结果就报502错误,因为他没有多余的php 进程给它提供服务,而这个站点不受影响,因为他用的是另一个池子,这就是为什么要定义多个池子的原因。
```
[root@www vhost]
# vi aaa.com.conf
[1]+ 已停止
vi
aaa.com.conf
[root@www vhost]
# cat test.com.conf
[root@www vhost]
# fg
vi
aaa.com.conf
server
{
{
listen 80 default_server;
server_name aaa.com;
index index.html index.htm index.php;
root
/data/wwwroot/default
;
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:
/tmp/aming
.sock;
fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME
/data/wwwroot/default
$fastcgi_script_name;
}
}
~
~
~
:wq
```
- 在主配置文件里面 加入 include = etc
/php-fpm
.d/*.conf 加入到[global]里面,然后再把[www]和[global]分别拆出来
```
[root@www vhost]
# ls
aaa.com.conf ld.conf proxy.conf ssl.conf
test
.com.conf
[root@www vhost]
# cd /usr/local/php-fpm/etc/
[root@www etc]
# ls
pear.conf php-fpm.conf php-fpm.conf.default php.ini
[root@www etc]
# vi php-fpm.conf
[global]
pid =
/usr/local/php-fpm/var/run/php-fpm
.pid
error_log =
/usr/local/php-fpm/var/log/php-fpm
.log
include = etc
/php-fpm
.d/*.conf
[www]
listen =
/tmp/php-fcgi
.sock
#listen = 127.0.0.1:9000
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
[aming.com]
listen =
/tmp/aming
.sock
#listen = 127.0.0.1:9000
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
:wq
[root@www etc]
# cat php-fpm.conf
[global]
pid =
/usr/local/php-fpm/var/run/php-fpm
.pid
error_log =
/usr/local/php-fpm/var/log/php-fpm
.log
include = etc
/php-fpm
.d/*.conf
[www]
listen =
/tmp/php-fcgi
.sock
#listen = 127.0.0.1:9000
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
[aming.com]
listen =
/tmp/aming
.sock
#listen = 127.0.0.1:9000
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
[root@www etc]
#
```
- 然后再把这些删掉,100dd 就是删除下面100行,实际只有27行
```
[root@www etc]
# vi php-fpm.conf
[global]
pid =
/usr/local/php-fpm/var/run/php-fpm
.pid
error_log =
/usr/local/php-fpm/var/log/php-fpm
.log
include = etc
/php-fpm
.d/*.conf
~
~
:wq
[root@www etc]
# vi php-fpm.conf
```
- 因为是include = etc
/php-fpm
.d/*.conf 所以要创建一个php-fpm.d的目录
- 然后再创建一个php-fpm.d的目录 再去目录里面创建 www.conf aming.conf
```
[root@www etc]
# mkdir php-fpm.d
[root@www etc]
# cd php-fpm.d/
[root@www php-fpm.d]
#
[root@www php-fpm.d]
# vi www.conf
[www]
listen =
/tmp/php-fcgi
.sock
#listen = 127.0.0.1:9000
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
~
~
~
:wq
[root@www php-fpm.d]
# vi aming.conf
[aming.com]
listen =
/tmp/aming
.sock
#listen = 127.0.0.1:9000
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
[root@www etc]
# vi php-fpm.conf
[root@www etc]
# mkdir php-fpm.d
[root@www etc]
# cd php-fpm.d/
[root@www php-fpm.d]
# vi www.conf
[root@www php-fpm.d]
# vi aming.conf
~
:wq
[root@www etc]
# vi php-fpm.conf
[root@www etc]
# mkdir php-fpm.d
[root@www etc]
# cd php-fpm.d/
[root@www php-fpm.d]
# vi www.conf
[root@www php-fpm.d]
# vi aming.conf
[root@www php-fpm.d]
# ls
aming.conf www.conf
[root@www php-fpm.d]
#
```
- 这样看着就很干净了,这个类似于nginx的vhost
```
[root@www php-fpm.d]
# cat ../php-fpm.conf
[global]
pid =
/usr/local/php-fpm/var/run/php-fpm
.pid
error_log =
/usr/local/php-fpm/var/log/php-fpm
.log
include = etc
/php-fpm
.d/*.conf
[root@www php-fpm.d]
#
```
- 再检查配置文件重启下
```
[root@localhost php-fpm.d]
# /usr/local/php-fpm/sbin/php-fpm -t
[23-Oct-2017 23:54:28] NOTICE: configuration
file
/usr/local/php-fpm/etc/php-fpm
.conf
test
is successful
[root@localhost php-fpm.d]
# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm .
done
Starting php-fpm
done
[root@localhost php-fpm.d]
#
[root@localhost php-fpm.d]
# !ps
ps
aux |
grep
php-fpm
root 5927 0.0 0.4 227240 4980 ? Ss 23:54 0:00 php-fpm: master process (
/usr/local/php-fpm/etc/php-fpm
.conf)
php-fpm 5928 0.0 0.4 227180 4720 ? S 23:54 0:00 php-fpm: pool aming
php-fpm 5929 0.0 0.4 227180 4724 ? S 23:54 0:00 php-fpm: pool aming
php-fpm 5930 0.0 0.4 227180 4724 ? S 23:54 0:00 php-fpm: pool aming
php-fpm 5931 0.0 0.4 227180 4724 ? S 23:54 0:00 php-fpm: pool aming
php-fpm 5932 0.0 0.4 227180 4728 ? S 23:54 0:00 php-fpm: pool aming
php-fpm 5933 0.0 0.4 227180 4728 ? S 23:54 0:00 php-fpm: pool aming
php-fpm 5934 0.0 0.4 227180 4728 ? S 23:54 0:00 php-fpm: pool aming
php-fpm 5935 0.0 0.4 227180 4728 ? S 23:54 0:00 php-fpm: pool aming
php-fpm 5936 0.0 0.4 227180 4728 ? S 23:54 0:00 php-fpm: pool aming
php-fpm 5937 0.0 0.4 227180 4728 ? S 23:54 0:00 php-fpm: pool aming
php-fpm 5938 0.0 0.4 227180 4728 ? S 23:54 0:00 php-fpm: pool aming
php-fpm 5939 0.0 0.4 227180 4728 ? S 23:54 0:00 php-fpm: pool aming
php-fpm 5940 0.0 0.4 227180 4728 ? S 23:54 0:00 php-fpm: pool aming
php-fpm 5941 0.0 0.4 227180 4728 ? S 23:54 0:00 php-fpm: pool aming
php-fpm 5942 0.0 0.4 227180 4728 ? S 23:54 0:00 php-fpm: pool aming
php-fpm 5943 0.0 0.4 227180 4728 ? S 23:54 0:00 php-fpm: pool aming
php-fpm 5944 0.0 0.4 227180 4728 ? S 23:54 0:00 php-fpm: pool aming
php-fpm 5945 0.0 0.4 227180 4732 ? S 23:54 0:00 php-fpm: pool aming
php-fpm 5946 0.0 0.4 227180 4732 ? S 23:54 0:00 php-fpm: pool aming
php-fpm 5947 0.0 0.4 227180 4732 ? S 23:54 0:00 php-fpm: pool aming
php-fpm 5948 0.0 0.4 227180 4728 ? S 23:54 0:00 php-fpm: pool www
php-fpm 5949 0.0 0.4 227180 4728 ? S 23:54 0:00 php-fpm: pool www
php-fpm 5950 0.0 0.4 227180 4728 ? S 23:54 0:00 php-fpm: pool www
php-fpm 5951 0.0 0.4 227180 4728 ? S 23:54 0:00 php-fpm: pool www
php-fpm 5952 0.0 0.4 227180 4732 ? S 23:54 0:00 php-fpm: pool www
php-fpm 5953 0.0 0.4 227180 4732 ? S 23:54 0:00 php-fpm: pool www
php-fpm 5954 0.0 0.4 227180 4732 ? S 23:54 0:00 php-fpm: pool www
php-fpm 5955 0.0 0.4 227180 4732 ? S 23:54 0:00 php-fpm: pool www
php-fpm 5956 0.0 0.4 227180 4732 ? S 23:54 0:00 php-fpm: pool www
php-fpm 5957 0.0 0.4 227180 4732 ? S 23:54 0:00 php-fpm: pool www
php-fpm 5958 0.0 0.4 227180 4732 ? S 23:54 0:00 php-fpm: pool www
php-fpm 5959 0.0 0.4 227180 4732 ? S 23:54 0:00 php-fpm: pool www
php-fpm 5960 0.0 0.4 227180 4732 ? S 23:54 0:00 php-fpm: pool www
php-fpm 5961 0.0 0.4 227180 4736 ? S 23:54 0:00 php-fpm: pool www
php-fpm 5962 0.0 0.4 227180 4736 ? S 23:54 0:00 php-fpm: pool www
php-fpm 5963 0.0 0.4 227180 4736 ? S 23:54 0:00 php-fpm: pool www
php-fpm 5964 0.0 0.4 227180 4736 ? S 23:54 0:00 php-fpm: pool www
php-fpm 5965 0.0 0.4 227180 4736 ? S 23:54 0:00 php-fpm: pool www
php-fpm 5966 0.0 0.4 227180 4736 ? S 23:54 0:00 php-fpm: pool www
php-fpm 5967 0.0 0.4 227180 4736 ? S 23:54 0:00 php-fpm: pool www
root 5969 0.0 0.0 112680 980 pts
/0
S+ 23:55 0:00
grep
--color=auto php-fpm
[root@localhost php-fpm.d]
#
```
- 多个池子就这样配置
# 12.22 php-fpm慢执行日志
- 咱们做PHP得网站,建议你使用LNMP,其中有一个原因在于我们可以去分析php的慢执行日志,为什么要分析它的 慢执行日志呢,运维工作中,经常会遇到一个问题
- 老板会经常反馈,你们公司的网站访问慢了,或者客户说网站慢了,要知道根本原因,要知道慢在什么地方,java网站先不说,php的网站有是办法查到底慢在哪里,就像系统负载高可以用工具去分析,vmstat
top
等等,可以找到哪个进程导致的,php网站慢,到底什么地方慢,所以这个慢日志,非常重要,
- 针对www做一个实验
```
[root@localhost php-fpm.d]
# vi www.conf
[www]
listen =
/tmp/php-fcgi
.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
request_slowlog_timeout = 1
//
超过1秒钟 我就要记录日志
slowlog =
/usr/local/php-fpm/var/log/www-slow
.log
//
日志放在这个目录下
~
~
:wq
[root@localhost php-fpm.d]
# vi www.conf
[root@localhost php-fpm.d]
# /usr/local/php-fpm/sbin/php-fpm -t
[24-Oct-2017 00:06:45] NOTICE: configuration
file
/usr/local/php-fpm/etc/php-fpm
.conf
test
is successful
[root@localhost php-fpm.d]
# /etc/init.d/php-fpm reload
Reload service php-fpm
done
[root@localhost php-fpm.d]
#
```
- 先看下日志有没有生成 有的 www-slow.log
```
[www]
listen =
/tmp/php-fcgi
.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
request_slowlog_timeout = 1
slowlog =
/usr/local/php-fpm/var/log/www-slow
.log
[root@localhost php-fpm.d]
# ls /usr/local/php-fpm/var/log/
php-fpm.log www-slow.log
[root@localhost php-fpm.d]
#
[root@localhost php-fpm.d]
# ls /usr/local/php-fpm/var/log/www-slow.log
/usr/local/php-fpm/var/log/www-slow
.log
[root@localhost php-fpm.d]
# cat !$
cat
/usr/local/php-fpm/var/log/www-slow
.log
[root@localhost php-fpm.d]
#
```
- 看看它有没有内容呢,空的,因为你还没有任何的超过1秒的 php执行的过程
- 接下来需要模拟一个慢执行的php
- 咱们来写一个脚本
- php-fcgi 它是被这个(
/usr/local/nginx/conf/vhost/test
.com.conf
)
test
.com.conf站点用着 fastcgi_pass unix:
/tmp/php-fcgi
.sock;
所以你要去这个站点做一些操作了,在这个目录下
/data/wwwroot/test
.com
```
[root@localhost php-fpm.d]
# vim /data/wwwroot/test.com/sleep.php
<?php
echo
“
test
slow log”;
sleep
(2);
echo
“
done
”;
?>
:wq
```
- 来测试下
```
[root@localhost php-fpm.d]
# curl -x127.0.0.1:80 test.com/sleep.php -I
HTTP
/1
.1 200 OK
Server: nginx
/1
.12.1
Date: Mon, 23 Oct 2017 16:19:16 GMT
Content-Type: text
/html
; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP
/5
.6.30
[root@localhost php-fpm.d]
#
```
- 出错误了,排查错误看日志,也有个简单的方法,直接打开它的配置文件,搜索display_error 把display_errors = On
- 可以直接查看到具体的错误是什么
```
[root@localhost php-fpm.d]
# vi /usr/local/php-fpm/etc/php.ini
; development version only
in
development environments, as errors shown to
; application
users
can inadvertently leak otherwise secure information.
; This is php.ini-development INI
file
.
;;;;;;;;;;;;;;;;;;;
; Quick Reference ;
;;;;;;;;;;;;;;;;;;;
; http:
//php
.net
/display-errors
display_errors = On
[root@localhost php-fpm.d]
# vi /usr/local/php-fpm/etc/php.ini
[root@localhost php-fpm.d]
# /etc/init.d/php-fpm reload
Reload service php-fpm
done
[root@localhost php-fpm.d]
# curl -x127.0.0.1:80 test.com/sleep.php
<br />
<b>Parse error<
/b
>: syntax error, unexpected
'slow'
(T_STRING), expecting
','
or
';'
in
<b>
/data/wwwroot/test
.com
/sleep
.php<
/b
> on line <b>2<
/b
><br />
[root@localhost php-fpm.d]
#
```
- 提示语法错误,分号不对,双引号不对,进去改下
```
[root@localhost php-fpm.d]
# vi /data/wwwroot/test.com/sleep.php
<?php
echo
"test slow log"
;
sleep
(2);
echo
"done"
;
?>
:wq
```
- 再来测试下,它会告诉你是这个脚本慢,脚本的第三行慢
/data/wwwroot/test
.com
/sleep
.php:3
```
[root@localhost php-fpm.d]
# vi /data/wwwroot/test.com/sleep.php
[root@localhost php-fpm.d]
# curl -x127.0.0.1:80 test.com/sleep.php
test
slow logdone
[root@localhost php-fpm.d]
# cat /usr/local/php-fpm/var/log/www-slow.log
[24-Oct-2017 00:29:29] [pool www] pid 6123
script_filename =
/data/wwwroot/test
.com
/sleep
.php
[0x00007f06da03d298]
sleep
()
/data/wwwroot/test
.com
/sleep
.php:3
[root@localhost php-fpm.d]
#
```
- 看下第三行
sleep
(2); 是这一行,因为它执行了2秒钟,只要超过了1秒就会被记录慢日志,这就是慢日志的功效了
```
[root@localhost php-fpm.d]
# cat /data/wwwroot/test.com/sleep.php
<?php
echo
"test slow log"
;
sleep
(2);
echo
"done"
;
?>
[root@localhost php-fpm.d]
#
```
- 咱们估以的让它执行慢的,然后生成慢日志
# 12.23 open_basedir
- open_bassdir 之前也介绍了,在php-fpm 也可以定义的,如果你服务器上有多个网站,去php.ini就不合适了,所以要么去apache虚拟主机配置文件里定义,要么去php-fpm配置文件里去定义,可以针对每个池子做不同的定义
- 比如我在www里面去定义
```
[root@localhost php-fpm.d]
# vi www.conf
[www]
[www]
listen =
/tmp/php-fcgi
.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
request_slowlog_timeout = 1
slowlog =
/usr/local/php-fpm/var/log/www-slow
.log
php_admin_value[open_basedir]=
/data/wwwroot/aming
.com:
/tmp/
~
:wq
[root@localhost php-fpm.d]
# cat www.conf
[www]
listen =
/tmp/php-fcgi
.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
request_slowlog_timeout = 1
slowlog =
/usr/local/php-fpm/var/log/www-slow
.log
php_admin_value[open_basedir]=
/data/wwwroot/aming
.com:
/tmp/
[root@localhost php-fpm.d]
#
```
- 正常的话
/tmp/php-fcgi
.sock 是
test
.com,定义成aming.com 的话,去请求去访问php,肯定会出问题
- 重启下
```
[root@localhost php-fpm.d]
# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm .
done
Starting php-fpm
done
[root@localhost php-fpm.d]
#
[root@localhost php-fpm.d]
# !curl
curl -x127.0.0.1:80
test
.com
/sleep
.php
No input
file
specified.
[root@localhost php-fpm.d]
# !netstat
netstat
-lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID
/Program
name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5682
/nginx
: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 874
/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1783
/master
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 5682
/nginx
: master
tcp6 0 0 :::3306 :::* LISTEN 1578
/mysqld
tcp6 0 0 :::22 :::* LISTEN 874
/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1783
/master
[root@localhost php-fpm.d]
# curl -x127.0.0.1:80 test.com/sleep.php -I
HTTP
/1
.1 404 Not Found
Server: nginx
/1
.12.1
Date: Mon, 23 Oct 2017 17:07:37 GMT
Content-Type: text
/html
; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP
/5
.6.30
[root@localhost php-fpm.d]
#
```
- 404肯定就不对了,所以说是有问题的
```
[root@localhost php-fpm.d]
# cat www.conf
[www]
listen =
/tmp/php-fcgi
.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
request_slowlog_timeout = 1
slowlog =
/usr/local/php-fpm/var/log/www-slow
.log
php_admin_value[open_basedir]=
/data/wwwroot/aming
.com:
/tmp/
[root@localhost php-fpm.d]
#
```
- 现在我们再去定义一个php
```
[root@localhost php-fpm.d]
# vi /usr/local/nginx/conf/vhost/test.com.conf
[root@localhost php-fpm.d]
# vi /data/wwwroot/test.com/
1.gif 2.js 3.php admin/ index.html
sleep
.php upload/
[root@localhost php-fpm.d]
# vi /data/wwwroot/test.com/3.php
[root@localhost php-fpm.d]
# curl -x127.0.0.1:80 test.com/3.php -I
HTTP
/1
.1 404 Not Found
Server: nginx
/1
.12.1
Date: Mon, 23 Oct 2017 17:12:18 GMT
Content-Type: text
/html
; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP
/5
.6.30
[root@localhost php-fpm.d]
# !vi www.conf
vi
/data/wwwroot/test
.com
/3
.php www.conf
2 files to edit
[root@localhost php-fpm.d]
# ls
aming.conf www.conf
[root@localhost php-fpm.d]
# vi www.conf
```
- 修改下www.conf,你针对的地方是
test
.com 如果这个地方不去定义正确的路径, 他就没办法去访问
```
[root@localhost php-fpm.d]
# vi www.conf
[www]
listen =
/tmp/php-fcgi
.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
request_slowlog_timeout = 1
slowlog =
/usr/local/php-fpm/var/log/www-slow
.log
php_admin_value[open_basedir]=
/data/wwwroot/test
.com:
/tmp/
~
~
~
:wq
[root@localhost php-fpm.d]
# vi www.conf
[root@localhost php-fpm.d]
# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm .
done
Starting php-fpm
done
[root@localhost php-fpm.d]
# !curl
curl -x127.0.0.1:80
test
.com
/3
.php -I
HTTP
/1
.1 200 OK
Server: nginx
/1
.12.1
Date: Mon, 23 Oct 2017 17:17:29 GMT
Content-Type: text
/html
; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP
/5
.6.30
[root@localhost php-fpm.d]
#
[root@localhost php-fpm.d]
# curl -x127.0.0.1:80 test.com/3.php
e PHP License as published by the PHP Group and included
in
the distribution
in
the
file
: LICENSE
<
/p
>
<p>This program is distributed
in
the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
<
/p
>
<p>If you did not receive a copy of the PHP license, or have any questions about PHP licensing, please contact license@php.net.
<
/p
>
<
/td
><
/tr
>
<
/table
>
<
/div
><
/body
><
/html
>[root@localhost php-fpm.d]
#
```
- 查看下错误日志
-
```
[root@localhost php-fpm.d]
# vi /usr/local/php-fpm/etc/php.ini
; http:
//php
.net
/display-errors
display_errors = Off
; Log errors to syslog (Event Log on Windows).
;error_log = syslog
error_log =
/usr/local/php-fpm/var/log/php_errors
.log
;error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
error_reporting =E_ALL
[root@localhost php-fpm.d]
# vi /usr/local/php-fpm/etc/php.ini
[root@localhost php-fpm.d]
#
```
- 把地址改错,会生成错误日志
```
[root@localhost php-fpm.d]
# vi www.conf
[www]
listen =
/tmp/php-fcgi
.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
request_slowlog_timeout = 1
slowlog =
/usr/local/php-fpm/var/log/www-slow
.log
php_admin_value[open_basedir]=
/data/wwwroot/wwtest
.com:
/tmp/
~
~
:wq
[root@localhost php-fpm.d]
# vi /usr/local/php-fpm/etc/php.ini
[root@localhost php-fpm.d]
# ls
aming.conf www.conf
[root@localhost php-fpm.d]
# vi www.conf
[root@localhost php-fpm.d]
# grep error_log /usr/local/php-fpm/etc/php.ini
; server-specific log, STDERR, or a location specified by the error_log
; Set maximum length of log_errors. In error_log information about the
source
is
;error_log = php_errors.log
;error_log = syslog
error_log =
/usr/local/php-fpm/var/log/php_errors
.log
; OPcache error_log
file
name. Empty string assumes
"stderr"
.
;opcache.error_log=
[root@localhost php-fpm.d]
# ls /usr/local/php-fpm/var/log/
php-fpm.log www-slow.log
[root@localhost php-fpm.d]
# touch /usr/local/php-fpm/var/log/php_errors.log
[root@localhost php-fpm.d]
# chmod 777 /usr/local/php-fpm/var/log/php_errors.log
[root@localhost php-fpm.d]
# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm .
done
Starting php-fpm
done
```
- 来访问下
```
[root@localhost php-fpm.d]
# !curl
curl -x127.0.0.1:80
test
.com
/3
.php
No input
file
specified.
[root@localhost php-fpm.d]
#
[root@localhost php-fpm.d]
# curl -x127.0.0.1:80 test.com/3.php -I
HTTP
/1
.1 404 Not Found
Server: nginx
/1
.12.1
Date: Mon, 23 Oct 2017 17:43:15 GMT
Content-Type: text
/html
; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP
/5
.6.30
[root@localhost php-fpm.d]
#
```
- 看下日志里有没有信息, 很明显地址 open_basedir restriction
in
effect 有限制
- . File(
/data/wwwroot/test
.com
/3
.php) is not within the allowed path(s): (
/data/wwwroot/wwtest
.com:
/tmp/
)
- 很明显这俩个地址不同,所以就会报错,就会有问题
```
[root@localhost php-fpm.d]
# cat /usr/local/php-fpm/var/log/php_errors.log
[23-Oct-2017 17:42:53 UTC] PHP Warning: Unknown: open_basedir restriction
in
effect. File(
/data/wwwroot/test
.com
/3
.php) is not within the allowed path(s): (
/data/wwwroot/wwtest
.com:
/tmp/
)
in
Unknown on line 0
[23-Oct-2017 17:42:53 UTC] PHP Warning: Unknown: failed to
open
stream: Operation not permitted
in
Unknown on line 0
[23-Oct-2017 17:43:15 UTC] PHP Warning: Unknown: open_basedir restriction
in
effect. File(
/data/wwwroot/test
.com
/3
.php) is not within the allowed path(s): (
/data/wwwroot/wwtest
.com:
/tmp/
)
in
Unknown on line 0
[23-Oct-2017 17:43:15 UTC] PHP Warning: Unknown: failed to
open
stream: Operation not permitted
in
Unknown on line 0
[root@localhost php-fpm.d]
#
```
- 我们把它改正
```
[root@localhost php-fpm.d]
# vi www.conf
[www]
listen =
/tmp/php-fcgi
.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
request_slowlog_timeout = 1
slowlog =
/usr/local/php-fpm/var/log/www-slow
.log
php_admin_value[open_basedir]=
/data/wwwroot/test
.com:
/tmp/
~
:wq
[root@localhost php-fpm.d]
# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm .
done
Starting php-fpm
done
[root@localhost php-fpm.d]
# curl -x127.0.0.1:80 test.com/3.php -I
HTTP
/1
.1 200 OK
Server: nginx
/1
.12.1
Date: Mon, 23 Oct 2017 17:48:47 GMT
Content-Type: text
/html
; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP
/5
.6.30
[root@localhost php-fpm.d]
#
```
- 现在可以了,这次就正常了,这就是openbasedir怎么去定义
# 12.24 php-fpm进程管理
```
[root@localhost php-fpm.d]
# cat www.conf
[www]
listen =
/tmp/php-fcgi
.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
request_slowlog_timeout = 1
slowlog =
/usr/local/php-fpm/var/log/www-slow
.log
php_admin_value[open_basedir]=
/data/wwwroot/test
.com:
/tmp/
[root@localhost php-fpm.d]
#
```
- pm = dynamic 表示以什么样的形式进程启动,用dynamic 动态,后面参数才会生效,
- 如果是静态,只有一行生效 pm.max_children = 50 其他后面的参数不会生效
- pm = dynamic
//
动态进程管理,
- 也可以是static(静态) 也就是我一下子记生成这么多
- pm.max_children = 50
//
最大子进程数,
ps
aux可以查看
- pm.start_servers = 20
//
启动服务时会启动的进程数
- pm.min_spare_servers = 5
//
定义在空闲时段,子进程数的最少数量,如果达到这个数值时,php-fpm服务会自动派生新的子进程。
- pm.max_spare_servers = 35
//
定义在空闲时段,子进程数的最大值,如果高于这个数值就开始清理空闲的子进程。
- pm.max_requests = 500
//
定义一个子进程最多处理的请求数,也就是说在一个php-fpm的子进程最多可以处理这么多请求,当达到这个数值时,它会自动退出。
本文转自 ch71smas 51CTO博客,原文链接:http://blog.51cto.com/ch71smas/1977607
|