源码编译安装httpd:
获取httpd源码包法一:
我这以windows上的虚拟机为例子,可以选择在windows上去官网下载httpd的源码包:
链接:Welcome! - The Apache HTTP Server Project


接着传入虚拟机实现安装
获取httpd源码包法二:
也可以在虚拟机上用wget命令直接获取httpd的源码包,我这里选择第二种方法,
bash
wget https://downloads.apache.org/httpd/httpd-2.4.62.tar.gz

安装httpd并进行相关配置
官方相关文档:Compiling and Installing - Apache HTTP Server Version 2.4
解压并安装相关依赖:
bash
#进行解压
tar xzf httpd-2.4.62.tar.gz
#切换到httpd目录
cd httpd-2.4.62
#安装相关依赖
dnf install gcc pcre-devel zlib-devel apr-devel apr-util-devel -y


编译安装:
bash
#配置
./configure --prefix=/usr/local/apache2 --enable-so --enable-mods-shared=all
#编译
make
#安装
make install



配置httpd:
bash
#创建httpd配置文件目录,并设置httpd.conf
mkdir /etc/httpd
cp /usr/local/apache2/conf/httpd.conf /etc/httpd/httpd.conf

服务管理脚本(方法一),使用systemd管理:
bash
#创建systemd服务文件
vim /usr/lib/systemd/system/httpd.service
在文件中添加如下内容:
bash
[Unit]
Description=Apache HTTP Server
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/apache2/logs/httpd.pid
ExecStart=/usr/local/apache2/bin/apachectl start
ExecStop=/usr/local/apache2/bin/apachectl stop
ExecReload=/usr/local/apache2/bin/apachectl graceful
Restart=on-failure
[Install]
WantedBy=multi-user.target
重载systemd配置:
bash
systemctl daemon-reload
启动httpd服务:
bash
systemctl start httpd
查看httpd服务状态:
bash
systemctl status httpd

测试httpd服务
bash
curl http://localhost

服务管理脚本(方法二),使用传统的sysvinit脚本:
创建sysvinit脚本
bash
vim /etc/init.d/httpd
输入以下内容:
bash
#!/bin/bash
# chkconfig: 2345 85 15
# description: Apache HTTP Server
# processname: httpd
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/apache2/bin/httpd
NAME=httpd
DESC="Apache HTTP Server"
LOCKFILE=/var/lock/subsys/httpd
test -x $DAEMON || exit 0
set -e
case "$1" in
start)
echo -n "Starting $DESC: "
$DAEMON -k start
touch $LOCKFILE
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
$DAEMON -k stop
rm -f $LOCKFILE
echo "$NAME."
;;
restart|force-reload)
echo -n "Restarting $DESC: "
$DAEMON -k graceful
echo "$NAME."
;;
status)
$DAEMON -k status
;;
*)
echo "Usage: $0 {start|stop|restart|force-reload|status}" >&2
exit 3
;;
esac
exit 0
设置脚本权限并添加到服务器管理:
bash
chmod +x /etc/init.d/httpd
chkconfig --add httpd
chkconfig httpd on
启动服务:
bash
service httpd start
测试服务:
bash
curl http://localhost

实验中遇到的问题:
编译过程中出现报错:
上述问题可能是gcc 编译器无法找到 /usr/lib/rpm/redhat/redhat-hardened-ld 文件或目录
我们来查找查找提供 redhat-hardened-ld 的软件包
bash
yum provides '*/redhat-hardened-ld'

重新安装redhat-rpm-config包
bash
yum install redhat-rpm-config -y

把之前编译生成的文件清除,避免旧的文件干扰:
bash
cd /root/httpd-2.4.62
make clean
接着重新编译即可