Apache介绍和安装

举报
Raymond运维 发表于 2025/09/24 15:32:18 2025/09/24
【摘要】 Apache介绍和安装

Apache介绍和安装

1.Apache介绍

Apache一般指Apache HTTP Server,是Apache软件基金会的一个开源网页服务器软件,以下为你详细介绍:

基本信息

它具有简单、快速、可靠等特点,能运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性,被广泛使用,是最流行的Web服务器软件之一。

发展历程

  • 1995年,美国国家超级电脑应用中心(NCSA)的HTTPd服务器项目停滞后,开发者们将修补代码汇总,形成了Apache HTTP Server,同年4月正式发布。
  • 此后不断更新完善,功能日益强大,逐渐成为互联网上使用最广泛的Web服务器软件。

特点

  • 开放源代码:遵循Apache许可证,用户可自由使用、修改和分发,众多开发者参与贡献,不断改进其功能和性能。
  • 跨平台性:支持多种操作系统,如Windows、Linux、Unix等,方便不同环境部署。
  • 模块化设计:拥有丰富模块,如身份验证、URL重写、缓存等,用户可根据需求灵活加载或卸载,定制服务器功能。
  • 稳定性与可靠性:经过长期发展和大量实践检验,能在高负载下稳定运行,确保网站持续提供服务。
  • 安全性:提供多种安全机制,如SSL/TLS加密、访问控制、防止DDoS攻击等,保护网站和用户数据安全。

应用场景

  • 企业网站:为各类企业搭建官方网站,展示企业信息、产品和服务,处理客户咨询和订单。
  • 电子商务平台:支撑在线购物网站,处理大量用户访问、交易数据和支付流程,保障交易安全稳定。
  • 内容管理系统(CMS):作为WordPress、Drupal等CMS的服务器平台,发布和管理各种类型的内容。

2.Apache安装

2.1 主机初始化

2.1.1 设置网卡名

  • Rocky Linux 9/10、Almalinux 9/10、CentOS Stream 9/10、AnolisOS 23、OpenCloudOS 9:

    • 创建 systemd 链接文件: 创建 /etc/systemd/network/70-eth0.link 文件。

      [root@rocky10 ~]# mkdir -p /etc/systemd/network/
      [root@rocky10 ~]# touch /etc/systemd/network/70-eth0.link
      
      [root@rocky10 ~]# ip addr
      1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
          link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
          inet 127.0.0.1/8 scope host lo
             valid_lft forever preferred_lft forever
          inet6 ::1/128 scope host noprefixroute 
             valid_lft forever preferred_lft forever
      2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
          link/ether 00:0c:29:02:55:30 brd ff:ff:ff:ff:ff:ff
          altname enp3s0
          altname enx000c29025530
          inet 172.31.15.1/20 brd 172.31.15.255 scope global dynamic noprefixroute ens160
             valid_lft 1776sec preferred_lft 1776sec
          inet6 fe80::20c:29ff:fe02:5530/64 scope link noprefixroute 
             valid_lft forever preferred_lft forever
      
      [root@rocky10 ~]# cat > /etc/systemd/network/70-eth0.link << EOF
      [Match]
      MACAddress=00:0c:29:f8:60:8f
      
      [Link]
      Name=eth0
      EOF
      
      # 也可以执行下面命令
      ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`
      ETHMAC=`ip addr show ${ETHNANME} | awk -F' ' '/ether/{print $2}'`
      mkdir /etc/systemd/network/
      touch /etc/systemd/network/70-eth0.link
      cat > /etc/systemd/network/70-eth0.link << EOF
      [Match]
      MACAddress=${ETHMAC}
      
      [Link]
      Name=eth0
      EOF
      
    • 修改 NetworkManager 配置文件: 如果使用 NetworkManager 管理网络,需要修改 /etc/NetworkManager/system-connections 下的配置文件。

      [root@rocky10 ~]# mv /etc/NetworkManager/system-connections/ens160.nmconnection /etc/NetworkManager/system-connections/eth0.nmconnection
      
      [root@rocky10 ~]# sed -i.bak 's/'ens160'/eth0/' /etc/NetworkManager/system-connections/eth0.nmconnection
      
      # 也可以执行下面命令
      mv /etc/NetworkManager/system-connections/${ETHNAME}.nmconnection /etc/NetworkManager/system-connections/eth0.nmconnection
      sed -i.bak 's/'${ETHNAME}'/eth0/' /etc/NetworkManager/system-connections/eth0.nmconnection
      
  • Rocky Linux 8、Almalinux 8、CentOS 7、CentOS Stream 8、openEuler 22.03和24.03 LTS、AnolisOS 8、OpenCloudOS 8、Kylin Server v10/v11、UOS Server v20:

    • 修改 GRUB 配置文件: 打开 /etc/default/grub 文件,添加以下内容到 GRUB_CMDLINE_LINUX

      net.ifnames=0 biosdevname=0
      

      执行以下命令:

      sed -ri.bak '/^GRUB_CMDLINE_LINUX=/s@"$@ net.ifnames=0 biosdevname=0"@' /etc/default/grub
      

      然后更新 GRUB 配置:

      grub2-mkconfig -o /boot/grub2/grub.cfg
      

      如果是 UEFI 引导系统,更新命令为:

      # Rocky
      grub2-mkconfig -o /boot/efi/EFI/rocky/grub.cfg
      
      # Almalinux
      grub2-mkconfig -o /boot/efi/EFI/almalinux/grub.cfg
      
      # CentOS
      grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg
      
      # openEuler
      grub2-mkconfig -o /boot/efi/EFI/openEuler/grub.cfg
      
      # AnolisOS
      grub2-mkconfig -o /boot/efi/EFI/anolis/grub.cfg
      
      # OpenCloudOS
      grub2-mkconfig -o /boot/efi/EFI/opencloudos/grub.cfg
      
      # Kylin Server
      grub2-mkconfig -o /boot/efi/EFI/kylin/grub.cfg
      
      # UOS Server
      grub2-mkconfig -o /boot/efi/EFI/uos/grub.cfg
      
      # UEFI 引导系统可以直接执行下面命令:
      EFI_DIR=`find /boot/efi/ -name "grub.cfg" | awk -F"/" '{print $5}'`
      grub2-mkconfig -o /boot/efi/EFI/${EFI_DIR}/grub.cfg
      
    • 创建 Udev 规则文件: 创建 /etc/udev/rules.d/10-network.rules 文件,并添加以下内容:

      SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="<网卡MAC地址>", NAME="eth0"
      

      **注意:**创建Udev这个步骤只在AnolisOS 8上执行。

      <网卡MAC地址> 替换为实际网卡的 MAC 地址。

      [root@anolisos8 ~]# ip addr
      1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
          link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
          inet 127.0.0.1/8 scope host lo
             valid_lft forever preferred_lft forever
          inet6 ::1/128 scope host 
             valid_lft forever preferred_lft forever
      2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
          link/ether 00:0c:29:47:02:3f brd ff:ff:ff:ff:ff:ff
          altname enp2s1
          inet 172.31.15.25/20 brd 172.31.15.255 scope global dynamic noprefixroute ens33
             valid_lft 1785sec preferred_lft 1785sec
          inet6 fe80::20c:29ff:fe47:23f/64 scope link noprefixroute 
             valid_lft forever preferred_lft forever
      
      [root@anolis8 ~]# cat >> /etc/udev/rules.d/10-network.rules << EOF
      SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="00:0c:29:c5:38:3b", NAME="eth0"
      EOF
      
      # 也可以执行下面命令
      ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`
      ETHMAC=`ip addr show ${ETHNANME} | awk -F' ' '/ether/{print $2}'`
      cat >> /etc/udev/rules.d/10-network.rules << EOF
      SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="${ETHMAC}", NAME="eth0"
      EOF
      
    • 修改网卡配置文件: 创建 /etc/sysconfig/network-scripts/ifcfg-eth0 文件,并配置网卡参数,例如:

      ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`
      mv /etc/sysconfig/network-scripts/ifcfg-${ETHNAME} /etc/sysconfig/network-scripts/ifcfg-eth0
      sed -i.bak 's/'${ETHNAME}'/eth0/' /etc/sysconfig/network-scripts/ifcfg-eth0
      
  • Ubuntu和Debian:

    # Ubuntu和Debian先启用root用户,并设置密码
    raymond@ubuntu2404:~$ cat set_root_login.sh 
    #!/bin/bash
    
    read -p "请输入密码: " PASSWORD
    echo ${PASSWORD} |sudo -S sed -ri 's@#(PermitRootLogin )prohibit-password@\1yes@' /etc/ssh/sshd_config
    sudo systemctl restart sshd
    sudo -S passwd root <<-EOF
    ${PASSWORD}
    ${PASSWORD}
    EOF
    
    raymond@ubuntu2404:~$ bash set_root_login.sh 
    请输入密码: 123456
    [sudo] password for raymond: New password: Retype new password: passwd: password updated successfully
    
    • 修改 GRUB 配置文件: 打开 /etc/default/grub 文件,添加以下内容到 GRUB_CMDLINE_LINUX

      net.ifnames=0 biosdevname=0
      

      执行以下命令:

      sed -ri.bak '/^GRUB_CMDLINE_LINUX=/s@"$@net.ifnames=0 biosdevname=0"@' /etc/default/grub
      

      然后更新 GRUB 配置:

      grub-mkconfig -o /boot/grub/grub.cfg
      

      如果是 UEFI 引导系统,更新命令为:

      # Ubuntu
      grub-mkconfig -o /boot/efi/EFI/ubuntu/grub.cfg
      
      # Debian
      grub-mkconfig -o /boot/efi/EFI/debian/grub.cfg
      
      # UEFI 引导系统可以直接执行下面命令:
      EFI_DIR=`find /boot/efi/ -name "grub.cfg" | awk -F"/" '{print $5}'`
      grub-mkconfig -o /boot/efi/EFI/${EFI_DIR}/grub.cfg
      
    • 修改网卡配置文件

      # Ubuntu Server 24.04 LTS
      ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`
      sed -i.bak 's/'${ETHNAME}'/eth0/' /etc/netplan/50-cloud-init.yaml
      
      # Ubuntu Server 22.04  LTS
      ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`
      touch /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
      cat > /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg <<-EOF
      network: {config: disabled}
      EOF
      sed -i.bak 's/'${ETHNAME}'/eth0/' /etc/netplan/50-cloud-init.yaml
      
      # Ubuntu Server 20.04 LTS
      ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`
      sed -i.bak 's/'${ETHNAME}'/eth0/' /etc/netplan/00-installer-config.yaml
      
      # Ubuntu Server 18.04 LTS
      ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`
      sed -i.bak 's/'${ETHNAME}'/eth0/' /etc/netplan/01-netcfg.yaml
      
      # Debian
      ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`
      sed -i.bak 's/'${ETHNAME}'/eth0/' /etc/network/interfaces
      
  • 重启系统

    reboot
    

    重启后,网卡名称将变为 eth0

2.1.2 设置ip地址

ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`

# IP变量是ip地址,PREFIX变量是子网掩码位数;GATEWAY变量是网关地址;PRIMARY_DNS变量是主DNS,BACKUP_DNS变量是备用DNS,根据自己的需求修改。
IP=172.31.0.10
PREFIX=20
GATEWAY=172.31.0.2
PRIMARY_DNS=223.5.5.5
BACKUP_DNS=180.76.76.76

Rocky Linux 9/10、Almalinux 9/10、CentOS Stream 9/10、AnolisOS 23、OpenCloudOS 9:

cat > /etc/NetworkManager/system-connections/${ETHNAME}.nmconnection <<-EOF
[connection]
id=${ETHNAME}
type=ethernet
interface-name=${ETHNAME}

[ipv4]
address1=${IP}/${PREFIX},${GATEWAY}
dns=${PRIMARY_DNS};${BACKUP_DNS};
method=manual
EOF

Rocky Linux 8、Almalinux 8、CentOS 7、CentOS Stream 8、openEuler 22.03和24.03 LTS、AnolisOS 8、OpenCloudOS 8、Kylin Server v10/v11、UOS Server v20:

cat > /etc/sysconfig/network-scripts/ifcfg-${ETHNAME} <<-EOF
NAME=${ETHNAME}
DEVICE=${ETHNAME}
ONBOOT=yes
BOOTPROTO=none
TYPE=Ethernet
IPADDR=${IP}
PREFIX=${PREFIX}
GATEWAY=${GATEWAY}
DNS1=${PRIMARY_DNS}
DNS2=${BACKUP_DNS}
EOF

Ubuntu:

# Ubuntu Server 18.04 LTS
cat > /etc/netplan/01-netcfg.yaml <<-EOF
network:
  version: 2
  renderer: networkd
  ethernets:
    ${ETHNAME}:
      dhcp4: no
      dhcp6: no
      addresses: [${IP}/${PREFIX}] 
      gateway4: ${GATEWAY}
      nameservers:
        addresses: [${PRIMARY_DNS}, ${BACKUP_DNS}]

# Ubuntu Server 20.04 LTS
cat > /etc/netplan/00-installer-config.yaml <<-EOF
network:
  version: 2
  renderer: networkd
  ethernets:
    ${ETHNAME}:
      dhcp4: no
      dhcp6: no
      addresses: [${IP}/${PREFIX}] 
      gateway4: ${GATEWAY}
      nameservers:
        addresses: [${PRIMARY_DNS}, ${BACKUP_DNS}]
EOF

# Ubuntu Server 22.04/24.04 LTS
cat > /etc/netplan/50-cloud-init.yaml <<-EOF
network:
  version: 2
  renderer: networkd
  ethernets:
    ${ETHNAME}:
      dhcp4: no
      dhcp6: no
      addresses: [${IP}/${PREFIX}]
      routes:
        - to: default
          via: ${GATEWAY}
      nameservers:
        addresses: [${PRIMARY_DNS}, ${BACKUP_DNS}]
EOF

Debian:

sed -ri -e "s/allow-hotplug/auto/g" -e "s/dhcp/static/g" /etc/network/interfaces
sed -i '/static/a\address '${IP}'/'${PREFIX}'\ngateway '${GATEWAY}'\ndns-nameservers '${PRIMARY_DNS}' '${BACKUP_DNS}'\n' /etc/network/interfaces

openSUSE:

cat > /etc/sysconfig/network/ifcfg-${ETHNAME} <<-EOF
STARTMODE='auto'
BOOTPROTO='static'
IPADDR='${IP}/${PREFIX}'
EOF
touch /etc/sysconfig/network/routes
cat > /etc/sysconfig/network/routes  <<-EOF
default ${GATEWAY} - -
EOF
sed -ri  's/(NETCONFIG_DNS_STATIC_SERVERS=).*/\1"'${PRIMARY_DNS}' '${BACKUP_DNS}'"/g' /etc/sysconfig/network/config

2.1.3 配置镜像源

Rocky:

# 阿里云镜像站Rocky系统镜像源地址是:mirrors.aliyun.com/rockylinux,火山引擎镜像站Rocky系统镜像源地址是:mirrors.volces.com/rockylinux,后面地址是rockylinux,使用下面命令执行
MIRROR=mirrors.aliyun.com
sed -i.bak -e 's|^mirrorlist=|#mirrorlist=|g' -e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://'${MIRROR}'/rockylinux|g' /etc/yum.repos.d/[Rr]ocky*.repo

# 搜狐镜像站Rocky系统镜像源地址是:mirrors.sohu.com/Rocky,后面地址是Rocky,使用下面命令执行
MIRROR=mirrors.sohu.com
sed -i.bak -e 's|^mirrorlist=|#mirrorlist=|g' -e 's|^#baseurl=http://'${OLD_MIRROR}'/$contentdir|baseurl=https://'${MIRROR}'/Rocky|g' /etc/yum.repos.d/[Rr]ocky*.repo

# 其余镜像站Rocky系统镜像源地址后面地址都是rocky,使用下面命令执行
MIRROR=mirrors.tencent.com
sed -i.bak -e 's|^mirrorlist=|#mirrorlist=|g' -e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://'${MIRROR}'/rocky|g' /etc/yum.repos.d/[Rr]ocky*.repo

dnf clean all && dnf makecache

Almalinux:

MIRROR=mirrors.tencent.com
OLD_MIRROR=$(sed -rn '/^.*baseurl=/s@.*=http.*://(.*)/(.*)/\$releasever/.*/$@\1@p' /etc/yum.repos.d/almalinux*.repo | head -1)
sed -i.bak -e 's|^mirrorlist=|#mirrorlist=|g' -e 's|^# baseurl=https://'${OLD_MIRROR}'|baseurl=https://'${MIRROR}'|g' /etc/yum.repos.d/almalinux*.repo

CentOS Stream 9和10:

cat update_mirror.pl
#!/usr/bin/perl

use strict;
use warnings;
use autodie;

# 要修改镜像源,请去修改url变量!
my $url = 'mirrors.aliyun.com';
my $mirrors = "https://$url/centos-stream";

if (@ARGV < 1) {
    die "Usage: $0 <filename1> <filename2> ...\n";
}

while (my $filename = shift @ARGV) {
    my $backup_filename = $filename . '.bak';
    rename $filename, $backup_filename;

    open my $input, "<", $backup_filename;
    open my $output, ">", $filename;

    while (<$input>) {
        s/^metalink/# metalink/;

        if (m/^name/) {
            my (undef, $repo, $arch) = split /-/;
            $repo =~ s/^\s+|\s+$//g;
            ($arch = defined $arch ? lc($arch) : '') =~ s/^\s+|\s+$//g;

            if ($repo =~ /^Extras/) {
                $_ .= "baseurl=${mirrors}/SIGs/\$releasever-stream/extras" . ($arch eq 'source' ? "/${arch}/" : "/\$basearch/") . "extras-common\n";
            } else {
                $_ .= "baseurl=${mirrors}/\$releasever-stream/$repo" . ($arch eq 'source' ? "/" : "/\$basearch/") . ($arch ne '' ? "${arch}/tree/" : "os") . "\n";
            }
        }

        print $output $_;
    }
}

rpm -q perl &> /dev/null || { echo -e "\\033[01;31m "安装perl工具,请稍等..."\033[0m";yum -y install perl ; }

perl ./update_mirror.pl /etc/yum.repos.d/centos*.repo

dnf clean all && dnf makecache

CentOS Stream 8:

MIRROR=mirrors.aliyun.com
sed -i.bak -e 's|^mirrorlist=|#mirrorlist=|g' -e 's|^#baseurl=http://mirror.centos.org/$contentdir|baseurl=https://'${MIRROR}'/centos-vault|g' /etc/yum.repos.d/CentOS-*.repo

dnf clean all && dnf makecache

CentOS 7:

MIRROR=mirrors.aliyun.com
OS_RELEASE_FULL_VERSION=`cat /etc/centos-release | sed -rn 's/^(CentOS Linux release )(.*)( \(Core\))/\2/p'`
sed -i.bak -e 's|^mirrorlist=|#mirrorlist=|g' -e 's|^#baseurl=http://mirror.centos.org/centos|baseurl=https://'${MIRROR}'/centos-vault|g' -e "s/\$releasever/${OS_RELEASE_FULL_VERSION}/g" /etc/yum.repos.d/CentOS-*.repo

yum clean all && yum makecache

Ubuntu Server 24.04 LTS:

MIRROR=mirrors.aliyun.com
sed -ri "s@^(URIs: )(http.*://)(.*)(/ubuntu).?@\1https://${MIRROR}\4@g" /etc/apt/sources.list.d/ubuntu.sources

apt update

Ubuntu Server 18.04/20.04/22.04 LTS:

MIRROR=mirrors.aliyun.com
OLD_MIRROR=`sed -rn "s@^deb http(.*)://(.*)/ubuntu/? $(lsb_release -cs) main.*@\2@p" /etc/apt/sources.list`
SECURITY_MIRROR=`sed -rn "s@^deb http(.*)://(.*)/ubuntu.* $(lsb_release -cs)-security main.*@\2@p" /etc/apt/sources.list`
sed -i.bak -e 's@http.*://'${OLD_MIRROR}'@https://'${MIRROR}'@g' -e 's@http.*://'${SECURITY_MIRROR}'@https://'${MIRROR}'@g' /etc/apt/sources.list

apt update

Debian:

MIRROR=mirrors.aliyun.com
OLD_MIRROR=`sed -rn "s@^deb http(.*)://(.*)/debian/? $(lsb_release -cs) main.*@\2@p" /etc/apt/sources.list`
SECURITY_MIRROR=`sed -rn "s@^deb http(.*)://(.*)/debian-security $(lsb_release -cs)-security main.*@\2@p" /etc/apt/sources.list`
sed -ri.bak -e 's/'${OLD_MIRROR}'/'${MIRROR}'/g' -e 's/'${SECURITY_MIRROR}'/'${MIRROR}'/g' -e 's/^(deb cdrom.*)/#\1/g' /etc/apt/sources.list

apt update

openEuler:

MIRROR=mirrors.aliyun.com
OLD_MIRROR=$(awk -F'/' '/^baseurl=/{print $3}' /etc/yum.repos.d/openEuler.repo | head -1)
sed -i.bak -e 's|^metalink=|#metalink=|g' -e 's|http.*://'${OLD_MIRROR}'|https://'${MIRROR}'/openeuler|g' /etc/yum.repos.d/openEuler.repo

dnf clean all && dnf makecache

AnolisOS:

OLD_MIRROR=$(sed -rn '/^.*baseurl=/s@.*=http.*://(.*)/(.*)/\$releasever/.*/$@\1@p' /etc/yum.repos.d/AnolisOS*.repo | head -1)

# 中国科学院软件研究所镜像站AnolisOS系统镜像源地址是:mirror.iscas.ac.cn/openanolis,后面地址是openanolis,使用下面命令执行
MIRROR=mirror.iscas.ac.cn
sed -i.bak -e 's|http.*://'${OLD_MIRROR}'/anolis|https://'${MIRROR}'/openanolis|g' /etc/yum.repos.d/AnolisOS*.repo

# 其余镜像站AnolisOS系统镜像源地址后面地址都是anolis,使用下面命令执行
MIRROR=mirrors.aliyun.com
sed -i.bak -e 's|http.*://'${OLD_MIRROR}'/anolis|https://'${MIRROR}'/anolis|g' /etc/yum.repos.d/AnolisOS*.repo

dnf clean all && dnf makecache

OpenCloudOS:

MIRROR=mirrors.tencent.com
OLD_MIRROR=$(awk -F'/' '/^baseurl=/{print $3}' /etc/yum.repos.d/OpenCloudOS*.repo | head -1)
sed -i.bak -e 's|http.*://'${OLD_MIRROR}'|https://'${MIRROR}'|g' /etc/yum.repos.d/OpenCloudOS*.repo

dnf clean all && dnf makecache

openSUSE:

MIRROR=mirrors.aliyun.com
OLD_MIRROR=$(awk -F'/' '/^baseurl=/{print $3}' /etc/zypp/repos.d/repo-*.repo | head -1)
sed -i.bak 's|http.*://'${OLD_MIRROR}'|https://'${MIRROR}'/opensuse|g' /etc/zypp/repos.d/repo-*.repo

zypper clean && zypper refresh

2.1.4 关闭防火墙

# Rocky、Almalinux、CentOS、openEuler、AnolisOS、OpenCloudOS、openSUSE、Kylin Server、UOS Server
systemctl disable --now firewalld

# CentOS 7还要执行下面命令
systemctl disable --now NetworkManager

# Ubuntu
systemctl disable --now ufw

# Debian默认没有安装防火墙,不用设置

2.1.5 禁用SELinux

# Rocky、Almalinux、CentOS、openEuler、AnolisOS、OpenCloudOS、Kylin Server、UOS Server
setenforce 0
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config

# Ubuntu、Debian和openSUSE默认没有安装SELinux,不用设置

2.1.6 禁用AppArmor

# openSUSE发行版默认不启用SELinux,而是使用AppArmor来提供强制访问控制(MAC)功能,需要禁用AppArmor。
systemctl disable --now apparmor

2.1.7 设置时区

timedatectl set-timezone Asia/Shanghai
echo 'Asia/Shanghai' >/etc/timezone

# Ubuntu还要设置下面内容
cat >> /etc/default/locale <<-EOF
LC_TIME=en_DK.UTF-8
EOF

2.2 包安装

2.2.1 Yum资源库安装Httpd

在Rocky、Almalinux、CentOS、openEuler、AnolisOS、OpenCloudOS、Kylin Server、UOS Server上安装httpd:

[root@rocky10 ~]# yum list httpd --showduplicates
Last metadata expiration check: 0:00:56 ago on Tue 23 Sep 2025 04:03:20 PM CST.
Available Packages
httpd.x86_64                                     2.4.63-1.el10                                          appstream
httpd.x86_64                                     2.4.63-1.el10_0.2                                      appstream

[root@rocky10 ~]# yum install -y httpd

[root@rocky10 ~]# systemctl daemon-reload && systemctl enable --now httpd

[root@rocky10 ~]# ss -ntl |grep 80
LISTEN 0      511                *:80              *:*

[root@rocky10 ~]# httpd -v
Server version: Apache/2.4.63 (Rocky Linux)
Server built:   Jul 15 2025 00:00:00

2.2.2 Apt资源库安装Apache2

在Ubuntu和Debian上安装apache2:

root@ubuntu2404:~# apt-cache madison apache2
   apache2 | 2.4.58-1ubuntu8.8 | https://mirrors.aliyun.com/ubuntu noble-updates/main amd64 Packages
   apache2 | 2.4.58-1ubuntu8.8 | https://mirrors.aliyun.com/ubuntu noble-security/main amd64 Packages
   apache2 | 2.4.58-1ubuntu8 | https://mirrors.aliyun.com/ubuntu noble/main amd64 Packages

root@ubuntu2404:~# apt install -y apache2

root@ubuntu2404:~# systemctl daemon-reload && systemctl enable --now apache2

root@ubuntu2404:~# ss -ntl |grep 80
LISTEN 0      511                *:80              *:*          

root@ubuntu2404:~# apache2 -v
Server version: Apache/2.4.58 (Ubuntu)
Server built:   2025-08-11T11:10:09

2.2.3 SUSE资源库安装Apache2

在openSUSE上安装apache2:

opensuse15:~ # zypper search "apache2"
Loading repository data...
Reading installed packages...

S | Name                                | Summary                                                    | Type
--+-------------------------------------+------------------------------------------------------------+-----------
  | apache2                             | The Apache HTTPD Server                                    | package
  | apache2                             | The Apache HTTPD Server                                    | srcpackage
  | apache2-devel                       | The Apache HTTPD Server                                    | package
  | apache2-devel                       | The Apache HTTPD Server                                    | srcpackage
  | apache2-event                       | The Apache HTTPD Server                                    | package
  | apache2-event                       | The Apache HTTPD Server                                    | srcpackage
  | apache2-icons-oxygen                | Oxygen icons for Apache 2                                  | package
  | apache2-manual                      | The Apache HTTPD Server                                    | package
  | apache2-manual                      | The Apache HTTPD Server                                    | srcpackage
  | apache2-mod_apparmor                | AppArmor module for apache2                                | package
  | apache2-mod_auth_gssapi             | GSSAPI Module for Apache                                   | package
  | apache2-mod_auth_kerb               | Kerberos Module for Apache                                 | package
  | apache2-mod_auth_mellon             | A SAML 2.0 authentication module for the Apache Server     | package
  | apache2-mod_auth_mellon-diagnostics | Build of mod_auth_mellon with diagnostic logging           | package
  | apache2-mod_auth_mellon-doc         | Documentation for mod_auth_mellon module                   | package
  | apache2-mod_auth_openidc            | Apache2.x module for an OpenID Connect enabled Identity -> | package
  | apache2-mod_auth_openidc            | Apache2.x module for an OpenID Connect enabled Identity -> | srcpackage
  | apache2-mod_authn_otp               | Apache module for one-time password authentication         | package
  | apache2-mod_dnssd                   | Apache2 module for Zeroconf support via DNS-SD             | package
  | apache2-mod_encoding                | Non-ASCII filename interoperability module for the Apach-> | package
  | apache2-mod_evasive                 | Denial of Service evasion module for Apache                | package
  | apache2-mod_fcgid                   | Alternative FastCGI module for Apache2                     | package
  | apache2-mod_jk                      | Connectors between Apache and Tomcat Servlet Container     | package
  | apache2-mod_jk                      | Connectors between Apache and Tomcat Servlet Container     | srcpackage
  | apache2-mod_maxminddb               | MaxMind DB Apache Module                                   | package
  | apache2-mod_mono                    | Run ASP.NET Pages on Unix with Apache and Mono             | package
  | apache2-mod_nss                     | SSL/TLS module for the Apache HTTP server                  | package
  | apache2-mod_perl                    | Embedded Perl for Apache                                   | package
  | apache2-mod_perl-devel              | Embedded Perl for Apache - Development package             | package
  | apache2-mod_php7                    | PHP7 module for the Apache 2.x webserver                   | package
  | apache2-mod_php7                    | PHP7 module for the Apache 2.x webserver                   | srcpackage
  | apache2-mod_php8                    | PHP module for the Apache 2.x webserver                    | package
  | apache2-mod_php8                    | PHP module for the Apache 2.x webserver                    | srcpackage
  | apache2-mod_security2               | Web Application Firewall for apache httpd                  | package
  | apache2-mod_security2               | Web Application Firewall for apache httpd                  | srcpackage
  | apache2-mod_uwsgi                   | uWSGI Module for Apache 2.0                                | package
  | apache2-mod_wsgi                    | A WSGI interface for Python3 web applications in Apache    | package
  | apache2-mod_wsgi-python3            | A WSGI interface for Python3 web applications in Apache    | package
  | apache2-prefork                     | The Apache HTTPD Server                                    | package
  | apache2-prefork                     | The Apache HTTPD Server                                    | srcpackage
  | apache2-utils                       | The Apache HTTPD Server                                    | package
  | apache2-utils                       | The Apache HTTPD Server                                    | srcpackage
  | apache2-worker                      | The Apache HTTPD Server                                    | package
  | apache2-worker                      | The Apache HTTPD Server                                    | srcpackage
  | dehydrated-apache2                  | Apache Integration for dehydrated                          | package
  | owasp-modsecurity-crs-apache2       | OWASP ModSecurity Common Rule Set (CRS)                    | package
  | perl-Apache2-AuthCookieDBI          | An AuthCookie module backed by a DBI database              | package
  | rubygem-passenger-apache2           | Passenger apache module                                    | package

opensuse15:~ # zypper info apache2
Loading repository data...
Reading installed packages...


Information for package apache2:
--------------------------------
Repository     : Update repository with updates from SUSE Linux Enterprise 15
Name           : apache2
Version        : 2.4.58-150600.5.35.1
Arch           : x86_64
Vendor         : SUSE LLC <https://www.suse.com/>
Installed Size : 1.4 MiB
Installed      : No
Status         : not installed
Source package : apache2-2.4.58-150600.5.35.1.src
Upstream URL   : https://httpd.apache.org/
Summary        : The Apache HTTPD Server
Description    : 
    The Apache HTTP Server Project is an effort to develop and
    maintain an open-source HTTP server for modern operating
    systems including UNIX and Windows. The goal of this project
    is to provide a secure, efficient and extensible server that
    provides HTTP services in sync with the current HTTP standards.

opensuse15:~ # zypper install -y apache2

opensuse15:~ # systemctl daemon-reload && systemctl enable --now apache2

opensuse15:~ # ss -ntl |grep 80
LISTEN 0      4096               *:80              *:*          

opensuse15:~ # httpd -v
Server version: Apache/2.4.58 (Linux/SUSE)
Server built:   2025-07-21 02:11:38.000000000 +0000

2.3 源码编译安装

2.3.1 下载并解压缩源码包

httpd源码包下载,去“https://httpd.apache.org”网站下载,选择“Download”,如图2所示。

图2 下载Apache源码包

选择“Source: httpd-2.4.65.tar.gz“,如图3所示。

图3 下载Apache源码包

cd /usr/local/src/

# Rocky、Almalinux、CentOS、AnolisOS、OpenCloudOS、Kylin Server默认没有wget包,需要安装
yum install -y wget

# openEuler 22.03/24.03 LTS、AnolisOS 23、OpenCloudOS 9没有安装tar包,需要安装
yum install -y tar

wget https://dlcdn.apache.org/httpd/httpd-2.4.65.tar.gz

tar xf httpd-2.4.65.tar.gz

2.3.2 源码编译安装

编译安装参数解释:

./configure # 准备httpd的编译环境
--prefix # 指定httpd安装目录
--enable-so # 启用 DSO(Dynamic Shared Objects)支持 ,即:开启动态模块加载功能。
--enable-ssl # 启用 Apache 的 SSL 模块( mod_ssl ),使其支持 HTTPS。
--enable-cgi # 该参数会启用 Apache 对 CGI 脚本 的支持,使得服务器可以运行如  .cgi 、 .pl 、 .py  等脚本文件,从而生成动态网页内容。
--enable-rewrite # 启用 mod_rewrite 模块,支持 URL 重写
--with-zlib # 启用 zlib 支持,用于压缩传输内容
--with-pcre # 用于指定 PCRE(Perl Compatible Regular Expressions)库的安装路径。PCRE 是 Apache 用于支持正则表达式的核心依赖,尤其是  mod_rewrite  等模块。
--with-included-apr # 使用 httpd 自带的 APR 和 APR-Util,适合避免系统依赖问题 

--enable-modules # 控制哪些模块被静态编译进主程序(即不能动态加载)。
# all :启用所有模块(静态方式),体积大,灵活性差。
# most :启用大多数常用模块(静态方式),是折中选择。

--enable-mpms-shared # 用于控制 MPM(Multi-Processing Module,多处理模块)是否以动态共享模块(DSO)方式构建。 
# --enable-mpms-shared=all,将所有平台支持的 MPM 模块都编译为共享模块,即  event 、 worker 、prefork  等都会被编译成  .so  文件,供运行时动态加载。
# --enable-mpms-shared=event worker,仅将指定的 MPM 编译为共享模块,多个模块用空格分隔。 
# 默认行为(不加此参数) ,MPM 会被静态编译进主程序,无法通过配置文件切换,必须重新编译才能更换 MPM。

--with-mpm # 用于指定默认使用的 MPM(Multi-Processing Module,多路处理模块)。这个参数决定了 Apache 的并发处理模型,对性能和资源使用有直接影响。
# prefork,多进程模型,每个进程处理一个请求,稳定性高,兼容性最好;适合低并发、需要兼容非线程安全模块(如 mod_php) 
# worker ,多进程 + 多线程混合模型,资源占用更少,并发能力更强;适合中高并发,线程安全模块
# event,类似 worker,但对 Keep-alive 连接做了优化,性能更好 ;高并发、静态资源为主,Apache 2.4+ 推荐 

2.3.2.1 Rocky 9/10、AlmaLinux 9/10、CentOS 7、CentOS Stream 9/10、openEuler 24.03 LTS、AnolisOS 23、OpenCloudOS 9、Kylin Server V11

进入httpd包解压的目录:

cd httpd-2.4.65

执行configure:

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event
...
configure: error: Bundled APR requested but not found at ./srclib/. Download and unpack the corresponding apr and apr-util packages to ./srclib/. # 提示,配置:错误:请求了捆绑的APR,但在./srclib/中未找到。请下载并解压相应的apr和apr-util包到./srclib/。

下载apr和apr-util包,解压并移动到相应目录:

cd ..

wget https://mirrors.cloud.tencent.com/apache/apr/apr-1.7.6.tar.gz
wget https://mirrors.cloud.tencent.com/apache/apr/apr-util-1.6.3.tar.gz

tar xf apr-1.7.6.tar.gz 
tar xf apr-util-1.6.3.tar.gz

mv apr-1.7.6 httpd-2.4.65/srclib/apr
mv apr-util-1.6.3 httpd-2.4.65/srclib/apr-util

进入httpd包解压的目录:

cd httpd-2.4.65

继续执行configure:

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event
...
checking for gcc... no # 提示,没有gcc
checking for cc... no
checking for cl.exe... no
checking for clang... no
configure: error: in '/usr/local/src/httpd-2.4.65/srclib/apr':
configure: error: no acceptable C compiler found in $PATH
See 'config.log' for more details
configure failed for srclib/apr

安装gcc包:

yum install -y gcc

继续执行configure:

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event
...
configure: error: pcre(2)-config for libpcre not found. PCRE is required and available from http://pcre.org/
 # 提示,没有pcre

安装pcre2-devel包:

yum install -y  pcre2-devel

继续执行configure:

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event
...
checking whether to enable mod_ssl... configure: error: mod_ssl has been requested but can not be built due to prerequisite failures # 提示,没有mod_ssl

安装openssl-devel包:

yum install -y openssl-devel

继续执行configure:

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event
...
config.status: creating test/pyhttpd/config.ini
config.status: creating docs/conf/httpd.conf
config.status: creating docs/conf/extra/httpd-autoindex.conf
config.status: creating docs/conf/extra/httpd-dav.conf
config.status: creating docs/conf/extra/httpd-default.conf
config.status: creating docs/conf/extra/httpd-info.conf
config.status: creating docs/conf/extra/httpd-languages.conf
config.status: creating docs/conf/extra/httpd-manual.conf
config.status: creating docs/conf/extra/httpd-mpm.conf
config.status: creating docs/conf/extra/httpd-multilang-errordoc.conf
config.status: creating docs/conf/extra/httpd-ssl.conf
config.status: creating docs/conf/extra/httpd-userdir.conf
config.status: creating docs/conf/extra/httpd-vhosts.conf
config.status: creating docs/conf/extra/proxy-html.conf
config.status: creating include/ap_config_layout.h
config.status: creating support/apxs
config.status: creating support/apachectl
config.status: creating support/dbmmanage
config.status: creating support/envvars-std
config.status: creating support/log_server_status
config.status: creating support/logresolve.pl
config.status: creating support/phf_abuse_log.cgi
config.status: creating support/split-logfile
config.status: creating build/rules.mk
config.status: creating build/pkg/pkginfo
config.status: creating build/config_vars.sh
config.status: creating include/ap_config_auto.h
config.status: executing default commands
configure: summary of build options:

    Server Version: 2.4.65
    Install prefix: /apps/httpd
    C compiler:     gcc
    CFLAGS:          -g -O2  
    CPPFLAGS:        -DLINUX -D_REENTRANT -D_GNU_SOURCE  
    LDFLAGS:           
    LIBS:             
    C preprocessor: gcc -E

执行make:

make -j $(nproc) && make install
...
httpd-2.4.65/srclib/apr-util/include/private  -I/usr/local/src/httpd-2.4.65/srclib/apr/include    -o xml/apr_xml.lo -c xml/apr_xml.c && touch xml/apr_xml.lo
xml/apr_xml.c:35:10: fatal error: expat.h: No such file or directory # 提示,缺少expat.h文件
   35 | #include <expat.h>
      |          ^~~~~~~~~
compilation terminated.
make[3]: *** [/usr/local/src/httpd-2.4.65/srclib/apr-util/build/rules.mk:207: xml/apr_xml.lo] Error 1
make[3]: *** Waiting for unfinished jobs....
make[3]: Leaving directory '/usr/local/src/httpd-2.4.65/srclib/apr-util'
make[2]: *** [/usr/local/src/httpd-2.4.65/srclib/apr-util/build/rules.mk:119: all-recursive] Error 1
make[2]: Leaving directory '/usr/local/src/httpd-2.4.65/srclib/apr-util'
make[1]: *** [/usr/local/src/httpd-2.4.65/build/rules.mk:75: all-recursive] Error 1
make[1]: Leaving directory '/usr/local/src/httpd-2.4.65/srclib'
make: *** [/usr/local/src/httpd-2.4.65/build/rules.mk:75: all-recursive] Error 1

安装expat-devel包:

yum install -y expat-devel

重新执行configure:

# 再次执行make之前先清理之前的操作
make clean

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event

重新执行make:

make -j $(nproc) && make install

总结编译过程:

# 下载apr和apr-util包,解压并移动到相应目录
wget https://mirrors.cloud.tencent.com/apache/apr/apr-1.7.6.tar.gz
wget https://mirrors.cloud.tencent.com/apache/apr/apr-util-1.6.3.tar.gz

tar xf apr-1.7.6.tar.gz 
tar xf apr-util-1.6.3.tar.gz

mv apr-1.7.6 httpd-2.4.65/srclib/apr
mv apr-util-1.6.3 httpd-2.4.65/srclib/apr-util

# 安装依赖包
yum install -y gcc pcre2-devel openssl-devel expat-devel

# 进入httpd包解压的目录
cd httpd-2.4.65

# 执行configure
./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event

# 执行make
make -j $(nproc) && make install

2.3.2.2 Rocky 8、AlmaLinux 8、CentOS Stream 8、openEuler 22.03 LTS、AnolisOS 8、OpenCloudOS 8、Kylin Server V10

进入httpd包解压的目录:

cd httpd-2.4.65

执行configure:

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event
...
configure: error: Bundled APR requested but not found at ./srclib/. Download and unpack the corresponding apr and apr-util packages to ./srclib/. # 提示,配置:错误:请求了捆绑的APR,但在./srclib/中未找到。请下载并解压相应的apr和apr-util包到./srclib/。

下载apr和apr-util并解压,复制到相应目录:

cd ..

wget https://mirrors.cloud.tencent.com/apache/apr/apr-1.7.6.tar.gz
wget https://mirrors.cloud.tencent.com/apache/apr/apr-util-1.6.3.tar.gz

tar xf apr-1.7.6.tar.gz 
tar xf apr-util-1.6.3.tar.gz

mv apr-1.7.6 httpd-2.4.65/srclib/apr
mv apr-util-1.6.3 httpd-2.4.65/srclib/apr-util

进入httpd包解压的目录:

cd httpd-2.4.65

继续执行configure:

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event
...
checking for gcc... no # 提示,没有gcc
checking for cc... no
checking for cl.exe... no
checking for clang... no
configure: error: in '/usr/local/src/httpd-2.4.65/srclib/apr':
configure: error: no acceptable C compiler found in $PATH
See 'config.log' for more details
configure failed for srclib/apr

安装gcc包:

yum install -y gcc

继续执行configure:

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event
...
configure: error: pcre(2)-config for libpcre not found. PCRE is required and available from http://pcre.org/
 # 提示,没有pcre

安装pcre2-devel包:

yum install -y  pcre2-devel

继续执行configure:

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event
...
checking whether to enable mod_ssl... configure: error: mod_ssl has been requested but can not be built due to prerequisite failures # 提示,没有mod_ssl

安装openssl-devel包:

yum install -y openssl-devel

继续执行configure:

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event
...
config.status: creating test/pyhttpd/config.ini
config.status: creating docs/conf/httpd.conf
config.status: creating docs/conf/extra/httpd-autoindex.conf
config.status: creating docs/conf/extra/httpd-dav.conf
config.status: creating docs/conf/extra/httpd-default.conf
config.status: creating docs/conf/extra/httpd-info.conf
config.status: creating docs/conf/extra/httpd-languages.conf
config.status: creating docs/conf/extra/httpd-manual.conf
config.status: creating docs/conf/extra/httpd-mpm.conf
config.status: creating docs/conf/extra/httpd-multilang-errordoc.conf
config.status: creating docs/conf/extra/httpd-ssl.conf
config.status: creating docs/conf/extra/httpd-userdir.conf
config.status: creating docs/conf/extra/httpd-vhosts.conf
config.status: creating docs/conf/extra/proxy-html.conf
config.status: creating include/ap_config_layout.h
config.status: creating support/apxs
config.status: creating support/apachectl
config.status: creating support/dbmmanage
config.status: creating support/envvars-std
config.status: creating support/log_server_status
config.status: creating support/logresolve.pl
config.status: creating support/phf_abuse_log.cgi
config.status: creating support/split-logfile
config.status: creating build/rules.mk
config.status: creating build/pkg/pkginfo
config.status: creating build/config_vars.sh
config.status: creating include/ap_config_auto.h
config.status: executing default commands
configure: summary of build options:

    Server Version: 2.4.65
    Install prefix: /apps/httpd
    C compiler:     gcc
    CFLAGS:          -g -O2  
    CPPFLAGS:        -DLINUX -D_REENTRANT -D_GNU_SOURCE  
    LDFLAGS:           
    LIBS:             
    C preprocessor: gcc -E

执行make:

yum install -y make

make -j $(nproc) && make install
...
httpd-2.4.65/srclib/apr-util/include/private  -I/usr/local/src/httpd-2.4.65/srclib/apr/include    -o xml/apr_xml.lo -c xml/apr_xml.c && touch xml/apr_xml.lo
xml/apr_xml.c:35:10: fatal error: expat.h: No such file or directory # 提示,缺少expat.h文件
   35 | #include <expat.h>
      |          ^~~~~~~~~
compilation terminated.
make[3]: *** [/usr/local/src/httpd-2.4.65/srclib/apr-util/build/rules.mk:207: xml/apr_xml.lo] Error 1
make[3]: *** Waiting for unfinished jobs....
make[3]: Leaving directory '/usr/local/src/httpd-2.4.65/srclib/apr-util'
make[2]: *** [/usr/local/src/httpd-2.4.65/srclib/apr-util/build/rules.mk:119: all-recursive] Error 1
make[2]: Leaving directory '/usr/local/src/httpd-2.4.65/srclib/apr-util'
make[1]: *** [/usr/local/src/httpd-2.4.65/build/rules.mk:75: all-recursive] Error 1
make[1]: Leaving directory '/usr/local/src/httpd-2.4.65/srclib'
make: *** [/usr/local/src/httpd-2.4.65/build/rules.mk:75: all-recursive] Error 1

安装expat-devel包:

yum install -y expat-devel

重新执行configure:

# 再次执行make之前先清理之前的操作
make clean

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event

重新执行make:

make -j $(nproc) && make install

总结编译过程:

# 下载apr和apr-util包,解压并移动到相应目录
wget https://mirrors.cloud.tencent.com/apache/apr/apr-1.7.6.tar.gz
wget https://mirrors.cloud.tencent.com/apache/apr/apr-util-1.6.3.tar.gz

tar xf apr-1.7.6.tar.gz 
tar xf apr-util-1.6.3.tar.gz

mv apr-1.7.6 httpd-2.4.65/srclib/apr
mv apr-util-1.6.3 httpd-2.4.65/srclib/apr-util

# 安装依赖包
yum install -y gcc pcre2-devel openssl-devel make expat-devel

# 进入httpd包解压的目录
cd httpd-2.4.65

# 执行configure
./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event

# 执行make
make -j $(nproc) && make install

2.3.2.3 UOS Server V20

进入httpd包解压的目录:

cd httpd-2.4.65

执行configure:

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event
...
configure: error: Bundled APR requested but not found at ./srclib/. Download and unpack the corresponding apr and apr-util packages to ./srclib/. # 提示,配置:错误:请求了捆绑的APR,但在./srclib/中未找到。请下载并解压相应的apr和apr-util包到./srclib/。

下载apr和apr-util包,解压并移动到相应目录:

cd ..

wget https://mirrors.cloud.tencent.com/apache/apr/apr-1.7.6.tar.gz
wget https://mirrors.cloud.tencent.com/apache/apr/apr-util-1.6.3.tar.gz

tar xf apr-1.7.6.tar.gz 
tar xf apr-util-1.6.3.tar.gz

mv apr-1.7.6 httpd-2.4.65/srclib/apr
mv apr-util-1.6.3 httpd-2.4.65/srclib/apr-util

进入httpd包解压的目录:

cd httpd-2.4.65

继续执行configure:

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event
...
config.status: creating test/pyhttpd/config.ini
config.status: creating docs/conf/httpd.conf
config.status: creating docs/conf/extra/httpd-autoindex.conf
config.status: creating docs/conf/extra/httpd-dav.conf
config.status: creating docs/conf/extra/httpd-default.conf
config.status: creating docs/conf/extra/httpd-info.conf
config.status: creating docs/conf/extra/httpd-languages.conf
config.status: creating docs/conf/extra/httpd-manual.conf
config.status: creating docs/conf/extra/httpd-mpm.conf
config.status: creating docs/conf/extra/httpd-multilang-errordoc.conf
config.status: creating docs/conf/extra/httpd-ssl.conf
config.status: creating docs/conf/extra/httpd-userdir.conf
config.status: creating docs/conf/extra/httpd-vhosts.conf
config.status: creating docs/conf/extra/proxy-html.conf
config.status: creating include/ap_config_layout.h
config.status: creating support/apxs
config.status: creating support/apachectl
config.status: creating support/dbmmanage
config.status: creating support/envvars-std
config.status: creating support/log_server_status
config.status: creating support/logresolve.pl
config.status: creating support/phf_abuse_log.cgi
config.status: creating support/split-logfile
config.status: creating build/rules.mk
config.status: creating build/pkg/pkginfo
config.status: creating build/config_vars.sh
config.status: creating include/ap_config_auto.h
config.status: executing default commands
configure: summary of build options:

    Server Version: 2.4.65
    Install prefix: /apps/httpd
    C compiler:     gcc
    CFLAGS:          -g -O2 -pthread  
    CPPFLAGS:        -DLINUX -D_REENTRANT -D_GNU_SOURCE  
    LDFLAGS:           
    LIBS:             
    C preprocessor: gcc -E

执行make:

make -j $(nproc) && make install
...
httpd-2.4.65/srclib/apr-util/include/private  -I/usr/local/src/httpd-2.4.65/srclib/apr/include    -o xml/apr_xml.lo -c xml/apr_xml.c && touch xml/apr_xml.lo
xml/apr_xml.c:35:10: fatal error: expat.h: No such file or directory # 提示,缺少expat.h文件
   35 | #include <expat.h>
      |          ^~~~~~~~~
compilation terminated.
make[3]: *** [/usr/local/src/httpd-2.4.65/srclib/apr-util/build/rules.mk:207: xml/apr_xml.lo] Error 1
make[3]: *** Waiting for unfinished jobs....
make[3]: Leaving directory '/usr/local/src/httpd-2.4.65/srclib/apr-util'
make[2]: *** [/usr/local/src/httpd-2.4.65/srclib/apr-util/build/rules.mk:119: all-recursive] Error 1
make[2]: Leaving directory '/usr/local/src/httpd-2.4.65/srclib/apr-util'
make[1]: *** [/usr/local/src/httpd-2.4.65/build/rules.mk:75: all-recursive] Error 1
make[1]: Leaving directory '/usr/local/src/httpd-2.4.65/srclib'
make: *** [/usr/local/src/httpd-2.4.65/build/rules.mk:75: all-recursive] Error 1

安装expat-devel包:

yum install -y expat-devel

重新执行configure:

# 再次执行make之前先清理之前的操作
make clean

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event

重新执行make:

make -j $(nproc) && make install

总结编译过程:

# 下载apr和apr-util包,解压并移动到相应目录
wget https://mirrors.cloud.tencent.com/apache/apr/apr-1.7.6.tar.gz
wget https://mirrors.cloud.tencent.com/apache/apr/apr-util-1.6.3.tar.gz

tar xf apr-1.7.6.tar.gz 
tar xf apr-util-1.6.3.tar.gz

mv apr-1.7.6 httpd-2.4.65/srclib/apr
mv apr-util-1.6.3 httpd-2.4.65/srclib/apr-util

# 安装依赖包
yum install -y expat-devel

# 进入httpd包解压的目录
cd httpd-2.4.65

# 执行configure
./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event

# 执行make
make -j $(nproc) && make install

2.3.2.4 openSUSE Leap 15

进入httpd包解压的目录:

cd httpd-2.4.65

执行configure:

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event
...
configure: error: Bundled APR requested but not found at ./srclib/. Download and unpack the corresponding apr and apr-util packages to ./srclib/. # 提示,配置:错误:请求了捆绑的APR,但在./srclib/中未找到。请下载并解压相应的apr和apr-util包到./srclib/。

下载apr和apr-util包,解压并移动到相应目录:

cd ..

wget https://mirrors.cloud.tencent.com/apache/apr/apr-1.7.6.tar.gz
wget https://mirrors.cloud.tencent.com/apache/apr/apr-util-1.6.3.tar.gz

tar xf apr-1.7.6.tar.gz 
tar xf apr-util-1.6.3.tar.gz

mv apr-1.7.6 httpd-2.4.65/srclib/apr
mv apr-util-1.6.3 httpd-2.4.65/srclib/apr-util

进入httpd包解压的目录:

cd httpd-2.4.65

继续执行configure:

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event
...
checking for gcc... no # 提示,没有gcc
checking for cc... no
checking for cl.exe... no
checking for clang... no
configure: error: in '/usr/local/src/httpd-2.4.65/srclib/apr':
configure: error: no acceptable C compiler found in $PATH
See 'config.log' for more details
configure failed for srclib/apr

安装gcc包:

zypper install -y gcc

继续执行configure:

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event
...
configure: error: pcre(2)-config for libpcre not found. PCRE is required and available from http://pcre.org/
 # 提示,没有pcre

安装pcre2-devel包:

zypper install -y  pcre2-devel

继续执行configure:

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event
...
checking whether to enable mod_ssl... configure: error: mod_ssl has been requested but can not be built due to prerequisite failures # 提示,没有mod_ssl

安装openssl-devel包:

zypper install -y openssl-devel

继续执行configure:

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event
...
config.status: creating test/pyhttpd/config.ini
config.status: creating docs/conf/httpd.conf
config.status: creating docs/conf/extra/httpd-autoindex.conf
config.status: creating docs/conf/extra/httpd-dav.conf
config.status: creating docs/conf/extra/httpd-default.conf
config.status: creating docs/conf/extra/httpd-info.conf
config.status: creating docs/conf/extra/httpd-languages.conf
config.status: creating docs/conf/extra/httpd-manual.conf
config.status: creating docs/conf/extra/httpd-mpm.conf
config.status: creating docs/conf/extra/httpd-multilang-errordoc.conf
config.status: creating docs/conf/extra/httpd-ssl.conf
config.status: creating docs/conf/extra/httpd-userdir.conf
config.status: creating docs/conf/extra/httpd-vhosts.conf
config.status: creating docs/conf/extra/proxy-html.conf
config.status: creating include/ap_config_layout.h
config.status: creating support/apxs
config.status: creating support/apachectl
config.status: creating support/dbmmanage
config.status: creating support/envvars-std
config.status: creating support/log_server_status
config.status: creating support/logresolve.pl
config.status: creating support/phf_abuse_log.cgi
config.status: creating support/split-logfile
config.status: creating build/rules.mk
config.status: creating build/pkg/pkginfo
config.status: creating build/config_vars.sh
config.status: creating include/ap_config_auto.h
config.status: executing default commands
configure: summary of build options:

    Server Version: 2.4.65
    Install prefix: /apps/httpd
    C compiler:     gcc
    CFLAGS:          -g -O2  
    CPPFLAGS:        -DLINUX -D_REENTRANT -D_GNU_SOURCE  
    LDFLAGS:           
    LIBS:             
    C preprocessor: gcc -E

执行make:

zypper install -y make

make -j $(nproc) && make install
...
httpd-2.4.65/srclib/apr-util/include/private  -I/usr/local/src/httpd-2.4.65/srclib/apr/include    -o xml/apr_xml.lo -c xml/apr_xml.c && touch xml/apr_xml.lo
xml/apr_xml.c:35:10: fatal error: expat.h: No such file or directory # 提示,缺少expat.h文件
   35 | #include <expat.h>
      |          ^~~~~~~~~
compilation terminated.
make[3]: *** [/usr/local/src/httpd-2.4.65/srclib/apr-util/build/rules.mk:207: xml/apr_xml.lo] Error 1
make[3]: *** Waiting for unfinished jobs....
make[3]: Leaving directory '/usr/local/src/httpd-2.4.65/srclib/apr-util'
make[2]: *** [/usr/local/src/httpd-2.4.65/srclib/apr-util/build/rules.mk:119: all-recursive] Error 1
make[2]: Leaving directory '/usr/local/src/httpd-2.4.65/srclib/apr-util'
make[1]: *** [/usr/local/src/httpd-2.4.65/build/rules.mk:75: all-recursive] Error 1
make[1]: Leaving directory '/usr/local/src/httpd-2.4.65/srclib'
make: *** [/usr/local/src/httpd-2.4.65/build/rules.mk:75: all-recursive] Error 1

安装libexpat-devel包:

zypper install -y libexpat-devel

重新执行configure:

# 再次执行make之前先清理之前的操作
make clean

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event

重新执行make:

make -j $(nproc) && make install

总结编译过程:

# 下载apr和apr-util包,解压并移动到相应目录
wget https://mirrors.cloud.tencent.com/apache/apr/apr-1.7.6.tar.gz
wget https://mirrors.cloud.tencent.com/apache/apr/apr-util-1.6.3.tar.gz

tar xf apr-1.7.6.tar.gz 
tar xf apr-util-1.6.3.tar.gz

mv apr-1.7.6 httpd-2.4.65/srclib/apr
mv apr-util-1.6.3 httpd-2.4.65/srclib/apr-util

# 安装依赖包
zypper install -y gcc pcre2-devel openssl-devel make libexpat-devel

# 进入httpd包解压的目录
cd httpd-2.4.65

# 执行configure
./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event

# 执行make
make -j $(nproc) && make install

2.3.2.5 Ubuntu Server 18.04/20.04/22.04/24.04 LTS、Debian 11/12/13

进入httpd包解压的目录:

cd httpd-2.4.65

执行configure:

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event
...
configure: error: Bundled APR requested but not found at ./srclib/. Download and unpack the corresponding apr and apr-util packages to ./srclib/. # 提示,配置:错误:请求了捆绑的APR,但在./srclib/中未找到。请下载并解压相应的apr和apr-util包到./srclib/。

下载apr和apr-util包,解压并移动到相应目录:

cd ..

wget https://mirrors.cloud.tencent.com/apache/apr/apr-1.7.6.tar.gz
wget https://mirrors.cloud.tencent.com/apache/apr/apr-util-1.6.3.tar.gz

tar xf apr-1.7.6.tar.gz 
tar xf apr-util-1.6.3.tar.gz

mv apr-1.7.6 httpd-2.4.65/srclib/apr
mv apr-util-1.6.3 httpd-2.4.65/srclib/apr-util

进入httpd包解压的目录:

cd httpd-2.4.65

继续执行configure:

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event
...
checking for gcc... no # 提示,没有gcc
checking for cc... no
checking for cl.exe... no
checking for clang... no
configure: error: in '/usr/local/src/httpd-2.4.65/srclib/apr':
configure: error: no acceptable C compiler found in $PATH
See 'config.log' for more details
configure failed for srclib/apr

安装gcc包:

apt install -y gcc

继续执行configure:

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event
...
configure: error: pcre(2)-config for libpcre not found. PCRE is required and available from http://pcre.org/
 # 提示,没有pcre

安装libpcre2-dev包:

apt install -y libpcre2-dev

继续执行configure:

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event
...
checking whether to enable mod_ssl... configure: error: mod_ssl has been requested but can not be built due to prerequisite failures # 提示,没有mod_ssl

安装libssl-dev包:

apt install -y libssl-dev

继续执行configure:

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event
...
config.status: creating test/pyhttpd/config.ini
config.status: creating docs/conf/httpd.conf
config.status: creating docs/conf/extra/httpd-autoindex.conf
config.status: creating docs/conf/extra/httpd-dav.conf
config.status: creating docs/conf/extra/httpd-default.conf
config.status: creating docs/conf/extra/httpd-info.conf
config.status: creating docs/conf/extra/httpd-languages.conf
config.status: creating docs/conf/extra/httpd-manual.conf
config.status: creating docs/conf/extra/httpd-mpm.conf
config.status: creating docs/conf/extra/httpd-multilang-errordoc.conf
config.status: creating docs/conf/extra/httpd-ssl.conf
config.status: creating docs/conf/extra/httpd-userdir.conf
config.status: creating docs/conf/extra/httpd-vhosts.conf
config.status: creating docs/conf/extra/proxy-html.conf
config.status: creating include/ap_config_layout.h
config.status: creating support/apxs
config.status: creating support/apachectl
config.status: creating support/dbmmanage
config.status: creating support/envvars-std
config.status: creating support/log_server_status
config.status: creating support/logresolve.pl
config.status: creating support/phf_abuse_log.cgi
config.status: creating support/split-logfile
config.status: creating build/rules.mk
config.status: creating build/pkg/pkginfo
config.status: creating build/config_vars.sh
config.status: creating include/ap_config_auto.h
config.status: executing default commands
configure: summary of build options:

    Server Version: 2.4.65
    Install prefix: /apps/httpd
    C compiler:     gcc
    CFLAGS:          -g -O2  
    CPPFLAGS:        -DLINUX -D_REENTRANT -D_GNU_SOURCE  
    LDFLAGS:           
    LIBS:             
    C preprocessor: gcc -E

执行make:

apt install -y make

make -j $(nproc) && make install
...
httpd-2.4.65/srclib/apr-util/include/private  -I/usr/local/src/httpd-2.4.65/srclib/apr/include    -o xml/apr_xml.lo -c xml/apr_xml.c && touch xml/apr_xml.lo
xml/apr_xml.c:35:10: fatal error: expat.h: No such file or directory # 提示,缺少expat.h文件
   35 | #include <expat.h>
      |          ^~~~~~~~~
compilation terminated.
make[3]: *** [/usr/local/src/httpd-2.4.65/srclib/apr-util/build/rules.mk:207: xml/apr_xml.lo] Error 1
make[3]: *** Waiting for unfinished jobs....
make[3]: Leaving directory '/usr/local/src/httpd-2.4.65/srclib/apr-util'
make[2]: *** [/usr/local/src/httpd-2.4.65/srclib/apr-util/build/rules.mk:119: all-recursive] Error 1
make[2]: Leaving directory '/usr/local/src/httpd-2.4.65/srclib/apr-util'
make[1]: *** [/usr/local/src/httpd-2.4.65/build/rules.mk:75: all-recursive] Error 1
make[1]: Leaving directory '/usr/local/src/httpd-2.4.65/srclib'
make: *** [/usr/local/src/httpd-2.4.65/build/rules.mk:75: all-recursive] Error 1

安装libexpat1-dev包:

apt install -y libexpat1-dev

重新执行configure:

# 再次执行make之前先清理之前的操作
make clean

./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event

重新执行make:

make -j $(nproc) && make install

总结编译过程:

# 下载apr和apr-util包,解压并移动到相应目录
wget https://mirrors.cloud.tencent.com/apache/apr/apr-1.7.6.tar.gz
wget https://mirrors.cloud.tencent.com/apache/apr/apr-util-1.6.3.tar.gz

tar xf apr-1.7.6.tar.gz 
tar xf apr-util-1.6.3.tar.gz

mv apr-1.7.6 httpd-2.4.65/srclib/apr
mv apr-util-1.6.3 httpd-2.4.65/srclib/apr-util

# 安装依赖包
apt install -y gcc libpcre2-dev libssl-dev make libexpat1-dev

# 进入httpd包解压的目录
cd httpd-2.4.65

# 执行configure
./configure --prefix=/apps/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event

# 执行make
make -j $(nproc) && make install

2.3.3 创建用户和组

# Rocky、Almalinux、CentOS、openEuler、AnolisOS、OpenCloudOS、 Kylin Server、UOS Server、Ubuntu、Debian
useradd -s /sbin/nologin -r apache

# openSUSE
groupadd -r apache
useradd -s /sbin/nologin -r -g apache apache

2.3.4 指定运行httpd的用户

sed -i 's/daemon/apache/' /apps/httpd/conf/httpd.conf

2.3.5 设置环境变量

echo "PATH=/apps/httpd/bin:$PATH" > /etc/profile.d/httpd.sh
. /etc/profile.d/httpd.sh

2.3.6 准备启动脚本并启动服务

# Rocky、Almalinux、CentOS、openEuler、AnolisOS、OpenCloudOS、 Kylin Server、UOS Server、openSUSE
cat > /usr/lib/systemd/system/httpd.service <<-EOF
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=forking
ExecStart=/apps/httpd/bin/apachectl start
ExecReload=/apps/httpd/bin/apachectl graceful
ExecStop=/apps/httpd/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

# Ubuntu和Debian
cat > /lib/systemd/system/httpd.service <<-EOF
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=forking
ExecStart=/apps/httpd/bin/apachectl start
ExecReload=/apps/httpd/bin/apachectl graceful
ExecStop=/apps/httpd/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload && systemctl enable --now httpd

2.3.7 验证Httpd

[root@rocky10 httpd-2.4.65]# ss -ntl |grep 80
LISTEN 0      511                *:80              *:* 

[root@rocky10 httpd-2.4.65]# httpd -v
Server version: Apache/2.4.65 (Unix)
Server built:   Sep 23 2025 17:37:36

2.3.8 一键安装Httpd源码编译的脚本

Shell脚本源码地址:

Gitee:https://gitee.com/raymond9/shell

Github:https://github.com/raymond999999/shell

可以去上面的Gitee或Github代码仓库拉取脚本。

[root@rocky10 ~]# cat install_httpd_source.sh
#!/bin/bash
#
#**********************************************************************************
#Author:        Raymond
#QQ:            88563128
#MP:            Raymond运维
#Date:          2025-09-24
#FileName:      install_httpd_source.sh
#URL:           https://wx.zsxq.com/group/15555885545422
#Description:   The mysql source script install supports 
#               “Rocky Linux 8, 9 and 10, AlmaLinux 8, 9 and 10, CentOS 7, 
#               CentOS Stream 8, 9 and 10, openEuler 22.03 and 24.03 LTS, 
#               AnolisOS 8 and 23, OpenCloudOS 8 and 9, Kylin Server v10 and v11, 
#               UOS Server v20, Ubuntu Server 18.04, 20.04, 22.04 and 24.04 LTS,  
#               Debian 11 , 12 and 13, openSUSE Leap 15“ operating systems.
#Copyright (C): 2025 All rights reserved
#**********************************************************************************
COLOR="echo -e \\033[01;31m"
END='\033[0m'

os(){
    . /etc/os-release
    MAIN_NAME=`sed -rn '/^NAME=/s@.*="([[:alpha:]]+).*"$@\1@p' /etc/os-release`
    if [ ${MAIN_NAME} == "Kylin" ];then
        MAIN_VERSION_ID=`sed -rn '/^VERSION_ID=/s@.*="([[:alpha:]]+)(.*)"$@\2@p' /etc/os-release`
    else
        MAIN_VERSION_ID=`sed -rn '/^VERSION_ID=/s@.*="?([0-9]+)\.?.*"?@\1@p' /etc/os-release`
    fi
    if [ ${MAIN_NAME} == "Ubuntu" -o ${MAIN_NAME} == "Debian" ];then
        FULL_NAME="${PRETTY_NAME}"
    elif [ ${MAIN_NAME} == "UOS" ];then
        FULL_NAME="${NAME}"
    else
        FULL_NAME="${NAME} ${VERSION_ID}"
    fi
}

os
SRC_DIR=/usr/local/src
INSTALL_DIR=/apps/httpd
APR_URL=https://mirrors.cloud.tencent.com/apache/apr/
APR_FILE=apr-1.7.6.tar.gz
APR_UTIL_URL=https://mirrors.cloud.tencent.com/apache/apr/
APR_UTIL_FILE=apr-util-1.6.3.tar.gz
HTTPD_URL=https://mirrors.cloud.tencent.com/apache/httpd/
HTTPD_FILE=httpd-2.4.65.tar.gz
MPM=event

check_file(){
    cd  ${SRC_DIR}
    if [ ${MAIN_NAME} == "Rocky" -o ${MAIN_NAME} == "AlmaLinux" -o ${MAIN_NAME} == "CentOS" -o ${MAIN_NAME} == "Anolis" -o ${MAIN_NAME} == "OpenCloudOS" -o ${MAIN_NAME} == "Kylin" ];then
        rpm -q wget &> /dev/null || { ${COLOR}"安装wget工具,请稍等......"${END};yum -y install wget &> /dev/null; }
    fi
    if [ ! -e ${APR_FILE} ];then
        ${COLOR}"缺少${APR_FILE}文件!"${END}
        ${COLOR}"开始下载${APR_FILE}源码包......"${END}
        wget ${APR_URL}${APR_FILE} || { ${COLOR}"下载${APR_FILE}源码包下载失败!"${END}; exit; }
    else
        ${COLOR}"${APR_FILE}文件已准备好!"${END}
    fi
    if [ ! -e ${APR_UTIL_FILE} ];then
        ${COLOR}"缺少${APR_UTIL_FILE}文件!"${END}
        ${COLOR}"开始下载${APR_UTIL_FILE}源码包......"${END}
        wget ${APR_UTIL_URL}${APR_UTIL_FILE} || { ${COLOR}"下载${APR_UTIL_FILE}源码包下载失败!"${END}; exit; }
    else
        ${COLOR}"${APR_UTIL_FILE}文件已准备好!"${END}
    fi
    if [ ! -e ${HTTPD_FILE} ];then
        ${COLOR}"缺少${HTTPD_FILE}文件!"${END}
        ${COLOR}"开始下载${HTTPD_FILE}源码包......"${END} 
        wget ${HTTPD_URL}${HTTPD_FILE} || { ${COLOR}"下载${HTTPD_FILE}源码包下载失败!"${END}; exit; }
    else
        ${COLOR}"${HTTPD_FILE}文件已准备好!"${END}
    fi
}

install_httpd(){
    [ -d ${INSTALL_DIR} ] && { ${COLOR}"Httpd已存在,安装失败!"${END};exit; }
    ${COLOR}"开始安装Httpd......"${END}
    ${COLOR}"开始安装Httpd依赖包,请稍等......"${END}
    if [ ${MAIN_NAME} == "openSUSE" ];then
        zypper install -y gcc pcre2-devel openssl-devel make libexpat-devel &> /dev/null
    elif [ ${MAIN_NAME} == "Ubuntu" -o ${MAIN_NAME} == "Debian" ];then
        apt update &> /dev/null;apt install -y gcc libpcre2-dev libssl-dev make libexpat1-dev
    else
        yum install -y gcc pcre2-devel openssl-devel make expat-devel &> /dev/null
    fi
    if [ ${MAIN_NAME} == "openEuler" ];then
        if [ ${MAIN_VERSION_ID} == 22 -o ${MAIN_VERSION_ID} == 24 ];then
            yum install -y tar &> /dev/null
        fi
    fi
    if [ ${MAIN_NAME} == "Anolis" ];then
        if [ ${MAIN_VERSION_ID} == 23 ];then
            yum install -y tar &> /dev/null
        fi
    fi
    if [ ${MAIN_NAME} == "OpenCloudOS" ];then
        if [ ${MAIN_VERSION_ID} == 9 ];then
            yum install -y tar &> /dev/null
        fi
    fi
    tar xf ${APR_FILE} && tar xf ${APR_UTIL_FILE} && tar xf ${HTTPD_FILE}
    APR_FILE_DIR=`echo ${APR_FILE}_| sed -nr 's/^(.*[0-9]).*/\1/p'`
    APR_UTIL_FILE_DIR=`echo ${APR_UTIL_FILE}_| sed -nr 's/^(.*[0-9]).*/\1/p'`
    HTTPD_FILE_DIR=`echo ${HTTPD_FILE}_| sed -nr 's/^(.*[0-9]).*/\1/p'`
    mv ${APR_FILE_DIR} ${HTTPD_FILE_DIR}/srclib/apr
    mv ${APR_UTIL_FILE_DIR} ${HTTPD_FILE_DIR}/srclib/apr-util
    cd ${HTTPD_FILE_DIR}
    ./configure --prefix=${INSTALL_DIR} --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=${MPM}
    make -j $(nproc) && make install
    [ $? -eq 0 ] && $COLOR"Httpd编译安装成功!"$END ||  { $COLOR"Httpd编译安装失败,退出!"$END;exit; }
    if [ ${MAIN_NAME} == "openSUSE" ];then
        id apache &> /dev/null || { groupadd -r apache && useradd -s /sbin/nologin -r -g apache apache; ${COLOR}"成功创建apache用户!"${END}; }
    else
        id apache &> /dev/null || { useradd -s /sbin/nologin -r apache ; ${COLOR}"成功创建apache用户!"${END}; }
    fi
    sed -i 's/daemon/apache/' ${INSTALL_DIR}/conf/httpd.conf
    echo "PATH=${INSTALL_DIR}/bin:$PATH" > /etc/profile.d/httpd.sh
    . /etc/profile.d/httpd.sh
    if [ ${MAIN_NAME} == "Ubuntu" -o ${MAIN_NAME} == "Debian" ];then
        cat > /lib/systemd/system/httpd.service <<-EOF
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=forking
ExecStart=${INSTALL_DIR}/bin/apachectl start
ExecReload=${INSTALL_DIR}/bin/apachectl graceful
ExecStop=${INSTALL_DIR}/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
    else
        cat > /usr/lib/systemd/system/httpd.service <<-EOF
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=forking
ExecStart=${INSTALL_DIR}/bin/apachectl start
ExecReload=${INSTALL_DIR}/bin/apachectl graceful
ExecStop=${INSTALL_DIR}/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
    fi
    systemctl daemon-reload && systemctl enable --now httpd &> /dev/null
    systemctl is-active httpd &> /dev/null ||  { ${COLOR}"Httpd启动失败,退出!"${END} ; exit; }
    ${COLOR}"${FULL_NAME}操作系统,Httpd安装完成!"${END}
}

main(){
    check_file
    install_httpd
}

if [ ${MAIN_NAME} == "Rocky" ];then
    if [ ${MAIN_VERSION_ID} == 8 -o ${MAIN_VERSION_ID} == 9 -o ${MAIN_VERSION_ID} == 10 ];then
        main
    fi
elif [ ${MAIN_NAME} == "AlmaLinux" ];then
    if [ ${MAIN_VERSION_ID} == 8 -o ${MAIN_VERSION_ID} == 9 -o ${MAIN_VERSION_ID} == 10 ];then
        main
    fi
elif [ ${MAIN_NAME} == "CentOS" ];then
    if [ ${MAIN_VERSION_ID} == 7 -o ${MAIN_VERSION_ID} == 8 -o ${MAIN_VERSION_ID} == 9 -o ${MAIN_VERSION_ID} == 10 ];then
        main
    fi
elif [ ${MAIN_NAME} == "openEuler" ];then
    if [ ${MAIN_VERSION_ID} == 22 -o ${MAIN_VERSION_ID} == 24 ];then
        main
    fi
elif [ ${MAIN_NAME} == "Anolis" ];then
    if [ ${MAIN_VERSION_ID} == 8 -o ${MAIN_VERSION_ID} == 23 ];then
        main
    fi
elif [ ${MAIN_NAME} == 'OpenCloudOS' ];then
    if [ ${MAIN_VERSION_ID} == 8 -o ${MAIN_VERSION_ID} == 9 ];then
        main
    fi
elif [ ${MAIN_NAME} == "Kylin" ];then
    if [ ${MAIN_VERSION_ID} == 10 -o ${MAIN_VERSION_ID} == 11 ];then
        main
    fi
elif [ ${MAIN_NAME} == "UOS" ];then
    if [ ${MAIN_VERSION_ID} == 20 ];then
        main
    fi
elif [ ${MAIN_NAME} == "openSUSE" ];then
    if [ ${MAIN_VERSION_ID} == 15 ];then
        main
    fi
elif [ ${MAIN_NAME} == "Ubuntu" ];then
    if [ ${MAIN_VERSION_ID} == 18 -o ${MAIN_VERSION_ID} == 20 -o ${MAIN_VERSION_ID} == 22 -o ${MAIN_VERSION_ID} == 24 ];then
        main
    fi
elif [ ${MAIN_NAME} == 'Debian' ];then
    if [ ${MAIN_VERSION_ID} == 11 -o ${MAIN_VERSION_ID} == 12 -o ${MAIN_VERSION_ID} == 13 ];then
        main
    fi
else
    ${COLOR}"此脚本不支持${FULL_NAME}操作系统!"${END}
fi
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。