簡介
xhgui基于xhprof,以圖形化方式顯示結果。簡單點就是更加直觀。 采用tideways + xhgui
組合的原因是我的PHP版本是7,而xhprof不支持,(雖然我安裝了一個支持php7的xhprof,但是猜測和xhgui不兼容。)
前提
我的PHP版本是PHP7:
MongoDB的版本是V3.6.2:
安裝步驟
1.安裝mongodb(mac下面安裝mongodb,這個自己google或者baidu吧)
brew install mongodb
2.安裝php的mongodb的擴展
brew install php70-mongo
查看是否安裝成功(記得重啟php-fpm)
3.安裝php的tideways擴展
brew install php70-tideways
cd /usr/local/etc/php/7.0/conf.d
vim ext-tideways_xhprof.ini
添加內容(這個就是編譯成功之后的路徑)
[tideways_xhprof]
extension="/usr/local/Cellar/php70/7.0.14_7/lib/php/extensions/no-debug-non-zts-20151012/tideways_xhprof.so"
;不需要自動加載,在程序中控制就行
tideways.auto_prepend_library=0
;頻率設置為100,在程序調用時能改
tideways.sample_rate=100
查看是否安裝成功(記得重啟php-fpm)
4.安裝 xhgui
cd /Users/birjemin/Developer/Php
git clone https://github.com/laynefyc/xhgui-branch.git
// 我這里把xhgui-branch目錄重命名為xhprof_gui
cd xhprof_gui/extension
php install.php
配置xhgui(extension, profiler.enable, db.host, db.db參數)
cd /Users/birjemin/Developer/Php/xhprof_gui/config
cp config.default.php config.php
vim config.php
恭喜安裝成功!
使用步驟
1.mongodb需要新建相應的db(上面的第四點confi.php里面的配置) 新建索引優化查詢
$ mongo
> use xhprof
> db.results.ensureIndex( { 'meta.SERVER.REQUEST_TIME' : -1 } )
> db.results.ensureIndex( { 'profile.main().wt' : -1 } )
> db.results.ensureIndex( { 'profile.main().mu' : -1 } )
> db.results.ensureIndex( { 'profile.main().cpu' : -1 } )
> db.results.ensureIndex( { 'meta.url' : 1 } )
2.在監聽的網站nginx配置加上
- fastcgi_param TIDEWAYS_SAMPLERATE “25”;
3.可以在需要監聽的接口中代碼片段前面引入header.php
include "/Users/birjemin/Developer/Php/xhprof_gui/external/header.php";
4.將克隆的xprof_gui配置虛擬主機,這個和你的項目一樣的,就把xprof也當做一個項目,配置成瀏覽器可訪問。比如我的配置:
- host: 127.0.0.1 local.xhprof_gui.com
- nginx server conf
server {
listen 80;
server_name local.xhprof-gui.com;
# root directive should be global
root /Users/birjemin/Developer/Php/xhprof_gui/webroot;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
5.重啟nginx,瀏覽器訪問local.xhprof-gui.com看看能不能訪問(沒有目錄??你是不是瀏覽器無法訪問目錄??權限沒開。。自己配置一下)
6.在postman或者瀏覽器訪問接口,轉啊轉,好了之后就可以去 http://local.xhprof-gui.com/
查看了。(圖我就不截了。。)
遇到的問題
1.安裝完了,跑起來數據為空
我猜測我安裝的xhprof雖然支持PHP7,但是和xhgui不兼容,把config.php里面的 extension
參數改成 tideways_xhprof
而不是 xhprof
2.mongoDb報錯
這是一個bug(issue),請按照這個方法修改相應文件。
3.為啥不在nginx里面配置
fastcgi_param PHP_VALUE "auto_prepend_file=/Users/birjemin/Developer/Php/xhprof_gui/external/header.php";
而是
include "/Users/birjemin/Developer/Php/xhprof_gui/external/header.php";
因為我這是本地調試,重在分析某一個接口,而不是觀測線上的正式環境。還有這個原因issues
備注
重啟php-fpm(視個人重啟方式而定,我的重啟方式是這樣的)
cd /usr/local/etc/php/7.0/
sudo killall php-fpm
sudo /usr/local/Cellar/php70/7.0.14_7/sbin/php-fpm -D