- wget
去這里找最新的包下載,我現在最新的是xhprof-0.9.4.tgzcd /tmp mkdir xhprof && cd xhprof wget http://pecl.php.net/get/xhprof-0.9.4.tgz tar -xvf xhprof-0.9.4.tgz cd xhprof-0.9.4/extension/ phpize
我在進行這步的時候,提示我沒有安裝phpize,下一步就是安裝phpize,如果已經安裝了phpize,可以跳過下一步
- phpize
因為phpize在php-devel包里面,所以先需要安裝php-develyum search php-devel
如果你和我一樣,找不到這個包的話,建議添加電子科技大學的源
cd /tmp wget http://mirrors.stuhome.net/files/centos_repo/6/CentOS-Base.repo cp CentOS-Base.repo /etc/yum.repos.d/CentOS-Base-Uestc.repo -v yum makecache yum install php-devel -y
裝好了phpize就可以進行安裝了
phpize
- 接下來是源碼安裝三部曲:
whereis php-config /*看看你的php-config在什么路徑,然后放到后面的path-to php-config,一般是/usr/bin/php-config */ ./configure --with-php-config=<path to php-config > make make install mkdir -p /var/tmp/xhprof
- 編譯添加了xhprof.so文件,現在需要配置php啟用這個so文件,去/etc/php.d/文件夾下面添加文件xhprof.ini
extension=xhprof.so xhprof.outout_dir=/var/tmp/xhprof /*請確定上面配置的/var/tmp/xhprof目錄是存在的*/
然后就是重新啟動你的php,之后用命令查看
php -ini | grep xhprof -i
看看是否已經安裝上了,安裝好了之后,就可以開始設置監控了
- 將文件拷貝到網站目錄里面,然后添加監控代碼到統計文件中
mkdir -p /var/www/xhprof && cd /var/www/xhprof/ cp -r /tmp/xhprof/xhprof-0.9.4/xhprof_html/ ./ cp -r /tmp/xhprof/xhprof-0.9.4/examples/ ./ cp -r /tmp/xhprof/xhprof-0.9.4/xhprof_lib/ ./ mkdir logs
配置nginx的虛擬主機目錄,在/etc/nginx/conf.d/目錄下面新建xhprof.conf
然后修改文件內容為:server{ listen 80; server_name effect.vpsbuy.info; root /var/www/xhprof; access_log /var/www/xhprof/logs/access.log; error_log /var/www/xhprof/logs/error.log; index index.php; location = favicon.ico { log_not_found off; access_log off; } location = /robots.txt{ allow all; log_not_found off; access_log off; } location / { root /var/www/xhprof; } location ~ \.php$ { include fastcgi_params; fastcgi_pass php; fastcgi_index xhprof_html/index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }
重新啟動nginx
nginx -t nginx -s reload
修改examples目錄下面的sample.php文件,將最后一行的echo改成這樣
echo "---------------\n". "Assuming you have set up the http based UI for \n". "XHProf at some address, you can view run at \n". "http://effect.vpsbuy.info/xhprof_html/index.php?run=$run_id&source=xhprof_foo\n". "---------------\n";
將這個
http://effect.vpsbuy.info/xhprof_html/
改成你自己的url路徑。
然后就是配置監控$xhprof_enable = false; if(mt_rand(1,10)==1){ //這里設置監控的比例 xhprof_enable(); $xhprof_enable = true; } //這里寫上你要監控的函數 if($xhprof_enable ){ $xhprof_data = xhprof_disable(); //$XHPROF_ROOT = realpath(dirname(__FILE__) .'/..'); $XHPROF_ROOT = /var/www/xhprof; include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php"; include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php"; // save raw data for this profiler run using default // implementation of iXHProfRuns. $xhprof_runs = new XHProfRuns_Default(); // save the run under a namespace "xhprof_foo" $run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo"); }
現在就可以統計了,然后就是安裝生成訪問圖像的
yum install graphviz
如果你覺得寫得不錯,請捐贈作者:
作者:
- 原文地址:http://www.factj.com/archives/147.html
- 微博:http://weibo.com/fudance
- 郵箱:strivescript#sina.com