天天看点

【Linux】【Httpd】Centos7 编译安装 & 添加systemd服务

针对Linux 运维来说,养成必要的、良好的习惯来说至关重要,目前科技技术的更新层次不齐,更新的速度都大不相同,所以在Linux 的世界里,我们在安装任何服务,任何应用的之前,都应该查询当前系统版本、系统内核情况,防止因差异化导致服务无法正常使用。

  • # cat /etc/redhat-release
    • CentOS Linux release 7.7.1908 (Core)
  • #uname -a
    • Linux test 3.10.0-957.21.3.el7.x86_64 #1 SMP Tue Jun 18 16:35:19 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

接下来,我们进入今天的主题,编译安装Httpd 和添加 systemd服务。

1、准备依赖环境

  • # yum -y install pcre-devel
  • # yum -y install openssl-devel
  • # yum -y groupinstall "Development Tools"

2、arp-util安装的依赖包

  • # yum install expat-devel

3、编译安装apr-1.7.0

  • # wget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.7.0.tar.gz
  • # tar xf apr-1.7.0.tar.gz
  • # cd apr-1.7.0
  • # ./configure -prefix=/usr/local/apr(执行时,可能会报rm: cannot remove `libtoolT': No such file or directory )
  • # make && make install

4、编译安装apr-util-1.6.1

  • # wget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-util-1.6.1.tar.gz
  • # tar xf apr-util-1.6.1.tar.gz
  • # cd apr-util-1.6.1
  • # ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
  • # make && make install

5、编译安装httpd-2.4.41

  • # wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.41.tar.gz
  • # tar xf httpd-2.4.41.tar.gz
  • # cd httpd-2.4.41
  • # ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modeles=most --enable-mpms-shared=all
  • # make && make install

注意事项:apache 2.4版本之后,MPM(Multipath Processing Moules)使用多路处理模块,一共有三种工作模型,默认prefork,通过--with-mpm来指定configure编译模型,比如worke模型:--with-mpm=worker

  • prefork模型:是两级进程模型,由父进程负责生成和管理多个子进程,每个子进程负责响应一个用户的请求。
  • worker模型:是三级进程模型,由父进程生成和管理多个子进程,每个子进程又生成多个线程,由每一个线程来响应一个用户的请求。
  • event模型:是二级进程模型,由父进程生成和管理多个子进程,每个子进程又通过事务(event-driven)机制直接响应多个用户请求。

6、启动httpd

启动之前,关于httpd服务如何加入systemctl并设置开机启动的话题,我们先来准备相关内容;

1)编写启动脚本:vim httpd

#!/bin/bash

# chkconfig: 12345 80 90 function start_http(){ /usr/local/apache/bin/apachectl start } function stop_http(){ /usr/local/apache/bin/apachectl stop } case "$1" in start) start_http ;; stop) stop_http ;; restart) stop_http start_http ;; *) echo "Usage : start | stop | restart" ;; esac

PS: 经实践检验,该httpd的shell脚本,的restart启动指令还在一定不足,会导致服务重启失败,直接active(exit)状态;处理方式分别执行systemctl stop httpd和systemctl start httpd 命令,先停后启的原则!后续有时间在优化该脚本!!!

2)加入系统服务

  • # chmod a+x httpd
  • # cp -arf httpd /etc/init.d/

3)启动编写的httpd服务

  • # systemctl daemon-reload
  • # systemctl start httpd

4)设置开机启动

  • # chkconfig --add httpd

7、httpd的安装目录结构说明

  • /etc/httpd/ : 配置文件的主目录,在编译安装./configure时候指定的--sysconfdir可以根据需要更换
    • extra : 子配置文件
    • httpd.conf : 主配置文件
    • magic
    • mime.type
    • original
  • /usr/local/apache/ : Apache 的安装服务的主目录
    • bin : apache 的 命令文件
    • build
    • cgi-bin
    • error
    • htdocs
    • icons
    • include
    • logs
    • man
    • manual
    • modules : apache的LoadModule的so文件

继续阅读