温馨提示×

centos怎样升级apache

小樊
52
2026-08-03 22:30:41
栏目: 智能运维

在 CentOS 上升级 Apache(httpd)主要有两种方式:通过系统包管理器升级(推荐) 和 通过源码编译升级。下面分别说明,并给出常见场景的操作步骤。


一、通过 yum / dnf 升级(推荐,适用于 CentOS 7 / 8 / Stream)

1️⃣ 查看当前 Apache 版本

httpd -v

2️⃣ 升级 Apache(CentOS 7)

yum update httpd -y

升级完成后重启:

systemctl restart httpd

3️⃣ 升级 Apache(CentOS 8 / Stream)

CentOS 8 默认使用 dnf:

dnf update httpd -y
systemctl restart httpd

4️⃣ 检查是否升级成功

httpd -v

二、启用第三方仓库升级到更新版本(如 Apache 2.4.5x)

CentOS 官方仓库中的 Apache 版本通常较旧,如果需要较新的 Apache 2.4 版本,可以使用 Software Collections (SCL) 或 Remi 仓库。

✅ 使用 Remi 仓库(推荐)

1️⃣ 安装 Remi 仓库

# CentOS 7
yum install epel-release -y
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm -y
# CentOS 8
dnf install epel-release -y
dnf install http://rpms.remirepo.net/enterprise/remi-release-8.rpm -y

2️⃣ 查看可用 Apache 版本

yum list available | grep httpd

3️⃣ 升级 Apache

yum update httpd --enablerepo=remi -y

或指定版本:

yum install httpd -y --enablerepo=remi

三、源码编译升级(不推荐,维护成本高)

适合需要特定版本或自定义模块的情况。

1️⃣ 安装依赖

yum groupinstall "Development Tools" -y
yum install openssl-devel pcre-devel expat-devel -y

2️⃣ 下载 Apache 源码

wget https://downloads.apache.org/httpd/httpd-2.4.58.tar.gz
tar -zxvf httpd-2.4.58.tar.gz
cd httpd-2.4.58

3️⃣ 编译安装

./configure --prefix=/usr/local/apache \
--enable-so \
--enable-ssl \
--with-mpm=prefork

make
make install

⚠️ 注意:源码安装不会自动创建 systemd 服务,需要手动配置。


四、升级前注意事项(非常重要)

✅ 1. 备份配置文件

cp -r /etc/httpd /etc/httpd.bak

✅ 2. 检查配置文件是否兼容

httpd -t

✅ 3. 确认模块兼容性

httpd -M

✅ 4. 升级后测试

systemctl status httpd
curl -I http://localhost

五、CentOS 版本与 Apache 推荐方式总结

CentOS 版本 推荐方式
CentOS 7 yum + Remi
CentOS 8 dnf + Remi
CentOS Stream dnf + Remi
特殊需求 源码编译

如果你愿意,可以告诉我:

  • ✅ CentOS 具体版本(cat /etc/centos-release)
  • ✅ 当前 Apache 版本
  • ✅ 是否使用 PHP / SSL / 虚拟主机

我可以给你一套完全适配你服务器的升级方案 ✅

0 踩