解决 CentOS 7系统宝塔面板安装 PHP fileinfo 扩展失败问题

      发布在:后端技术      评论:0 条评论

一、问题背景

最近在宝塔面板中为 PHP 8.0 安装 fileinfo 扩展时,任务日志反复出现以下错误:

checking whether the C compiler works... no
configure: error: in `/www/server/php/80/src/ext/fileinfo':
configure: error: C compiler cannot create executables

同时,在命令行运行 yum 相关操作时,也会看到:

http://vault.epel.cloud/.../repodata/repomd.xml: [Errno 14] HTTP Error 403 - Forbidden
Repository epel is listed more than once in the configuration

这些现象都指向同一个根源:CentOS 7 已于 2024 年 6 月 30 日结束生命周期(EOL),官方镜像源停止服务,导致所有基于 YUM 的软件包安装全部失败,编译 PHP 扩展所需的 C 编译器也无法安装。

二、错误分析

  1. YUM 源失效:默认的官方源返回 403 Forbidden,vault.epel.cloud 等归档源也不再提供访问。

  2. 重复仓库定义:旧系统中可能有多份 EPEL 仓库配置文件,导致 epel is listed more than once 警告。

  3. 编译器缺失:宝塔的安装脚本依赖 devtoolset-7-gcc 套件来提供较新的 GCC 编译器(GCC 7.3.1),但由于 SCLo 仓库的 GPG 公钥无法自动获取,整个编译器安装失败,最终导致 configure 阶段报错。

三、安全解决全流程

以下操作均在 root 用户下执行,所有修改均具备可逆性,确保服务器安全。

3.1 备份旧 YUM 源配置

cp -a /etc/yum.repos.d /etc/yum.repos.d.bak.$(date +%Y%m%d_%H%M%S)

3.2 替换为可用镜像源

清空所有旧仓库文件:

rm -f /etc/yum.repos.d/*.repo

创建 阿里云 CentOS Vault 归档源 配置文件(已固定至最终版 7.9.2009,避免找不到资源):

cat > /etc/yum.repos.d/CentOS-Base.repo <<'EOF'
[base]
name=CentOS-7 - Base (aliyun)
baseurl=http://mirrors.aliyun.com/centos-vault/7.9.2009/os/$basearch/
gpgcheck=1
enabled=1
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-7

[updates]
name=CentOS-7 - Updates (aliyun)
baseurl=http://mirrors.aliyun.com/centos-vault/7.9.2009/updates/$basearch/
gpgcheck=1
enabled=1
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-7

[extras]
name=CentOS-7 - Extras (aliyun)
baseurl=http://mirrors.aliyun.com/centos-vault/7.9.2009/extras/$basearch/
gpgcheck=1
enabled=1
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-7
EOF

cat > /etc/yum.repos.d/epel.repo <<'EOF'
[epel]
name=Extra Packages for Enterprise Linux 7 (aliyun)
baseurl=http://mirrors.aliyun.com/epel/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=http://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-7
EOF

cat > /etc/yum.repos.d/CentOS-SCLo-scl-rh.repo <<'EOF'
[centos-sclo-rh]
name=CentOS-7 - SCLo rh (aliyun)
baseurl=http://mirrors.aliyun.com/centos/7/sclo/$basearch/rh/
gpgcheck=1
enabled=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-SIG-SCLo
EOF

安全说明:所有仓库均开启 gpgcheck=1,保留签名验证。GPG 密钥地址采用官方链接,但后续可能需要手动导入(见下文)。

3.3 重建 YUM 缓存并验证

yum clean all
yum makecache
yum repolist

此时应显示 base, updates, extras, epel, centos-sclo-rh 共计约 38,000 个软件包,且无任何 403 或重复仓库错误。

3.4 安装基础编译工具

yum groupinstall -y "Development Tools"
yum install -y kernel-headers-$(uname -r) gcc gcc-c++ make

这一步会装上系统自带的 GCC 4.8.5,但后续宝塔脚本仍会尝试安装 devtoolset-7,因此我们继续处理。

3.5 解决 devtoolset-7 安装与 GPG 密钥问题

直接在宝塔面板再次安装 fileinfo 时,日志中会看到:

warning: ... Header V4 RSA/SHA1 Signature, key ID f2ee9d55: NOKEY
Public key for devtoolset-7-binutils... is not installed
GPG key retrieval failed: [Errno 14] HTTP Error 404 - Not Found

这说明 centos-sclo-rh 仓库需要 GPG 密钥 RPM-GPG-KEY-CentOS-SIG-SCLo,但自动获取失败。我们需要手动导入。

方法从 CentOS 官方源导入(推荐)

rpm --import https://www.centos.org/keys/RPM-GPG-KEY-CentOS-SIG-SCLo

若无任何输出,即表示导入成功。可用以下命令验证:

rpm -q gpg-pubkey --qf '%{NAME}-%{VERSION}-%{RELEASE}\t%{SUMMARY}\n' | grep f2ee9d55

如果看到含 f2ee9d55 的条目,则导入完成。

3.6 手动安装 devtoolset-7 编译器套件

GPG 密钥就位后,执行:

yum install -y devtoolset-7-gcc devtoolset-7-gcc-c++

验证安装:

scl enable devtoolset-7 'gcc --version'

输出应为 gcc (GCC) 7.3.1 20180303 ...,说明编译器就绪。

3.7 重新安装 fileinfo 扩展

回到宝塔面板,进入 软件商店已安装PHP-8.0安装扩展,找到 fileinfo 并点击安装。  
此时任务日志会显示编译通过,扩展安装成功。

四、附加优化建议

  1. 锁定仓库配置防止变更  
    避免系统更新覆盖 vault 源配置:

   echo 'exclude=centos-release*' >> /etc/yum.conf
热门推荐