Skip to content

本地性能调试

Tideways、xhprof 和 xhgui 打造 PHP 非侵入式监控平台



环境准备

安装之前确保已经正确安装了以下软件

  • PHP
  • Nginx
  • Mongodb

安装 PHP mongodb 扩展

$ sudo pecl install mongodb

PHP 配置文件中添加

[mongodb]
extension=mongodb.so

安装 PHP tideaways 扩展

常规编译安装

$ git clone https://github.com/tideways/php-xhprof-extension.git
$ cd /path/php-xhprof-extension
$ phpize
$ ./configure
$ make
$ sudo make install

PHP 配置文件中添加

[tideways]
extension=tideways_xhprof.so
; 不需要自动加载,在程序中控制就行
tideways.auto_prepend_library=0
; 频率设置为100,在程序调用时可以修改
tideways.sample_rate=100

安装 xhgui-branch(xhgui 的汉化版)

$ git clone https://github.com/laynefyc/xhgui-branch.git
$ cd xhgui-branch
$ php install.php

修改 xhgui-branch 配置文件

<?php
return array(
     ...
    'extension' => 'tideways_xhprof',
     ...
    'save.handler' => 'mongodb',
    'db.host' => 'mongodb://127.0.0.1:27017',
    'db.db' => 'xhprof',
     ...
);

启动 mongodb 并设置 xhgui 索引,命令如下:

$ 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 } )

xhgui 本地虚拟主机配置参考

server {
    listen       80;
    server_name  xhgui.test;
    root         /path/xhgui-branch/webroot;
    # access_log  logs/access.log;
    error_log    logs/error.log;
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        index  index.php index.html index.htm;
    }
    location ~ \.php$ {
        fastcgi_pass localhost:9000;
        fastcgi_read_timeout 150;
        fastcgi_index index.php;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

在项目入口的 PHP文件中 配置,告诉 PHP 程序在执行前要调用的服务

 require_once('/path/wwwroot/xhgui-branch/external/header.php');