Skywalking php扩展(SkyAPM-php-sdk)安装

安装的背景

为了监控服务器上php调用各个服务的流量和请求延迟等数据,选择了Skywalking,但是由于安装过程异常不顺利,所以写下安装过程。
1. 尝试了 pecl 安装失败
2. 按照官方文档安装,安装失败
3. lib使用的较多,文档没有全部说明

准备工作

apt install php7.4-dev # 更具自己的php版本选择
apt install php7.4-curl # 更具自己的php版本选择
apt install libcurl4-gnutls-dev libcurl4-openssl-dev build-essential libssl-dev build-essential autoconf libtool pkg-config cmake autoconf automake libtool curl make g++ unzip

安装GRPC 和 protobuf

这两个扩展的安装失败的概率比较大,建议细心检查一下。安装顺序不建议对掉。编译后整个文件夹会有4GB左右大小,占空5G多,建议在SSD上编译并且保持外网速度足够,要不然时间会非常久。

apt-get install build-essential autoconf automake libtool curl make g++ unzip pkg-config cmake -y
git clone -b v1.34.x https://github.com/grpc/grpc.git
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:/usr/local/lib64/
# grpc
cd grpc/
git submodule update --init
mkdir -p cmake/build
cd cmake/build
cmake ../.. -DBUILD_SHARED_LIBS=ON -DgRPC_INSTALL=ON
make -j$(nproc)
make install
ldconfig
# protobuf
cd grpc/third_party/protobuf
./autogen.sh
./configure
make -j$(nproc)
make install
ldconfig

安装php扩展

这边基本是常规操作了,make的时候注意一下不能加-j会出现莫名其妙问题,扩展不大对编译时间也没太大影响。

curl -Lo v4.1.1.tar.gz https://github.com/SkyAPM/SkyAPM-php-sdk/archive/v4.1.1.tar.gz
tar zxvf v4.1.1.tar.gz
cd SkyAPM-php-sdk-4.1.1
phpize
./configure
make
sudo make install

编译后文件夹大概有 108MB 如果差距较大检查一下是不是文件有缺失。

du -d 1 -h

108M    ./skyapm-php-sdk

接下来就是改php.ini等常规操作不在赘述

; Loading extensions in PHP
extension=skywalking.so

; enable skywalking
skywalking.enable = 1

; Set skyWalking collector version (5 or 6 or 7 or 8)
skywalking.version = 8

; Set app code e.g. MyProjectName
skywalking.app_code = MyProjectName

; Set grpc address
skywalking.grpc=127.0.0.1:11800

结果

php7.4 --ri skywalking

skywalking

Directive => Local Value => Master Value
skywalking.enable => On => On
skywalking.version => 6 => 6
skywalking.app_code => MyProjectName => MyProjectName
skywalking.authentication => no value => no value
skywalking.grpc => 192.168.10.5:11800 => 192.168.10.5:11800
skywalking.grpc_tls_enable => 0 => 0
skywalking.grpc_tls_pem_root_certs => no value => no value
skywalking.grpc_tls_pem_private_key => no value => no value
skywalking.grpc_tls_pem_cert_chain => no value => no value
skywalking.log_enable => 0 => 0
skywalking.log_path => /tmp => /tmp
skywalking.error_handler_enable => 0 => 0

补充说明

  1. 比较多的朋友在php 扩展编译的 ./configure 时候出现奇奇怪怪的 lib缺失问题,建议官网下载一个比较新的 cmake,然后重复上面步骤。
  2. 扩展加载报错。试试 sudo ldconfig -v

Dockerfile

有很多朋友说还是有问题,我直接贴一个Dockerfile希望能帮到大家

FROM php:8.0.7-cli as lib-image

ENV GRPC_RELEASE_TAG v1.31.x
ENV PROTOBUF_RELEASE_TAG 3.13.x
RUN mkdir -p /usr/local/lib && mkdir -p /usr/local/lib64
ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:/usr/local/lib64
ENV LD_RUN_PATH=$LD_RUN_PATH:/usr/local/lib:/usr/local/lib64

RUN set -ex \
    && echo "--- prepare depends ---" \
    && apt-get update && apt install -y build-essential libssl-dev build-essential autoconf libtool pkg-config cmake autoconf automake libtool curl make g++ unzip git \
    && echo "--- clone grpc ---" \
    && git clone --depth 1 -b ${GRPC_RELEASE_TAG} https://github.com/grpc/grpc ./grpc \
    && cd ./grpc \
    && git submodule update --init --recursive

RUN echo "--- installing protobuf ---" \
    && cd ./grpc/third_party/protobuf \
    && ./autogen.sh && ./configure && make -j$(nproc) && make install && make clean

RUN echo "--- installing grpc ---" \
    && cd ./grpc \
    && mkdir -p cmake/build && cd cmake/build && cmake ../.. -DBUILD_SHARED_LIBS=ON -DgRPC_INSTALL=ON \
    && make -j$(nproc) && make install && make clean
RUN rm -rf ./grpc

RUN docker-php-source extract
RUN apt-get update && apt install -y git libboost-all-dev libcurl4-gnutls-dev
RUN cd /usr/src/php/ext/mysqli && phpize && ./configure && make && make install
RUN echo "--- installing skywalking php ---" \
    && git clone --depth 1 https://github.com/SkyAPM/SkyAPM-php-sdk.git -b v4.1.2 ./skywalking \
    && cd ./skywalking \
    && phpize && ./configure && make && make install &&docker-php-ext-enable skywalking

RUN apt-get update && apt-get install -y \
apt-utils  \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
libgmp-dev \
libzip-dev \
zlib1g-dev \
libssl-dev \
htop \
iftop

RUN git clone https://github.com/swoole/swoole-src.git -b v4.6.x --depth 1 ./swoole-src
RUN cd ./swoole-src && \
phpize && \
./configure \
--enable-http2 && \
make && make install && docker-php-ext-enable swoole

RUN pecl install mongodb &&\
    docker-php-ext-enable mongodb

RUN pecl install redis &&\
    docker-php-ext-enable redis

RUN docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/ && \
docker-php-ext-install -j$(nproc) gmp zip gd pcntl mysqli pdo_mysql

RUN curl -sS https://getcomposer.org/installer -o composer-setup.php
RUN php composer-setup.php --install-dir=/usr/local/bin --filename=composer

RUN apt-get clean

FROM php:8.0.7-cli
ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:/usr/local/lib64
ENV LD_RUN_PATH=$LD_RUN_PATH:/usr/local/lib:/usr/local/lib64
ENV C_INCLUDE_PATH=/usr/src/php:/usr/include/mariadb/server
ENV CPLUS_INCLUDE_PATH=/usr/src/php:/usr/include/mariadb/server
COPY --from=lib-image /usr/local/lib /usr/local/lib
COPY --from=lib-image /usr/local/lib64 /usr/local/lib64
COPY --from=lib-image /usr/local/bin /usr/local/bin
COPY --from=lib-image /usr/local/include /usr/local/include
COPY --from=lib-image /usr/local/etc/php/conf.d /usr/local/etc/php/conf.d
COPY --from=lib-image /usr/lib /usr/lib
RUN apt-get update && apt install -y git
RUN php -m
点赞

发表回复

电子邮件地址不会被公开。必填项已用 * 标注