一、准备编译环境
Apache 2.4.1 是从源码编译安装的,先把编译器和基础工具装好:
sudo yum groupinstall -y "Development Tools"
sudo yum install -y gcc make autoconf apr-devel apr-util-devel pcre-devel openssl-devel
二、下载并解压
安装包下载:https://pan.quark.cn/s/14aa7efc2a7b
tar -zxvf httpd-2.4.1.tar.gz
cd httpd-2.4.1
三、配置编译参数
进入目录后先配置,指定安装位置和常用模块:
./configure \
--prefix=/usr/local/apache2 \
--enable-so \
--enable-rewrite \
--enable-ssl \
--with-mpm=prefork
--prefix:安装目录--enable-so:支持动态加载模块--enable-rewrite:开启重写规则--enable-ssl:支持 HTTPS--with-mpm=prefork:使用 prefork 模式(老版本常用)
如果缺依赖,会明确提示缺什么包,按提示装即可。
四、编译并安装
配置没问题就开始编译:
make
sudo make install
没有报错就说明安装完成。
五、启动 Apache
进入安装目录的 bin 目录启动:
cd /usr/local/apache2/bin
sudo ./apachectl start
六、验证是否成功
打开浏览器访问:
http://服务器IP
看到 “It works!” 就是成功了。
也可以在服务器上用 curl 测试:
curl http://localhost
七、常用命令
- 启动:
sudo /usr/local/apache2/bin/apachectl start
- 停止:
sudo /usr/local/apache2/bin/apachectl stop
- 重启:
sudo /usr/local/apache2/bin/apachectl restart
- 检查配置:
sudo /usr/local/apache2/bin/apachectl configtest
八、常见问题
80 端口被占用
修改
conf/httpd.conf里的:Listen 8080然后重启。
启动时报 “Address already in use”
用:
netstat -tlnp | grep 80找到占用进程并停掉。
远程访问不了
放行防火墙端口:
sudo firewall-cmd --add-port=80/tcp --permanent sudo firewall-cmd --reload
这样就完成了 Apache httpd-2.4.1 的源码安装,适合老项目或需要特定版本的环境。