记录一次安装 PHP-Protobuf 的过程。
开发环境
- 操作系统:CentOS Linux release 7.9.2009 (Core)
- PHP:8.1.21
在安装 php-protobuf 扩展之前,需要先安装 protobuf 编译器。
1. 安装 Protobuf 依赖
1.0. Protobuf 的依赖:
- git
- gcc
- bazel
1.1. 安装 git
[root@iZ2zehdkhnyvzixuie7bzhZ tmp] yum -y install git
1.2. 安装 GCC 11
CentOS 7.9 使用 yum 安装 GCC 最高版本是 4.8.5 ,在安装 Protobuf 时会出现错误,需要安装更高级别的 GCC 。
这里安装 GCC 的版本 11:
[root@iZ2zehdkhnyvzixuie7bzhZ tmp] yum install -y centos-release-scl
[root@iZ2zehdkhnyvzixuie7bzhZ tmp] yum install -y devtoolset-11-gcc devtoolset-11-gcc-c++
# 启用 gcc 11 版本
[root@iZ2zehdkhnyvzixuie7bzhZ tmp] scl enable devtoolset-11 bash
[root@iZ2zehdkhnyvzixuie7bzhZ tmp] gcc --version
gcc (GCC) 11.2.1 20220127 (Red Hat 11.2.1-9)
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1.3. 安装 bazel 6.3
使用 bazel 4.x 版本来编译 Protobuf 也会出现异常,尝试使用 6.3 版本来编译能够顺利成功。
依赖项:
- Bash
- zip、unzip
- C++ 构建工具链
- JDK:必须提供版本 11。
- Python。版本 2 和版本 3 受支持,安装其中一个版本就足够了。
安装依赖
[root@iZ2zehdkhnyvzixuie7bzhZ tmp] yum -y install java-11-openjdk zip unzip
[root@iZ2zehdkhnyvzixuie7bzhZ tmp] yum -y groupinstall "Development Tools"
安装 Python 3
刚开始使用 CentOS 7.9 自带的 python 2.7 安装。
默认版本:
[root@iZ2zehdkhnyvzixuie7bzhZ tmp]# python --version
Python 2.7.5
现有的 python 可执行文件:
[root@iZ2zehdkhnyvzixuie7bzhZ tmp]# find /usr/bin -name python*
/usr/bin/python2
/usr/bin/python2.7
/usr/bin/python
虽然 bazel 官方说 python 版本 2 的也可以,不过我在运行编译脚本的时候,会出现错误。在网上查了一下,发现有人提到需要将 Python 升级到 3.x 的版本。
不过在将 Python 切换到 3.x 版本之后,yum 命令会受到影响,运行时会出现以下这样的错误:
[root@iZ2zehdkhnyvzixuie7bzhZ tmp]# yum --help
File "/usr/bin/yum", line 30
except KeyboardInterrupt, e:
^
SyntaxError: invalid syntax
所以需要在 bazel 6.3 完成安装后,将 python 默认版本切换回为 2.7。
安装 Python 3
[root@iZ2zehdkhnyvzixuie7bzhZ tmp] yum -y install python3
# 先将 python 默认版本切换到 3.x
[root@iZ2zehdkhnyvzixuie7bzhZ tmp] cp /usr/bin/python3 /usr/bin/python
下载独立于架构的单个分发归档
独立于架构的单个分发归档的文件后缀为
-dist.zip。 归档页面:github.com/bazelbuild/…
[root@iZ2zehdkhnyvzixuie7bzhZ tmp] wget https://github.com/bazelbuild/bazel/releases/download/6.3.0/bazel-6.3.0-dist.zip
[root@iZ2zehdkhnyvzixuie7bzhZ tmp] unzip bazel-6.3.0-dist.zip -d bazel-6.3.0-dist
[root@iZ2zehdkhnyvzixuie7bzhZ tmp] cd bazel-6.3.0-dist
运行编译脚本
[root@iZ2zehdkhnyvzixuie7bzhZ bazel-6.3.0-dist] env EXTRA_BAZEL_ARGS="--tool_java_runtime_version=local_jdk" bash ./compile.sh
这里需要大概 30 分钟左右。
安装
将 bazel 二进制文件复制到 PATH 上的目录中。
[root@iZ2zehdkhnyvzixuie7bzhZ bazel-6.3.0-dist] cp output/bazel /usr/bin
[root@iZ2zehdkhnyvzixuie7bzhZ bazel-6.3.0-dist] bazel --version
bazel 6.3.0- (@non-git)
bazel安装完成后,将 python 切换为 2.7 版本,这样就不会影响到 yum 的使用。
[root@iZ2zehdkhnyvzixuie7bzhZ tmp] cp /usr/bin/python /usr/bin/python2.7
2. 安装 Protobuf
2.1. 下载源代码
[root@iZ2zehdkhnyvzixuie7bzhZ tmp] git clone git@github.com:protocolbuffers/protobuf.git
[root@iZ2zehdkhnyvzixuie7bzhZ tmp] cd protobuf-23.4
2.2. 克隆子模块并生成配置脚本
[root@iZ2zehdkhnyvzixuie7bzhZ protobuf-23.4] git submodule update --init --recursive
这里克隆子模块过程中出现异常终止,需要反复多运行几次。
2.3. 构建 C++ Protocol Buffer 运行时和 Protocol Buffer 编译器 (protoc)
[root@iZ2zehdkhnyvzixuie7bzhZ protobuf-23.4] bazel build :protoc :protobuf
这里安装也需要挺长一段时间。
2.4. 安装编译器
[root@iZ2zehdkhnyvzixuie7bzhZ protobuf-23.4] cp bazel-bin/protoc /usr/bin
[root@iZ2zehdkhnyvzixuie7bzhZ protobuf-23.4] protoc --version
libprotoc 23.4
3. 安装 PHP-Protobuf 扩展
3.1 安装依赖
- libtool
- make
- gcc:这个在上面已经安装过了。
- pear:这个在安装 PHP 8.1 的时候,已经集成在
/application/php/8.1/bin/目录中。 - pecl:这个在安装 PHP 8.1 的时候,已经集成在
/application/php/8.1/bin/目录中。
所以这里只需要安装 libtool 和 make。
[root@iZ2zehdkhnyvzixuie7bzhZ protobuf-23.4] yum -y install libtool make
3.2. 编译安装
[root@iZ2zehdkhnyvzixuie7bzhZ protobuf-23.4] cd php/ext/google/protobuf
[root@iZ2zehdkhnyvzixuie7bzhZ protobuf] /application/php/8.1/bin/phpize
[root@iZ2zehdkhnyvzixuie7bzhZ protobuf] ./configure --with-php-config=/application/php/8.1/bin/php-config
[root@iZ2zehdkhnyvzixuie7bzhZ protobuf] make && make install
3.3. 配置
在/application/php/8.1/lib/php.ini文件中加入 extension=protobuf 的配置项来开启 protobuf 扩展。
[php-protobuf]
extension=protobuf
检查是否成功安装:
[root@iZ2zehdkhnyvzixuie7bzhZ protobuf] /application/php/8.1/bin/php -m | grep "protobuf"
protobuf
:-) 这样就安装成功了。