
本文详解在自定义安装 OpenSSL 3.x 后,如何通过环境变量精准配置 PHP 8.1 的 ./configure 过程,解决 Package requirements (openssl >= 1.0.2) were not met: No package 'openssl' found 错误。
本文详解在自定义安装 openssl 3.x 后,如何通过环境变量精准配置 php 8.1 的 ./configure 过程,解决 package requirements (openssl >= 1.0.2) were not met: no package 'openssl' found 错误。
PHP 源码编译过程中对 OpenSSL 的检测依赖 pkg-config 工具查找 openssl.pc 文件(而非仅依赖头文件或库路径)。当你将 OpenSSL 3.x 安装至 /opt/openssl 这类非标准路径时,pkg-config 默认无法定位其元信息文件,因此即使已正确设置 --with-openssl=/opt/openssl,configure 脚本仍会失败——因为它本质上调用的是 pkg-config --cflags --libs openssl,而该命令因缺少 .pc 文件或 PKG_CONFIG_PATH 指向错误而报错。
✅ 正确做法是绕过 pkg-config 的自动查找机制,显式提供编译与链接所需的全部参数:
-
OPENSSL_CFLAGS:指定 OpenSSL 头文件路径(如-I/opt/openssl/include) -
OPENSSL_LIBS:指定库路径及链接库(如-L/opt/openssl/lib64 -lssl -lcrypto) - (可选但推荐)
PKG_CONFIG_PATH:若 OpenSSL 安装时生成了.pc文件(例如执行make install前运行./config --prefix=/opt/openssl --libdir=lib64 pkgconfig),可将其所在目录加入该变量(如/opt/openssl/lib64/pkgconfig);否则可设为/usr/local/lib/pkgconfig或留空。
注意:--with-openssl 参数在此场景下应指向 OpenSSL 的 bin 目录(即 --with-openssl=/opt/openssl/bin),而非根目录或 lib 目录——PHP configure 脚本会从中读取 openssl 可执行文件版本,并用于部分功能校验(如 openssl version -v),这是官方文档明确要求的用法。
以下是经验证的完整 configure 命令(适配你的安装路径):
./configure \ OPENSSL_CFLAGS="-I/opt/openssl/include" \ OPENSSL_LIBS="-L/opt/openssl/lib64 -lssl -lcrypto" \ PKG_CONFIG_PATH="/opt/openssl/lib64/pkgconfig:/usr/local/lib/pkgconfig" \ --with-config-file-path=/etc/php \ --sysconfdir=/etc/php.d \ --enable-mysqlnd \ --with-pdo-mysql=mysqlnd \ --with-pdo-pgsql=/usr/bin/pg_config \ --enable-bcmath \ --enable-fpm \ --with-fpm-user=root \ --with-fpm-group=root \ --enable-mbstring \ --enable-shmop \ --enable-sockets \ --enable-sysvmsg \ --enable-sysvsem \ --enable-sysvshm \ --with-zlib \ --with-curl \ --with-pear \ --with-openssl=/opt/openssl/bin \ --enable-pcntl \ --enable-gd \ --with-jpeg \ --with-mysqli \ --with-readline \ --with-freetype \ --with-ldap
? 关键注意事项:
- 确保
/opt/openssl/lib64/下存在libssl.so.3和libcrypto.so.3(你已创建软链,但需确认ldconfig -p | grep ssl能识别); - 若后续
make阶段报undefined reference to SSL_*,请检查OPENSSL_LIBS中的-lssl -lcrypto顺序不可颠倒(libssl依赖libcrypto); - 编译完成后,运行
php -i | grep -i openssl验证是否成功启用,输出中应包含OpenSSL Library Version => OpenSSL 3.0.5 5 Jul 2022和OpenSSL Header Version => OpenSSL 3.0.5 5 Jul 2022; - 不建议覆盖系统默认 OpenSSL(如你做的
/bin/openssl替换),生产环境应优先使用发行版维护的 OpenSSL,自定义安装仅用于构建依赖。
掌握这一模式后,你同样可复用于其他需要显式链接第三方库(如 cURL、libxml2、ICU)的 PHP 编译场景——核心原则始终是:当 pkg-config 失效时,用 XXX_CFLAGS + XXX_LIBS 显式接管。
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!











