源码编译安装 Apache HTTP Server(httpd)2.4,并提供系统服务管理脚本以及测试,可以按照以下步骤进行。这里将提供两种方法来管理系统服务:一种是使用传统的init.d脚本,另一种是使用systemd服务单元文件。
步骤 1:源码编译安装 httpd 2.4
1. 下载 httpd 源码从 Apache 官方网站下载 httpd 2.4 的源码包
2. 解压源码包
tar -xjf httpd-2.4.xx.tar.bz2
cd httpd-2.4.xx
3. 配置、编译和安装
./configure --prefix=/usr/local/apache2 --enable-so --enable-rewrite
make
sudo make install
步骤 2:创建系统服务管理脚本
方法一:使用init.d脚本
1. 创建 init.d 脚本在/etc/init.d/目录下创建一个名为apache2的脚本文件
#!/bin/bash
# /etc/init.d/apache2
APACHE_HOME=/usr/local/apache2
APACHE_BIN=$APACHE_HOME/bin/apachectl
case "$1" in
start)
echo "Starting Apache HTTP Server..."
$APACHE_BIN start
;;
stop)
echo "Stopping Apache HTTP Server..."
$APACHE_BIN stop
;;
restart)
echo "Restarting Apache HTTP Server..."
$APACHE_BIN restart
;;
status)
$APACHE_BIN status
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
exit 0
2. 设置脚本权限并添加到系统服务
sudo chmod +x /etc/init.d/apache2
sudo chkconfig --add apache2
sudo chkconfig apache2 on
方法二:使用systemd服务单元文件
1. 创建 systemd 服务单元文件在/etc/systemd/system/目录下创建一个名为apache2.service的文件
【ini】
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/apache2/bin/apachectl start
ExecStop=/usr/local/apache2/bin/apachectl stop
ExecReload=/usr/local/apache2/bin/apachectl graceful
PIDFile=/usr/local/apache2/logs/httpd.pid
PrivateTmp=true
[Install]
WantedBy=multi-user.target
2. 重新加载 systemd 配置并启用服务
sudo systemctl daemon-reload
sudo systemctl enable apache2
步骤 3:测试服务管理脚本
1. 使用init.d脚本测试
sudo /etc/init.d/apache2 start
sudo /etc/init.d/apache2 status
curl http://localhost
sudo /etc/init.d/apache2 stop
2. 使用systemd服务测试
sudo systemctl start apache2
sudo systemctl status apache2
curl http://localhost
sudo systemctl stop apache2