centos9编译安装优化httpd-2.2.27

httpd-2.2 已经 EOL(官方停止维护),**CentOS 9 默认只支持 httpd 2.4+**,所以编译安装需要手动处理依赖。

  • 如果必须用 2.2.27(兼容老系统/业务),建议隔离环境(如 docker / podman)运行。
  • 给出 编译安装步骤 + 优化配置 + 一键脚本

1.依赖准备

CentOS 9 没有部分旧库,需要安装兼容库:

1
2
3
4
5
yum groupinstall -y "Development Tools"
yum install -y gcc gcc-c++ make wget tar \
zlib zlib-devel pcre pcre-devel \
apr apr-devel apr-util apr-util-devel \
expat expat-devel

如果系统仓库缺少 apr / apr-util,可从 Apache 官方档案下载编译:

1
2
3
4
5
6
7
wget https://archive.apache.org/dist/apr/apr-1.7.0.tar.gz
wget https://archive.apache.org/dist/apr/apr-util-1.6.1.tar.gz
tar -zxf apr-1.7.0.tar.gz
cd apr-1.7.0 && ./configure --prefix=/usr/local/apr && make && make install
cd ..
tar -zxf apr-util-1.6.1.tar.gz
cd apr-util-1.6.1 && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install

2.httpd-2.2.27编译安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
wget https://archive.apache.org/dist/httpd/httpd-2.2.27.tar.gz
tar -zxf httpd-2.2.27.tar.gz
cd httpd-2.2.27

./configure \
--prefix=/usr/local/apache2 \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util \
--enable-so \
--enable-rewrite \
--enable-deflate \
--enable-expires \
--enable-headers \
--enable-mods-shared=all

make && make install

3.优化配置

编辑 /usr/local/apache2/conf/httpd.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 基础优化
ServerTokens Prod
ServerSignature Off
KeepAlive On
KeepAliveTimeout 3
MaxKeepAliveRequests 100

# MPM Prefork 优化(根据内存调整)
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 200
MaxRequestsPerChild 3000
</IfModule>

# 压缩与缓存
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain text/html text/xml text/css application/javascript application/json
</IfModule>

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access plus 7 days"
ExpiresByType application/javascript "access plus 7 days"
ExpiresByType image/jpeg "access plus 30 days"
ExpiresByType image/png "access plus 30 days"
</IfModule>

启动服务:

1
/usr/local/apache2/bin/apachectl start

4.一键安装优化脚本

保存为 install_httpd_2.2.27.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
# httpd-2.2.27 一键编译安装优化脚本 for CentOS 9
# Author: 🔥焰

set -e

PREFIX=/usr/local/apache2
APR=/usr/local/apr
APR_UTIL=/usr/local/apr-util
HTTPD_VER=2.2.27

echo "[1/5] 安装依赖..."
yum groupinstall -y "Development Tools"
yum install -y gcc gcc-c++ make wget tar zlib zlib-devel pcre pcre-devel expat expat-devel lynx

echo "[2/5] 安装 apr 和 apr-util..."
cd /usr/local/src
wget -q https://archive.apache.org/dist/apr/apr-1.7.0.tar.gz
wget -q https://archive.apache.org/dist/apr/apr-util-1.6.1.tar.gz
tar -zxf apr-1.7.0.tar.gz && cd apr-1.7.0
./configure --prefix=$APR && make -j$(nproc) && make install
cd ..
tar -zxf apr-util-1.6.1.tar.gz && cd apr-util-1.6.1
./configure --prefix=$APR_UTIL --with-apr=$APR && make -j$(nproc) && make install
cd ..

echo "[3/5] 下载并编译 httpd-$HTTPD_VER..."
wget -q https://archive.apache.org/dist/httpd/httpd-$HTTPD_VER.tar.gz
tar -zxf httpd-$HTTPD_VER.tar.gz && cd httpd-$HTTPD_VER
./configure \
--prefix=$PREFIX \
--with-apr=$APR \
--with-apr-util=$APR_UTIL \
--enable-so \
--enable-rewrite \
--enable-deflate \
--enable-expires \
--enable-headers \
--enable-mods-shared=all
make -j$(nproc) && make install

echo "[4/5] 优化配置..."
CONF=$PREFIX/conf/httpd.conf
cp $CONF ${CONF}.bak

cat >> $CONF <<EOF

# --------- 优化配置 ---------
ServerTokens Prod
ServerSignature Off
KeepAlive On
KeepAliveTimeout 3
MaxKeepAliveRequests 100

<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 200
MaxRequestsPerChild 3000
</IfModule>

<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain text/html text/xml text/css application/javascript application/json
</IfModule>

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access plus 7 days"
ExpiresByType application/javascript "access plus 7 days"
ExpiresByType image/jpeg "access plus 30 days"
ExpiresByType image/png "access plus 30 days"
</IfModule>
EOF

echo "[5/5] 启动 httpd..."
$PREFIX/bin/apachectl start

echo "✅ httpd-$HTTPD_VER 安装完成!目录:$PREFIX"

用法:

1
2
chmod +x install_httpd_2.2.27.sh
./install_httpd_2.2.27.sh

5.优化httpd.conf配置

/usr/local/apache2/conf/httpd.conf
...
ServerName :80
...