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

打印 上一主题 下一主题

主题 906|帖子 906|积分 2718

总结必要安装的包

  1. sudo yum groupinstall "Development Tools" -y  #httpd的依赖包
  2. yum install tar -y #tar压缩包
  3. sudo yum install apr-devel apr-util-devel
  4. #APR库 提供跨平台接口的库
  5. sudo yum install pcre pcre-devel  # PCRE库和 pcre-config工具--提供PCRE库的编译和链接信息
  6. ​​
  7. sudo yum install openssl-devel  # Openssl库
复制代码
方法一、用init.d(这里用HTTPD-2.4.54为例)

1.安装httpd必要的依赖包

  1. sudo yum groupinstall "Development Tools" -y
复制代码

2.下载httpd2.4 源码

  1. wget https://archive.apache.org/dist/httpd/httpd-2.4.54.tar.gz
复制代码

3.解压下载压缩包

安装好tar包-----yum install tar -y
  1. tar -zxvf httpd-2.4.54.tar.gz
  2. cd httpd-2.4.54
复制代码

4.设置http

  1. ./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是一个为操作系统级功能提供跨平台接口的库

  1. 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库的编译和链接信息。

  1. sudo yum install pcre pcre-devel  
复制代码
5.编译并安装httpd

  1. make -j 4
  2. sudo make install
复制代码
6.创建Apache用户和组

  1. sudo groupadd -r apache
  2. sudo useradd -r -g apache -s /sbin/nologin apache
复制代码
7.将 User daemon 改为 User apache:

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

8.将 Group daemon 改为 Group apache


  1. 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":


  1. sed -r -i "s%^DocumentRoot ".*"%DocumentRoot "/var/www"%" /usr/local/httpd/conf/httpd.conf
复制代码

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


  1. sed -r -i "s%^<Directory ".*htdocs">%<Directory "/var/www">%" /usr/local/httpd/conf/httpd.conf
复制代码

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


  1. sudo mkdir -p /var/www
复制代码

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


  1. sudo vi /etc/init.d/httpd 
复制代码
  1. #!/bin/bash
  2. . /etc/rc.d/init.d/functions
  3. apachectl=/usr/local/httpd/bin/apachectl
  4. httpd=/usr/local/httpd/bin/httpd
  5. prog=httpd
  6. start() {
  7.     echo -n $"Starting $prog: "
  8.     daemon $httpd $OPTIONS
  9.     RETVAL=$?
  10.     echo
  11.     [ $RETVAL = 0 ] && touch /var/lock/subsys/httpd
  12.     return $RETVAL
  13. }
  14. stop() {
  15.     echo -n $"Stopping $prog: "
  16.     killproc $httpd
  17.     RETVAL=$?
  18.     echo
  19.     [ $RETVAL = 0 ] && rm -f /var/lock/subsys/httpd
  20.     return $RETVAL
  21. }
  22. # See how we were called.
  23. case "$1" in
  24.   start)
  25.     start
  26.     ;;
  27.   stop)
  28.     stop
  29.     ;;
  30.   status)
  31.     status $httpd
  32.     ;;
  33.   restart)
  34.     stop
  35.     start
  36.     ;;
  37.   condrestart)
  38.     if [ -f /var/lock/subsys/httpd ]; then
  39.         stop
  40.         start
  41.     fi
  42.     ;;
  43.   *)
  44.     echo $"Usage: $0 {start|stop|restart|condrestart|status}"
  45.     exit 1
  46. esac
  47. exit $?
复制代码
13.赋予执行权限

  1. sudo chmod +x /etc/init.d/httpd
复制代码
14.将 httpd 服务添加到系统服务管理中,并设置开机启动

  1. sudo chkconfig --add httpd
  2. sudo chkconfig httpd on
复制代码
        如果出现以下的问题:

                查看/etc/init.d/目录下是否存在httpd脚本,并查抄它是否包含chkconfig所需的注释(如# chkconfig: 2345 10 90)-----对于没有包含chkconfig这个就必要以下的操作
                添加chkconfig注释
在脚本的开头部分(通常在#!/bin/sh之后),添加以下注释行
  1. # chkconfig: 2345 10 90
  2. # description: Apache HTTP Server
复制代码
 15.启动httpd服务

  1. systemctl start httpd
  2. systemctl enable httpd
复制代码

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

  利用 curl 命令测试(本地或长途测试 ) 

  1. 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 开发库
  1. sudo yum install openssl-devel
复制代码
4.make


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

  1. sudo yum groupinstall "Development Tools"
复制代码
5.创建 systemd 服务文件

创建vim  /etc/systemd/system/httpd.service 文件
  1. [Unit]
  2. Description=The Apache HTTP Server
  3. After=network.target
  4. [Service]
  5. Type=forking
  6. PIDFile=/usr/local/apache/logs/httpd.pid
  7. ExecStart=/usr/local/apache/bin/httpd -k start
  8. ExecReload=/usr/local/apache/bin/httpd -k restart
  9. ExecStop=/usr/local/apache/bin/httpd -k stop
  10. PrivateTmp=true
  11. [Install]
  12. WantedBy=multi-user.target
  13. ~
复制代码
6.重载systemd设置:

  1. systemctl daemon-reload
复制代码
7.启动httpd服务和状态:

  1. systemctl start httpd
  2. systemctl status httpd
复制代码

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


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


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

商道如狼道

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表