当前位置: 首页 > news >正文

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

总结需要安装的包

sudo yum groupinstall "Development Tools" -y  #httpd的依赖包yum install tar -y #tar压缩包sudo yum install apr-devel apr-util-devel  #APR库 提供跨平台接口的库sudo yum install pcre pcre-devel  # PCRE库和 pcre-config工具--提供PCRE库的编译和链接信息​​
sudo yum install openssl-devel  # Openssl库​
​

方法一、用init.d(这里用HTTPD-2.4.54为例)

1.安装httpd需要的依赖包


sudo yum groupinstall "Development Tools" -y

2.下载httpd2.4 源码

wget https://archive.apache.org/dist/httpd/httpd-2.4.54.tar.gz

3.解压下载压缩包

安装好tar包-----yum install tar -y

tar -zxvf httpd-2.4.54.tar.gz
cd httpd-2.4.54 

4.配置http

 ./configure --prefix=/usr/local/httpd  --enable-so --enable-rewrite --enable-charset-lite --enable-cgi --with-zlib --with-pcre --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork

        源码编译的时候出现这个问题

                1、configure: checking for APR... no configure: error: APR not found. Please read the documentation.这个错误,意味着配置脚本无法在系统中找到APR库。APR是一个为操作系统级功能提供跨平台接口的库

sudo yum install apr-devel apr-util-devel 

                2、 configure: error: pcre(2)-config for libpcre not found. PCRE is required and available from http://pcre.org/  出现这个问题意味着配置脚本无法在系统中找到PCRE库的 pcre-config 工具。这个工具用于提供PCRE库的编译和链接信息。

sudo yum install pcre pcre-devel  

5.编译并安装httpd


make -j 4
sudo make install

6.创建Apache用户和组

sudo groupadd -r apache
sudo useradd -r -g apache -s /sbin/nologin apache

7.将 User daemon 改为 User apache:

sed -r -i "s/^User [a-zA-Z]*/User apache/" /usr/local/httpd/conf/httpd.conf 


8.将 Group daemon 改为 Group apache

sed -r -i "s/^Group [a-zA-Z]*/Group apache/" /usr/local/httpd/conf/httpd.conf


9.将 DocumentRoot "/usr/local/httpd/htdocs" 改为 DocumentRoot "/var/www":

sed -r -i "s%^DocumentRoot \".*\"%DocumentRoot \"/var/www\"%" /usr/local/httpd/conf/httpd.conf 


10.将 <Directory "/usr/local/httpd/htdocs"> 改为 <Directory "/var/www">

sed -r -i "s%^<Directory \".*htdocs\">%<Directory \"/var/www\">%" /usr/local/httpd/conf/httpd.conf 


11.确保 /var/www 目录存在

sudo mkdir -p /var/www


12.创建一个系统服务管理脚本 /etc/init.d/httpd:

sudo vi /etc/init.d/httpd 
#!/bin/bash
. /etc/rc.d/init.d/functionsapachectl=/usr/local/httpd/bin/apachectl
httpd=/usr/local/httpd/bin/httpd
prog=httpd
start() {echo -n $"Starting $prog: "daemon $httpd $OPTIONSRETVAL=$?echo[ $RETVAL = 0 ] && touch /var/lock/subsys/httpdreturn $RETVAL
}stop() {echo -n $"Stopping $prog: "killproc $httpdRETVAL=$?echo[ $RETVAL = 0 ] && rm -f /var/lock/subsys/httpdreturn $RETVAL
}# See how we were called.
case "$1" instart)start;;stop)stop;;status)status $httpd;;restart)stopstart;;condrestart)if [ -f /var/lock/subsys/httpd ]; thenstopstartfi;;*)echo $"Usage: $0 {start|stop|restart|condrestart|status}"exit 1
esacexit $?

13.赋予执行权限

sudo chmod +x /etc/init.d/httpd 

14.将 httpd 服务添加到系统服务管理中,并设置开机启动

sudo chkconfig --add httpd
sudo chkconfig httpd on 

        如果出现以下的问题:

                查看/etc/init.d/目录下是否存在httpd脚本,并检查它是否包含chkconfig所需的注释(如# chkconfig: 2345 10 90)-----对于没有包含chkconfig这个就需要以下的操作

                添加chkconfig注释
在脚本的开头部分(通常在#!/bin/sh之后),添加以下注释行

# chkconfig: 2345 10 90
# description: Apache HTTP Server

 15.启动httpd服务

systemctl start httpd
systemctl enable httpd

16、测试 httpd 服务是否正常运行       

  使用 curl 命令测试(本地或远程测试 ) 
yum install curl -y

步骤:
        在命令行中输入curl http://<服务器IP地址或域名>。
        如果 httpd 服务正常运行,curl命令会返回服务器返回的网页内容,例如 HTML 代码等。如果服务没有正常运行,可能会出现无法连接、超时等错误信息。

我这里用了我服务端的IP地址

方法二:使用 systemd 服务文件(这里用HTTPD-2.4.46为例)

前三个步骤与init.d脚本的方法大同小异出现错误的形式也一样可以看以上的方法步骤

1.下载源码

   wget https://archive.apache.org/dist/httpd/httpd-2.4.46.tar.gz

2.解压源码

tar -xzf httpd-2.4.46.tar.gz

cd httpd-2.4.46

3.编译安装

./configure --prefix=/usr/local/apache --enable-so --enable-ssl

        checking whether to enable mod_ssl... configure: error: mod_ssl has been requested but can not be built due to prerequisite failures遇到这个错误,通常意味着缺少必要的依赖项或环境配置不正确,导致 configure 脚本无法成功构建 SSL 模块

         确保你的系统上已经安装了 OpenSSL 开发库

sudo yum install openssl-devel

4.make

        出现以下错误,这意味着你的系统中没有安装 make 工具。make 是一个用于管理编译过程的工具,它根据 Makefile 中的指令来编译和链接程序

sudo yum groupinstall "Development Tools"

5.创建 systemd 服务文件

创建vim  /etc/systemd/system/httpd.service 文件

[Unit]
Description=The Apache HTTP Server
After=network.target[Service]
Type=forking
PIDFile=/usr/local/apache/logs/httpd.pid
ExecStart=/usr/local/apache/bin/httpd -k start
ExecReload=/usr/local/apache/bin/httpd -k restart
ExecStop=/usr/local/apache/bin/httpd -k stop
PrivateTmp=true[Install]
WantedBy=multi-user.target
~

6.重载systemd配置:

systemctl daemon-reload

7.启动httpd服务和状态:

systemctl start httpd
systemctl status httpd

8.测试 httpd 服务是否正常运行(网页访问测试)


        前提条件:确保 httpd 服务已经启动,并且防火墙规则允许访问 httpd 服务的端口。
步骤:
        打开网页浏览器,在地址栏中输入服务器的 IP 地址或者域名(如果有配置域名解析)。
        如果 httpd 服务正常运行并且配置正确,应该可以看到 Apache 的默认欢迎页面或者你自己配置的网站首页。这表明 httpd 服务能够正确地接收和处理 HTTP 请求,并返回相应的网页内容。

http://www.lryc.cn/news/521421.html

相关文章:

  • Linux固定ip
  • Java 输入输出流(上)
  • mysql、oracle、sqlserver的区别
  • Java+Maven+GDAL
  • 初识算法和数据结构P1:保姆级图文详解
  • 【Go】Go Gorm 详解
  • 【IDEA版本升级JDK21报错方法引用无效 找不到符号】
  • Node.js 版本管理工具完全指南
  • JavaSE学习心得(多线程与网络编程篇)
  • 平均精确率均值(mAP)
  • VUE学习笔记1__创建VUE实例
  • Inxpect毫米波安全雷达:精准检测与动态保护,工业自动化可靠选择
  • 基于禁忌搜索算法的TSP问题最优路径搜索matlab仿真
  • C51交通控制系统的设计与实现
  • 深度学习的超参数
  • 网络安全面试题及经验分享
  • 【Golang 面试题】每日 3 题(三十一)
  • 微服务架构:挑战与机遇并存
  • Vue语音播报功能
  • 【Java设计模式-4】策略模式,消灭if/else迷宫的利器
  • citrix netscaler13.1 重写负载均衡响应头(基础版)
  • 【AI学习】地平线首席架构师苏箐关于自动驾驶的演讲
  • QILSTE H11-D212HRTCG/5M高亮红绿双色LED灯珠 发光二极管LED
  • 2️⃣java基础进阶——多线程、并发与线程池的基本使用
  • RAG多路召回
  • 复杂 C++ 项目堆栈保留以及 eBPF 性能分析
  • 网安——计算机网络基础
  • ZCC1923替代BOS1921Piezo Haptic Driver with Digital Front End
  • Kutools for Excel 简体中文版 - 官方正版授权
  • PostgreSQL和MySQL有什么区别?