PHP服務緩存優化原理

Nginx 根據擴展名或者過濾規則將PHP程序請求傳遞給解析PHP的FCGI,也就是php-fpm進程

緩存操作碼(opcode)
Opcode,PHP編譯后的中間文件,緩存給用戶訪問
當客戶端請求一個PHP程序時,服務器的PHP引擎會解析該PHP程序,並將其編譯為特定的操作碼文件,該文件是執行PHP代碼后的一種二進制文件表現形式。默認情況下,這個編譯好的操作碼文件由PHP引擎執行后丟棄;而操作碼緩存的原理就是將編譯后的操作碼保存下來,並放入到共享內存里,以便再下一次調用該PHP頁面時重用它,避免了相同代碼的重復編譯。節省了PHP引擎重復編譯的時間,降低了服務器負載,同時減少了CPU和內存的開銷.
常用的PHP緩存加速軟件
1)xcache
經測試,xcache效率更好、社區活躍、兼容PHP版本多
2)ZendOpcache
Apache公司自主研發軟件,5.5版本以后自帶,加enbale-opcache編譯參數直接使用,但是軟件穩定性有待檢測
3)eAccelerator
5.3版本以前經常使用的加速軟件,隨着5.5版本升級,和xcache等優秀軟件的出現,社區活躍度開始下降
緩存軟件首選xcahe、持續關注ZendOpcache...
xcache部署
1)下載xcache,添加為PHP擴展模塊,編譯安裝
[root@web01 tools]# wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.bz2
[root@web01 tools]# tar xf xcache-3.2.0.tar.bz2 [root@web01 tools]# cd xcache-3.2.0 [root@web01 xcache-3.2.0]# /application/php/bin/phpize [root@web01 xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/application/php/bin/php-config [root@web01 xcache-3.2.0]# make && make install ... Installing shared extensions: /application/php5.5.32/lib/php/extensions/no-debug-non-zts-20121212/ Installing header files: /application/php5.5.32/include/php/ 
         2)配置php擴展生效
[root@db02 application]# vim /application/php/lib/php.ini extension_dir = "/application/php5.5.32/lib/php/extensions/no-debug-non-zts-20121212/" extension = memcache.so extension = imagick.so extension = xcache.so 
         3)重啟php后模塊生效
[root@db02 application]# /application/php/bin/php -m|grep "XCache" XCache XCache Cacher 
         4)xcache配置文件
[root@db02 ~]# cat ~/tools/xcache-3.2.0/xcache.ini|egrep -v "^;|^ " >> /application/php/lib/php.ini [xcache-common] extension = xcache.so #模塊 [xcache.admin] xcache.admin.enable_auth = On #開啟密碼認證 xcache.admin.user = "mOo" xcache.admin.pass = "md5 encrypted password" [xcache] xcache.shm_scheme = "mmap" #設置Xcache如何從系統分配共享內存 xcache.size = 60M #緩存大小,0禁止緩存 xcache.count = 1 #指定將xcache切分為多少塊,建議與CPU核數相同(grep -c processor /proc/cpuinfo) xcache.slots = 8K xcache.ttl = 0 #設置cache對象生存期TTL,0永不過期;如果上線次數多,調小 xcache.gc_interval = 0 #回收器掃描過期的對象回收內存空間的間隔,0不掃描 xcache.var_size = 4M #變量緩存,而不是opcache緩存 xcache.var_count = 1 xcache.var_slots = 8K xcache.var_ttl = 0 xcache.var_maxttl = 0 xcache.var_gc_interval = 300 xcache.var_namespace_mode = 0 xcache.var_namespace = "" xcache.coredump_type = 0 
         5)查看PHP chache加載情況
[root@db02 ~]# /application/php/sbin/php-fpm -v
PHP 5.5.32 (fpm-fcgi) (built: Jun 29 2016 11:32:56) Copyright (c) 1997-2015 The PHP Group Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies with XCache v3.2.0, Copyright (c) 2005-2014, by mOo with XCache Cacher v3.2.0, Copyright (c) 2005-2014, by mOo 
         6)web界面配置
[root@db02 ~]# echo -n "123456"|md5sum
e10adc3949ba59abbe56e057f20f883e  -
[root@db02 ~]# cp ~/tools/xcache-3.2.0/htdocs /application/nginx/html/www/xadmin -a [root@db02 ~]# vim /application/php/lib/php.ini [Date] date.timezone = Asia/Chongqing [xcache.admin] xcache.admin.enable_auth = On xcache.admin.user = "admin" xcache.admin.pass = "e10adc3949ba59abbe56e057f20f883e" [root@db02 ~]# pkill php-fpm [root@db02 ~]# /application/php/sbin/php-fpm 
         
ab壓力測試效果
1)未加xcache之前
[root@db02 application]# ab -n 3000 -c 100 http://10.0.0.111/test_info.php # 3000次會話請求、100並發數 Server Software: nginx/1.6.3 Server Hostname: 10.0.0.111 Server Port: 80 Document Path: /test_info.php #測試頁面 Document Length: 83921 bytes #頁面大小 Concurrency Level: 100 #100並發數 Time taken for tests: 7.973 seconds #整個測試持續時間 Complete requests: 3000 #完成的請求總數 Failed requests: 302 #失敗的請求次數 (Connect: 0, Receive: 0, Length: 302, Exceptions: 0) Write errors: 0 Total transferred: 252203675 bytes #整個過程的網絡傳輸量 HTML transferred: 251762675 bytes #HTML內容傳輸量 Requests per second: 376.25 [#/sec] (mean) #吞吐量,每秒能夠處理的並發數 Time per request: 265.779 [ms] (mean) #平均事務響應時間 Time per request: 2.658 [ms] (mean, across all concurrent requests) #每個連接請求實際運行時間 Transfer rate: 30889.42 [Kbytes/sec] received #平均每秒網絡上的流量,可以幫助排除是否存在網絡流量大導致響應時間延長的問題 Connection Times (ms) min mean[+/-sd] median max Connect: 0 1 3.2 0 21 Processing: 14 261 32.2 261 331 Waiting: 2 260 32.4 260 331 Total: 29 261 29.9 261 331 Percentage of the requests served within a certain time (ms) 50% 261 66% 268 75% 273 80% 276 90% 287 #90%的請求任務在287ms內完成 95% 303 98% 315 99% 322 100% 331 (longest request) 
         2)配置xache之后
[root@db02 application]# ab -n 3000 -c 100 http://10.0.0.111/test_info.php Server Software: nginx/1.6.3 Server Hostname: 10.0.0.111 Server Port: 80 Document Path: /test_info.php Document Length: 172 bytes Concurrency Level: 100 Time taken for tests: 0.516 seconds Complete requests: 3000 Failed requests: 0 Write errors: 0 Non-2xx responses: 3000 Total transferred: 969000 bytes HTML transferred: 516000 bytes Requests per second: 5819.42 [#/sec] (mean) #並發數上升為5000+ Time per request: 17.184 [ms] (mean) Time per request: 0.172 [ms] (mean, across all concurrent requests) Transfer rate: 1835.62 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 1.3 0 9 Processing: 6 17 2.1 16 21 Waiting: 0 17 2.2 16 21 Total: 7 17 1.6 16 21 Percentage of the requests served within a certain time (ms) 50% 16 66% 17 75% 18 80% 19 90% 19 95% 19 98% 20 99% 21 100% 21 (longest request) 
         由於是虛機測試環境,不一定十分准確,未安裝xcache並發數在400-500,安裝后並發數在5000左右,緩存效果提升10倍以上...
