• shell脚本安装apache服务


    使用shell脚本安装apache
    安装httpd步骤:
    创建一个系统用户
    下载依赖包
    下载软件Apr、Apr-util、httpd软件包、并解压
    编译apr、Apr-util、httpd
    设置环境变量、头文件、man文档
    设置开机自启

    #!/bin/bash
    
    # 关闭防火墙和SElinux
    systemctl disable --now firewalld
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config 
    setenforce 0
    echo -e "\033[1;32m 防火墙和SElinux已关闭 \033[0m"
    
    # 定义变量
    httpd_version=2.4.54
    install_dir=/usr/local/apache
    
    # 创建用户
    id apache &> /dev/null
    if [ $? -ne 0 ];then
        useradd -r -M -s /sbin/nologin apache
        echo "创建apache用户"
    else
        echo "用户apache已存在"
    fi 
    
    # 下载依赖包
    yum -yq install boost-devel --allowerasing openssl-devel pcre-devel expat-devel libtool make gcc gcc-c++ wget
    
    # 下载软件包
    if [ ! -d apr-1.7.0 ] && [ ! -d httpd-${httpd_version} ];then
        cd /usr/src
       # wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz
       # wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz
       # wget https://downloads.apache.org/httpd/httpd-${httpd_version}.tar.gz
    fi
    
    
    # 下载并解压Apr包
    cd /usr/src
    if [ ! -d /usr/src/apr-1.7.0 ];then
        tar xf apr-1.7.0.tar.gz
        sed -i '/$RM "$cfgfile"/d' apr-1.7.0/configure
        echo "Apr包已解压"
    else
        echo "apr-1.7.0目录已存在"
    fi
    
    # 下载并 解压apr-util包
    if [ ! -d /usr/src/apr-util-1.6.1 ];then
        tar xf apr-util-1.6.1.tar.gz
        echo "apr-util包已解压"
    else
        echo "apr-util-1.6.1目录已存在"
    fi
    
    # 下载并解压httpd包
    if [ ! -d /usr/src/httpd-${httpd_version} ];then
        tar xf httpd-${httpd_version}.tar.gz
        echo "httpd包已解压"
    else
        echo "httpd-${httpd_version}目录已存在"
    fi
    
    # 编译Apr
    cd /usr/src/apr-1.7.0
    if [ ! -d /usr/local/apr ];then
        ./configure --prefix=/usr/local/apr && \
        make && make install 
    else
        echo "apr目录已存在"
    fi
    
    # 编译apr-util
    cd ../apr-util-1.6.1
    if [ ! -d /usr/local/apr-util ];then 
        ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && \
        make && make install 
    else 
        echo "apr-util目录已存在"
    fi
    
    # 编译httpd
    cd ../httpd-${httpd_version}
    if [ ! -d $install_dir ];then
        ./configure --prefix=$install_dir \
             --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-modules=most \
             --enable-mpms-shared=all \
             --with-mpm=prefork && \
        make && make install
    else
        echo "apache目录已存在"
    fi
    
    if [ ! -f etc/profile.d/apache.sh ] && [ ! -h /usr/include/apache];then
        # 设置环境变量
        echo "export PATH=$install_dir/bin:\$PATH" > /etc/profile.d/apache.sh
        # 设置头文件
        ln -s $install_dir/include /usr/include/apache
        # 设置man文档
        echo "MANDATORY_MANPATH        $install_dir/man" >> /etc/man_db.conf
    fi
    
    # 配置service文件
    if [ ! -f /usr/lib/systemd/system/httpd.service ];then
    cat > /usr/lib/systemd/system/httpd.service << EOF
    [Unit]
    Description=httpd server daemon
    After=network.target sshd-keygen.target
    
    [Service]
    Type=forking
    ExecStart=$install_dir/bin/apachectl start
    ExecStop=$install_dir/bin/apachectl stop
    ExecReload=/bin/kill -HUP \$MAINPID
    
    [Install]
    WantedBy=multi-user.target
    EOF
    fi
    
    # 刷新程序
    systemctl daemon-reload
    
    # 启动、并开机自启
    systemctl enable --now httpd
    echo -e "\e[1;32m apache已开机自启动 \e[0m"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
  • 相关阅读:
    C#是否应该限制链式重载的设计模式?
    1.2 Portfolio Theroy
    web前端开发网页设计课堂作业/html练习《课程表》
    MS14-068 漏洞分析—不安全的PAC
    【C++类】深拷贝、内联函数、数据成员
    SpringCloudAlibaba微服务docker容器打包和部署示例实战
    [附源码]计算机毕业设计SpringBoot游戏论坛网站
    为什么SQL预编译可以防止SQL注入攻击
    哈希树讲解
    分数计算 初级题目
  • 原文地址:https://blog.csdn.net/m0_58805648/article/details/126085689