首先我们先安装 nghttp2 库
# 对于基于Debian/Ubuntu的系统
apt-get install libnghttp2-dev
# 对于基于RHEL/CentOS的系统
yum install libnghttp2-devel
# 提前删除可能存在的curl目录 php8.0以上的版本才会在这个目录安装
rm -rf /usr/local/curl_2
面板安装的时候需要使用编译安装而不是快速安装,点击安装后提示选择下载节点就可以删除任务了
修改文件 /www/server/panel/install/php.sh 找到代码 添加新模块 --with-nghttp2
Install_Curl_New(){
if [ ! -f "/usr/local/curl_2/bin/curl" ];then
curlVersion="7.74.0"
wget ${download_Url}/src/curl-${curlVersion}.tar.gz
tar -zxf curl-${curlVersion}.tar.gz
cd curl-${curlVersion}
rm -rf /usr/local/curl_2
./configure --prefix=/usr/local/curl_2 --enable-ldap --enable-ldaps --with-brotli --with-libssh2 --with-libssh --enable-ares --with-gssapi --without-nss --enable-smb --with-libidn2 --with-ssl=/usr/local/openssl111 --with-nghttp2
[ $? -ne 0 ] && Error_Msg
make -j${cpuCore}
if [ $? -ne 0 ];then
make clean
./configure --prefix=/usr/local/curl_2 --disable-ldap --disable-ldaps --with-brotli --with-libssh2 --with-libssh --enable-ares --with-gssapi --without-nss --enable-smb --with-libidn2 --with-ssl=/usr/local/openssl111 --with-nghttp2
make -j${cpuCore}
fi
make install
cd ..
rm -f curl-${curlVersion}.tar.gz
rm -rf curl-${curlVersion}
fi
}
然后使用命令安装 php
cd /www/server/panel/install
bash php.sh install 8.3
测试 php curl 是否支持 http2
php -r "if(extension_loaded('curl')) { echo 'HTTP/2支持: ' . (curl_version()['features'] & CURL_VERSION_HTTP2 ? '是' : '否') . PHP_EOL; } else { echo 'curl扩展未加载\n'; }"
然后我们解答为什么出现这个情况。
最近再写采集模块的过程中发现 curl http版本是1.1的话经常会被403,测试发现如果是http2的话就不会被403,然后就开始升级curl版本,发现宝塔面板的安装过程中curl模块没有开启 http2模块,而且php也没开启http2模块,经过上时间的测试安装,终于发现了这个安装脚本的坑,使用面板安装每次都会重新安装但是curl模块无法开启http2,所有才有了上面的脚本。
完整的脚本地址:
https://raw.githubusercontent.com/yooer/bt_php_sh/refs/heads/main/php.sh