FROM php:8.1-fpm-alpine AS xhprof
WORKDIR /app
# graphviz and ttf-freefont are required to vizualize functions calls
RUN apk update && apk add git autoconf make g++ graphviz ttf-freefont
RUN git clone https://github.com/longxinH/xhprof.git ./xhprof && \
cd xhprof/extension/ && \
phpize && \
./configure && \
make && \
make install
RUN apk del git autoconf make g++
RUN echo $'extension = xhprof.so \n\
xhprof.output_dir = /app/var/xhprof' > /usr/local/etc/php/conf.d/xhprof.ini
WORKDIR /app/xhprof/xhprof_html
CMD ["/usr/local/bin/php", "-S", "0.0.0.0:8090"]
EXPOSE 8090
Or without alpine:
FROM php:8.1-fpm AS xhprof
WORKDIR /www
# graphviz and ttf-freefont are required to vizualize functions calls
RUN apt update && apt install -y git autoconf make g++ graphviz fonts-freefont-ttf
RUN git clone https://github.com/longxinH/xhprof.git ./xhprof && \
cd xhprof/extension/ && \
phpize && \
./configure && \
make && \
make install
RUN echo 'extension = xhprof.so \n\
xhprof.output_dir = /www/xhprof/xhprof_runs' > /usr/local/etc/php/conf.d/xhprof.ini
WORKDIR /www/xhprof/xhprof_html
CMD ["/usr/local/bin/php", "-S", "0.0.0.0:8090"]
EXPOSE 8090
If you use docker compose
don't forget to add configuration:
services:
xhprof:
restart: unless-stopped
build:
context: ./
dockerfile: .docker/php/Dockerfile # path to your XHProf Dockerfile
target: xhprof
ports:
- "8090:8090"
volumes:
- .:/app
# ...
# Install XHProf extension
COPY --from=xhprof /app/xhprof/extension/modules/xhprof.so /tmp/xhprof.so
RUN mv /tmp/xhprof.so $(php-config --extension-dir)/xhprof.so
COPY --from=xhprof /usr/local/etc/php/conf.d/xhprof.ini /usr/local/etc/php/conf.d/xhprof.ini
# ...
xhprof_enable();
// app code
// ...
// end app code
$xhprofData = xhprof_disable();
$XHPROF_ROOT = '/app/xhprof'; // or any other path to php lib
include_once $XHPROF_ROOT . '/xhprof_lib/utils/xhprof_lib.php';
include_once $XHPROF_ROOT . '/xhprof_lib/utils/xhprof_runs.php';
$xhprofRuns = new \XHProfRuns_Default();
$runId = $xhprofRuns->save_run($xhprofData, 'my_app');