12.21php-fpm的pool12.22php-fpm慢執行日誌12.23open_basedir12.24php-fpm程式管理

技術小牛人發表於2017-11-12
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


相關文章