编译配置PHP-8.1关联OpenSSL3时提示找不到openssl包,求解决
问题描述
已编译安装OpenSSL3到/opt/openssl,验证版本正常:
./config -fPIC shared --prefix=/opt/openssl make test make install ln -s /opt/openssl/lib64/libssl.so.3 /usr/lib64/libssl.so.3 ln -s /opt/openssl/lib64/libcrypto.so.3 /usr/lib64/libcrypto.so.3 sudo mv /bin/openssl /bin/openssl.backup ln -s /opt/openssl/bin/openssl /usr/bin/ openssl version # 输出:OpenSSL 3.0.5 5 Jul 2022 (Library: OpenSSL 3.0.5 5 Jul 2022)
配置PHP8.1时使用以下命令:
./configure --with-config-file-path=/etc/php \ --sysconfdir=/etc/php.d \ --enable-mysqlnd \ --with-pdo-mysql \ --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-phpdbg \ --enable-shmop \ --enable-sockets \ --enable-sysvmsg \ --enable-sysvsem \ --enable-sysvshm \ --with-zlib \ --with-curl \ --with-pear \ --with-openssl=/opt/openssl \ --with-openssl-dir=/opt/openssl \ --enable-pcntl \ --enable-gd \ --with-jpeg \ --with-mysqli \ --with-readline \ --with-freetype \ --with-ldap
出现错误:
checking for openssl >= 1.0.2... no
configure: error: Package requirements (openssl >= 1.0.2) were not met:No package 'openssl' found
Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.Alternatively, you may set the environment variables OPENSSL_CFLAGS
and OPENSSL_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
错误原因
PHP的configure脚本优先通过pkg-config检测OpenSSL,而自定义安装的OpenSSL的.pc配置文件(位于/opt/openssl/lib64/pkgconfig/openssl.pc)不在系统默认的PKG_CONFIG_PATH路径中,导致pkg-config无法找到OpenSSL包信息。
解决方法
方法1:指定PKG_CONFIG_PATH环境变量
在执行PHP的configure命令前,先设置环境变量指向OpenSSL的pkgconfig目录:
export PKG_CONFIG_PATH=/opt/openssl/lib64/pkgconfig
然后再运行原来的configure命令即可。
方法2:直接设置OPENSSL_CFLAGS和OPENSSL_LIBS
绕开pkg-config,直接告诉configure OpenSSL的头文件和库文件位置:
export OPENSSL_CFLAGS="-I/opt/openssl/include" export OPENSSL_LIBS="-L/opt/openssl/lib64 -lssl -lcrypto"
之后再执行PHP的configure命令。
额外说明
PHP8.1本身完全支持OpenSSL3,无需额外打补丁。如果仍有问题,可以检查:
/opt/openssl/lib64/pkgconfig目录下是否存在openssl.pc文件,若不存在可能是OpenSSL编译时未生成,可重新编译OpenSSL并确保./config参数正确。- 系统中是否安装了
pkg-config工具,若未安装需先通过包管理器安装(如yum install pkgconfig或apt install pkg-config)。
内容的提问来源于stack exchange,提问作者Belokonev Alexnadr

