下面以 CentOS 7/8 为例,介绍 源码编译安装 Apache HTTP Server(httpd) 的完整流程。
(以 Apache 2.4.x 为例,这是目前主流版本)
# CentOS 7
yum groupinstall -y "Development Tools"
yum install -y \
gcc \
make \
autoconf \
libtool \
pcre-devel \
openssl-devel \
expat-devel \
zlib-devel
# CentOS 8
dnf groupinstall -y "Development Tools"
dnf install -y \
gcc \
make \
autoconf \
libtool \
pcre-devel \
openssl-devel \
expat-devel \
zlib-devel
Apache 2.4 需要:
mkdir -p /usr/local/src/apache
cd /usr/local/src/apache
wget https://dlcdn.apache.org//httpd/httpd-2.4.58.tar.gz
wget https://dlcdn.apache.org/apr/apr-1.7.4.tar.gz
wget https://dlcdn.apache.org/apr/apr-util-1.6.3.tar.gz
版本号可根据官网调整
https://httpd.apache.org/download.cgi
tar -xf apr-1.7.4.tar.gz
cd apr-1.7.4
./configure --prefix=/usr/local/apr
make && make install
cd ..
tar -xf apr-util-1.6.3.tar.gz
cd apr-util-1.6.3
./configure \
--prefix=/usr/local/apr-util \
--with-apr=/usr/local/apr
make && make install
cd ..
tar -xf httpd-2.4.58.tar.gz
cd httpd-2.4.58
./configure \
--prefix=/usr/local/apache \
--sysconfdir=/etc/httpd \
--enable-so \
--enable-ssl \
--enable-rewrite \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util \
--with-ssl \
--with-pcre
常用模块说明:
--enable-so:启用动态模块--enable-ssl:支持 HTTPS--enable-rewrite:支持 URL 重写make
make install
安装完成后:
/usr/local/apache/etc/httpd/httpd.confvim /etc/httpd/httpd.conf
确认:
Listen 80
ServerName localhost:80
/usr/local/apache/bin/apachectl start
curl http://localhost
或浏览器访问服务器 IP。
vim /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
PrivateTmp=true
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable httpd
systemctl start httpd
systemctl status httpd
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save
APR not found确认已正确安装 apr 和 apr-util,并指定路径。
netstat -tunlp | grep 80
检查:
DocumentRoot 权限<Directory> 配置✅ 已成功在 CentOS 上 源码编译安装 Apache 2.4
✅ 支持 SSL、Rewrite
✅ 可接入 systemd 管理
如果你需要:
可以继续问我。