部署 LAMP(Linux, Apache, MySQL, PHP)环境现有很多教程,无论是一键安装脚本、宝塔面板,还是购买云 VPS 时预装的镜像,都能轻松搞定。为什么最终选择“编译安装”这种原始的方式?纯粹是突发奇想、自讨苦吃。虽然过程曲折,最后可能也只是“知其然不知其所以然”,但这确实是了解 Linux 软件依赖和架构的最佳途径。
同时,为了方便后续部署,我也将所有步骤整合到了一个 Shell 脚本中,如果你不想手动一步步操作,可以直接执行:
wget https://blog.nyankowo.com/bash/installapache.sh && chmod +x installapache.sh && bash installapache.sh1. 安装预备软件
本教程基于 CentOS/RHEL 环境。首先需要安装编译环境(GCC, Make 等)以及 Apache 所需的基础依赖库。
yum install -y perl epel-release
yum install -y wget tar gcc gcc-c++ cmake make lsof expat-devel openssl-devel zlib zlib-devel chkconfig firewalld2. 安装依赖库
Apache 2.4 的编译安装主要依赖三个核心库:apr、apr-util 和 pcre。此外,为了支持 HTTP/2 协议,我们需要额外安装 nghttp2。
2.1 安装 nghttp2
Apache 在开启 http2 模块时,系统默认的库版本可能过低,因此我们需要手动编译较新版本的 nghttp2。软件下载位置nghttp2 Releases:
wget https://github.com/nghttp2/nghttp2/releases/download/v1.63.0/nghttp2-1.63.0.tar.gz -P /usr/local/src
tar -xvf /usr/local/src/nghttp2-1.63.0.tar.gz -C /usr/local/src && cd /usr/local/src/nghttp2-1.63.0
./configure && make && make install2.2 安装 PCRE
软件下载位置PCRE SourceForge:
wget https://jaist.dl.sourceforge.net/project/pcre/pcre/8.45/pcre-8.45.tar.gz -P /usr/local/src
tar -xvf /usr/local/src/pcre-8.45.tar.gz -C /usr/local/src && cd /usr/local/src/pcre-8.45
./configure --prefix=/usr/local/pcre && make && make install2.3 安装 APR
APR (Apache Portable Runtime) 是 Apache 的核心运行库。这里有一个常见的坑:libtool 配置问题,我们需要在编译前通过 sed 命令修正它,否则会导致编译失败。软件下载位置APR Archive:
wget https://archive.apache.org/dist/apr/apr-1.7.5.tar.gz -P /usr/local/src
tar -xvf /usr/local/src/apr-1.7.5.tar.gz -C /usr/local/src && cd /usr/local/src/apr-1.7.5
# 修复 libtool 报错:将 RM='$RM' 替换为 RM='$RM -f'
sed -i "s/RM='\$RM'/RM='\$RM -f'/g" configure
./configure --prefix=/usr/local/apr && make && make install2.4 安装 APR-Util
注意:APR-Util 依赖于 APR,请务必先完成上一步。软件下载位置APR-Util Archive:
wget https://archive.apache.org/dist/apr/apr-util-1.6.3.tar.gz -P /usr/local/src
tar -xvf /usr/local/src/apr-util-1.6.3.tar.gz -C /usr/local/src && cd /usr/local/src/apr-util-1.6.3
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/bin/apr-1-config && make && make install3. 编译安装 Apache (Httpd)
依赖库准备完毕后,终于来到主角 Apache 的安装。这里我们启用了大量常用模块(如 SSL、Rewrite、Deflate 等)。软件下载位置Apache Download:
wget https://dlcdn.apache.org/httpd/httpd-2.4.67.tar.gz -P /usr/local/src
tar -xvf /usr/local/src/httpd-2.4.67.tar.gz -C /usr/local/src && cd /usr/local/src/httpd-2.4.67
# 编译配置
./configure \
--prefix=/usr/local/apache \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util \
--with-pcre=/usr/local/pcre/bin/pcre-config \
--enable-deflate \
--enable-expires \
--enable-headers \
--enable-modules=most \
--enable-so \
--enable-mpms-shared=all \
--enable-rewrite \
--enable-ssl \
--with-nghttp2=/usr/local/nghttp2 \
--enable-proxy-http2=shared
make && make install4. 系统服务与环境变量配置
安装完成后,为了方便管理,我们需要配置软链接、环境变量以及 Systemd 服务。
4.1 基础配置
# 创建软链接,方便访问
ln -s /usr/local/apache /apache
# 添加环境变量
sed -i '$aexport PATH=$PATH:/apache/bin' /etc/profile && source /etc/profile常用命令测试:
- 检查配置:
apachectl -t - 启动服务:
apachectl start - 停止服务:
apachectl stop - 平滑重启:
apachectl graceful
4.2 注册 Systemd 服务
为了能使用 systemctl 方便地管理 Apache,并实现开机自启,我们需要创建一个服务文件:
cat <<EOL | sudo tee /etc/systemd/system/httpd.service
[Unit]
Description=Apache HTTP Server (2.4.67 Secure)
After=network-online.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/apache/logs/httpd.pid
ExecStart=/usr/local/apache/bin/apachectl start
ExecReload=/usr/local/apache/bin/apachectl graceful
ExecStop=/usr/local/apache/bin/apachectl stop
KillMode=mixed
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOL
# 重载系统服务并设置开机自启
sudo systemctl daemon-reload
systemctl enable httpd5. 防火墙配置
如果你的服务器开启了 firewalld,需要放行 Web 端口(80/TCP)。
# 查看防火墙状态
systemctl status firewalld
# 开启80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
# 重载配置生效
firewall-cmd --reload6. 验证安装
最后,我们需要修改 Apache 配置文件以消除启动时的 ServerName 警告,并测试访问。
修改配置:备份并编辑配置文件。
cp /apache/conf/httpd.conf /apache/conf/httpd.conf_bak sed -i 's/#ServerName [www.example.com:80/ServerName](https://www.example.com:80/ServerName) localhost:80/g' /apache/conf/httpd.conf- 启动服务:
systemctl start httpd - 浏览器访问:在浏览器输入服务器 IP,如果看到 "It works!" 字样(如下图所示),说明 Apache 已成功部署。
