Linux安装Apache2.4.54操作步骤
基于 Red Hat系统从软件源安装
1. 安装 Apache 2.4.54
使用 yum 或 dnf(CentOS 8 及以上版本)命令安装 Apache:
bash
# CentOS 7及以下
sudo yum install httpd
# CentOS 8及以上
sudo dnf install httpd
2. 验证安装
使用以下命令验证 Apache 服务的状态:
bash
sudo systemctl status httpd
同样,若看到 active (running) 字样,说明服务已成功启动。在浏览器中输入服务器 IP 地址验证安装效果。
3. 管理 Apache 服务
启动服务:
bash
sudo systemctl start httpd
停止服务:
bash
sudo systemctl stop httpd
重启服务:
bash
sudo systemctl restart httpd
设置开机自启:
bash
sudo systemctl enable httpd
从源码编译安装(适用于所有 Linux 发行版)
1. 安装必要的依赖包
不同发行版安装依赖包的命令不同,以 CentOS 为例:
bash
sudo yum install -y gcc make pcre-devel expat-devel
如果是 Ubuntu 系统,使用以下命令:
bash
sudo apt install -y build-essential libpcre3-dev libexpat1-dev
2. 下载 Apache 2.4.54 源码包
从 Apache 官方镜像站点下载源码包:
bash
wget https://archive.apache.org/dist/httpd/httpd-2.4.54.tar.gz
3. 解压源码包
bash
tar -zxvf httpd-2.4.54.tar.gz
4. 进入解压后的目录
bash
cd httpd-2.4.54
5. 配置编译选项
bash
./configure --prefix=/usr/local/apache2 --enable-so --enable-rewrite
--prefix=/usr/local/apache2:指定 Apache 的安装路径。
--enable-so:启用动态加载模块支持。
--enable-rewrite:启用 URL 重写模块。
6. 编译和安装
bash
make
sudo make install
7. 启动 Apache 服务
bash
/usr/local/apache2/bin/apachectl start
8. 验证安装
在浏览器中输入服务器的 IP 地址进行验证。
9. 设置开机自启(可选)
创建一个启动脚本,以 CentOS 为例:
bash
sudo vi /etc/init.d/httpd
在文件中添加以下内容:
bash
#!/bin/sh
# chkconfig: 2345 85 15
# description: Apache HTTP Server
. /etc/rc.d/init.d/functions
apachectl="/usr/local/apache2/bin/apachectl"
prog="httpd"
lockfile=/var/lock/subsys/httpd
start() {
echo -n $"Starting $prog: "
daemon $apachectl start
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
daemon $apachectl stop
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
stop
start
}
reload() {
echo -n $"Reloading $prog: "
daemon $apachectl graceful
RETVAL=$?
echo
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
reload
;;
status)
status $prog
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status}"
exit 1
esac
exit $?
保存并退出文件后,赋予脚本执行权限:
bash
sudo chmod +x /etc/init.d/httpd
然后将其添加到系统服务管理中,并设置开机自启:
bash
sudo chkconfig --add httpd
sudo chkconfig httpd on