1.安装相关软件

1.1 安装tideways-xhprof

a. 编辑.env配置文件
PHP_FPM_INSTALL_XHPROF=true
b. 代码脚本

如果使用的是laradock,以下代码会在php-fpm/Dockerfile中,无需编写

###########################################################################
# 安装 Xhprof:
ARG INSTALL_XHPROF=false
RUN if [ ${INSTALL_XHPROF} = true ]; then \
# Install the php xhprof extension
if [ $(php -r "echo PHP_MAJOR_VERSION;") = 7 ]; then \
curl -L -o /tmp/xhprof.tar.gz "https://github.com/tideways/php-xhprof-extension/archive/v4.1.7.tar.gz"; \
else \
curl -L -o /tmp/xhprof.tar.gz "https://codeload.github.com/phacility/xhprof/tar.gz/master"; \
fi \
&& mkdir -p xhprof \
&& tar -C xhprof -zxvf /tmp/xhprof.tar.gz --strip 1 \
&& ( \
cd xhprof \
&& phpize \
&& ./configure \
&& make \
&& make install \
) \
&& rm -r xhprof \
&& rm /tmp/xhprof.tar.gz \
;fi

COPY php-fpm/xhprof.ini /usr/local/etc/php/conf.d

RUN if [ ${INSTALL_XHPROF} = false ]; then \
rm /usr/local/etc/php/conf.d/xhprof.ini \
;fi
###########################################################################

经实践发现,只有v4.1.x版本才能收集到SQL执行详情.

c.安装命令
# 重新构建PHP-FPM镜像
docker-compose build php-fpm
# 删除老的的PHP-FPM服务
docker-compose stop php-fpm && docker-compose rm -f php-fpm
# 重新启动PHP-FPM
docker-compose up -d php-fpm

1.2 安装Mogodb

a.启动Mongo
# 安装使用
docker-compose up -d mongo
b.初始化Mongo
$ 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 } )

1.3 下载xhgui-branch

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

下载之后,需要进入项目执行composer install

root@55930fd205be:/var/www/xhgui-branch# composer install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Warning: The lock file is not up to date with the latest changes in composer.json. You may be getting outdated dependencies. Run update to update them.
Package operations: 14 installs, 0 updates, 0 removals
- Installing pimple/pimple (v1.1.1): Downloading (100%)
- Installing slim/slim (2.6.3): Downloading (100%)
- Installing slim/views (0.1.3): Downloading (100%)
- Installing twig/twig (v1.33.0): Downloading (100%)
- Installing mongodb/mongodb (1.1.2): Downloading (100%)
- Installing alcaeus/mongo-php-adapter (1.0.10): Downloading (100%)
- Installing phpunit/php-token-stream (1.2.2): Downloading (100%)
- Installing symfony/yaml (v2.8.19): Downloading (100%)
- Installing phpunit/php-text-template (1.2.1): Downloading (100%)
- Installing phpunit/phpunit-mock-objects (1.2.3): Downloading (connecting...)

2.配置设置

2.1 设置(xhgui-branch)Nginx站点

添加Nginx配置

server {
listen 80;
server_name php-view.com;
root /var/www/xhgui-branch/webroot;
location / {
index index.php;
if (!-e $request_filename) {
rewrite . /index.php last;
}
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php-upstream;
fastcgi_index index.php;
try_files $uri =404;
}
}

2.2 修改xhgui-branch配置

配置文件位置xhgui-branch/config/config.default.php

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

3.使用

3.1 修改被监控项目Nginx

如果要监控项目A的性能。需要修改项目A对应的Nginx配置,添加:fastcgi_param "auto_prepend_file=/xxx/xhgui-branch/external/header.php"

如下示例:

server {
listen 80;
....
location ~ \.php$ {
include fastcgi_params;
# 只需要添加这一行,告诉 PHP 程序在执行前要调用的服务
fastcgi_param "auto_prepend_file=/xxx/xhgui-branch/external/header.php";
....
}
}

3.2 访问

在访问之前需要重启Nginx

image-20210521152102942

image-20210604181852686

image-20210604182036381

image-20210604182939332