在 CentOS 上编译安装 Apache HTTP Server(httpd),一般步骤如下(以 CentOS 7 / 8 为例,适用于大多数情况)。
yum groupinstall -y "Development Tools"
yum install -y gcc make autoconf automake
Apache 依赖以下库(常见):
yum install -y \
pcre pcre-devel \
zlib zlib-devel \
openssl openssl-devel \
expat expat-devel
mkdir -p /usr/local/src
cd /usr/local/src
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
可到官网查看最新版本:https://httpd.apache.org/download.cgi
Apache 2.4 需要 APR 和 APR-Util。
cd /usr/local/src
wget https://downloads.apache.org/apr/apr-1.7.4.tar.gz
tar -zxvf apr-1.7.4.tar.gz
wget https://downloads.apache.org/apr/apr-util-1.6.3.tar.gz
tar -zxvf apr-util-1.6.3.tar.gz
cp -r apr-1.7.4 httpd-2.4.58/srclib/apr
cp -r apr-util-1.6.3 httpd-2.4.58/srclib/apr-util
cd /usr/local/src/httpd-2.4.58
推荐配置:
./configure \
--prefix=/usr/local/apache \
--sysconfdir=/etc/httpd \
--enable-so \
--enable-ssl \
--enable-rewrite \
--with-included-apr \
--with-mpm=prefork \
--enable-modules=most \
--enable-mods-shared=most
说明:
--prefix:安装路径--enable-so:启用动态模块--enable-ssl:HTTPS 支持--enable-rewrite:URL 重写--with-included-apr:使用自带 APRmake
make install
/usr/local/apache/bin/apachectl start
浏览器访问:
http://服务器IP
看到 It works! 表示成功。
echo 'export PATH=/usr/local/apache/bin:$PATH' >> /etc/profile
source /etc/profile
vi /etc/systemd/system/httpd.service
内容示例:
[Unit]
Description=Apache HTTP Server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop
ExecReload=/usr/local/apache/bin/apachectl graceful
PIDFile=/usr/local/apache/logs/httpd.pid
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start httpd
systemctl enable httpd
netstat -tunlp | grep 80
关闭占用端口的服务(如 nginx)。
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
✅ 编译安装 Apache 的核心步骤:
./configure → make → make install如果你需要 指定 PHP / 多版本 Apache / 性能优化 / 与 Nginx 搭配,可以继续问我。