一键编译安装和卸载httpd2.4.41

一键编译安装和卸载httpd-2.4.41

需要注意的地方:

1、编译apr和apr-utils:官方的INSTALL文档说的很详细了,下载对应的包解压到srclib目录即可,不要带版本号

2、system脚本:yum装一个httpd,考过来改改就行了

3、启动用户问题:源码安装的httpd配置文件用户是daemon,装完后要改下

脚本在CentOS7测试能用

[root@node1 ~]# cat httpd-2.4.41.sh#!/bin/bash#**************************************************************#Author:                     哈啰#QQ:                         599503252#Date:                       2019-08-08#FileName:                   install_httpd.sh#URL:                        https://blog.51cto.com/14012942#Description:                The test script#Copyright (C):              2019 Copyright ©  站点名称  版权所有#************************************************************#set -eRED="\033[0;31m"GREEN="\033[0;32m"NO_COLOR="\033[0m"PREFIX=/usr/local/httpd2.4.41
SYSCONFDIR=/etc/httpd
SRC=/usr/src
FLAG=$1CPUS=`cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l`
CORE=`cat /proc/cpuinfo| grep "cpu cores"| uniq | awk '{print $4}'`
J=$((${CPUS}*${CORE}))
FILEURL='https://ftp.bit.nl/apache/httpd-2.4.41.tar.gz https://archive.apache.org/dist/apr/apr-1.7.0.tar.gz https://archive.apache.org/dist/apr/apr-util-1.6.1.tar.gz'# 判断是不是rootjudge_root() {
    [ $(id -u) != "0" ] && { echo -e "${RED}Error:${NO_COLOR} You must be root to run this script."; exit 1; }   
}# download download_source() {    cd
    yum install wget -y    for i in ${FILEURL};do
    {
        wget ${i}
        if [ ! "$?" -eq 0 ];then
            echo "download failed!"
            exit 1        fi
    }    done}# installinstall() {    # Add the "apache" group and user
    /usr/sbin/groupadd -g 48 -r apache 2> /dev/null || :
    /usr/sbin/useradd -c "Apache" -u 48 -g apache \
    -s /sbin/nologin -r -d /usr/share/httpd apache 2> /dev/null || : 
    wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    yum install gcc openssl-devel pcre-devel libnghttp2-devel ncurses-devel  lbzip2  bzip2 expat-devel autoconf libtool -y
    tar xf httpd-2.4.41.tar.gz -C ${SRC}/    #apr这个解压到srclib目录即可,官方文档说的很明白了,如下
    #if you   need to use the APR and APR-Util from the apr.apache.org  project. If the latter, download the latest versions and unpack them to ./srclib/apr and ./srclib/apr-util (no  version numbers in the directory names) and use  ./configure's --with-included-apr option
    tar xf  apr-util-1.6.1.tar.gz -C /usr/src/httpd-2.4.41/srclib/
    tar xf apr-1.7.0.tar.gz -C /usr/src/httpd-2.4.41/srclib/
    mv /usr/src/httpd-2.4.41/srclib/apr-util-1.6.1 /usr/src/httpd-2.4.41/srclib/apr-util
    mv /usr/src/httpd-2.4.41/srclib/apr-1.7.0  /usr/src/httpd-2.4.41/srclib/apr    cd ${SRC}/httpd-2.4.41
    ./configure \
    --prefix=${PREFIX} \
    --sysconfdir=${SYSCONFDIR} \
    --enable-http2 \
    --enable-ssl \
    --enable-so \
    --enable-cgi \
    --enable-rewrite \
    --with-zlib \
    --with-pcre \
    --with-included-apr \
    --enable-modules=most \
    --enable-mpms-shared=all \
    --with-mpm=prefork 
    make -j ${J}
    make install    # 使用system启动就不要path变量了
    # echo "PATH=${PREFIX}/bin:$PATH" >> /etc/profile.d/env.sh
    # source /etc/profile.d/env.sh
    cat > /usr/lib/systemd/system/httpd.service <<EOF
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=simple
EnvironmentFile=${SYSCONFDIR}/httpd.conf
ExecStart=${PREFIX}/bin/apachectl -k start  -DFOREGROUND
ExecReload=${PREFIX}/bin/apachectl  -k graceful
ExecStop=/usr/bin/kill -WINCH ${MAINPID}PrivateTmp=true[Install]
WantedBy=multi-user.target

EOF    #修改apache启动的用户
    sed  '/^Group/ s/daemon/apache/' /etc/httpd/httpd.conf  -i
    sed  '/^User/ s/daemon/apache/' /etc/httpd/httpd.conf  -i    #加载一下
    systemctl daemon-reload
}# test_webtest_web() {    #防止启动太慢,后续可以改
    sed  '/#ServerName www.example.com/a ServerName www.example.com:80' ${SYSCONFDIR}/httpd.conf -i    # apachectl start
    systemctl start httpd
    ss -ltn | grep -q :80
    [ "$?" -eq 0 ] && echo -e "${GREEN}May be web server is ok! \n If not ok,please check selinux and firewalld status.${NO_COLOR}" || \        echo -e "${RED}ERROR,Please check the web server.${NO_COLOR}"}remove_httpd() {    # source /etc/profile.d/env.sh
    # apachectl stop
    systemctl stop httpd
    rm -rf ${PREFIX} ${SYSCONFDIR} ${SRC}/httpd-2.4.41 /usr/lib/systemd/system/httpd.service    # sed -i '/^PATH/d' /etc/profile.d/env.sh}judge_uninstall(){if [ "$FLAG" = "uninstall" ];then
    remove_httpd    exit 0fi}main() {
    judge_uninstall
    judge_root
    download_source
    install
    test_web
}

main


上一篇:linux编译安装Apache


下一篇:如何让Tomcat使用APR连接器