linux搭建php性能分析工具xhgui+tideways_xhprof

先上效果图:
linux搭建php性能分析工具xhgui+tideways_xhprof
linux搭建php性能分析工具xhgui+tideways_xhprof
linux搭建php性能分析工具xhgui+tideways_xhprof

安装环境

Centos、php7、MongoDB3、nginx

php扩展

mongodb:http://pecl.php.net/package/mongodb
tideways_xhprof:https://github.com/tideways/php-xhprof-extension

安装MongoDB3

默认我的环境yum安装的是MongoDB2版本,xhgui要求3版本及以上。

创建MongoDB3的yum源

vim /etc/yum.repos.d/mongodb-org-3.4.repo

文件内容为:

[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc

安装

yum install -y mongodb-org

网络不好时间可能稍长

启动MongoDB

service mongod start

增加索引

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

如果需要调整默认端口或者权限等信息自行搜索。

安装xgui汉化版项目

项目地址:

https://github.com/laynefyc/xhgui-branch

下载项目代码

git clone https://github.com/laynefyc/xhgui-branch.git

composer安装

cd xhgui-branch
composer install

目录权限

chmod -R 777 cache

nginx配置

为xhgui项目配置虚拟主机,以下为我的配置,仅供参考:

server {
        listen       80;
        server_name php.monitor.com;
        root    /your/project/path/xhgui-branch/webroot;
        index index.html index.php;
        rewrite_log on;
        location / {
               try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ [^/]\.php(/|$)
        {
                try_files $uri =404;
                fastcgi_pass  127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi.conf;
        }

        access_log  /log/path/php_monitor-access.log  access;
        error_log  /log/path/php_monitor-error.log  error;
}

编辑配置文件

vim /your/project/path/xhgui-branch/config/config.default.php

调整以下几点:

...
'extension' => 'tideways_xhprof',
...
'profiler.enable' => function() {
        if($_SERVER['SERVER_NAME'] == 'your.project.domain.com'){
                // 100%采样,默认为1%
                return rand(1, 100) === 42;
        }else{
                return False;
        }
    },
...

mongodb连接信息根据自己情况填写。

配置需要监控的项目

调整需要监控项目的nginx配置,增加以下配置:

fastcgi_param PHP_VALUE "auto_prepend_file=/your/project/path/xhgui-branch/external/header.php";

重启nginx

nginx -s reload

初次访问可能会因为采样频率问题暂时没有数据,可以临时调高采样频率。

猜你喜欢

转载自blog.51cto.com/3502902/2486204