源码编译安装httpd 2.4,并提供系统服务管理脚本

源码编译安装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

接着重新编译即可

相关推荐
myy-learn几秒前
31-TCP并发
服务器·网络·tcp/ip
名字还没想好☜7 分钟前
Docker buildx 多架构镜像实战:一次构建 amd64+arm64,告别 exec format error
运维·docker·eureka·架构·kubernetes
智购科技无人售货机厂家24 分钟前
2026自动售货机备用电池管理系统设计:从充放电保护到健康监测的工程实践~YH
大数据·linux·单片机·嵌入式硬件·微信
小雪崩25 分钟前
嵌入式学习 day39:TCP并发服务器模型
linux·服务器·c语言·网络·学习·tcp/ip
智购无人售货机厂家32 分钟前
2026自动售货机软硬件版本管理策略:从版本号规范到兼容性矩阵的工程实践~YH
运维·服务器·人工智能·单片机·嵌入式硬件·线性代数·矩阵
spencer_tseng44 分钟前
[j2cache ehcache.xml]/tmp/.ehcache-diskstore.lock (Permission denied)
xml·linux·python·ehcache·[j2cache
内核笔记1 小时前
休眠唤醒 standby 调试
linux·bsp
小白不想画工图1 小时前
银河麒麟V10系统安装QT5.12
linux·开发语言·qt·银河麒麟
bksczm1 小时前
Linux 五种 I/O 模型与多路复用详解
运维·服务器·网络
wjcroom1 小时前
FileBrowser的docker运行了改变密码长度的做法
运维·docker·容器