#!/bin/bash
#
#********************************************************************
#Author: shijianing
#QQ: 3253533894
#Date: 2021-10-12
#FileName: install_httpd.sh
#URL: www.shjjianing.cn
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
URL=mirrors.aliyun.com
#URL=repo.huaweicloud.com
#URL=mirrors.cloud.tencent.com
#URL=mirrors.tuna.tsinghua.edu.cn
#URL=mirrors.163.com
#URL=archives.fedoraproject.org
#URL=mirrors.sohu.com
APR_URL=https://${URL}/apache/apr/
APR_FILE=apr-1.7.0
TAR=.tar.bz2
APR_UTIL_URL=https://${URL}/apache/apr/
APR_UTIL_FILE=apr-util-1.6.1
HTTPD_URL=https://${URL}/apache/httpd/
HTTPD_FILE=httpd-2.4.46
INSTALL_DIR=/apps/httpd24
CPUS=`lscpu|awk '/^CPU\(s\)/ {print $2}'`
MPM=event
color () {
RES_COL=60
MOVE_TO_COL="echo -en \\033[${RES_COL}G"
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_WARNING="echo -en \\033[1;33m"
SETCOLOR_NORMAL="echo -en \E[0m"
echo -n "$1" && $MOVE_TO_COL
echo -n "["
if [ $2 = "success" -o $2 = "0" ];then
${SETCOLOR_SUCCESS}
echo -n $" OK "
elif [ $2 = "failure" -o $2 = "1" ];then
${SETCOLOR_FAILURE}
echo -n $"FAILED"
else
${SETCOLOR_WARNING}
echo -n $"WARNING"
fi
${SETCOLOR_NORMAL}
echo -n "]"
echo
}
install_package(){
if [[ `lsb_release -is` == "CentOS" ]]&>/dev/null;then
yum -y install gcc make pcre-devel openssl-devel expat-devel wget bzip2 &>/dev/null
if [ $? -eq 0 ];then
color "相关软件安装成功" 0
else
color "相关软件安装失败" 1
fi
else
apt update &>/dev/null
apt -y install gcc make libapr1-dev libaprutil1-dev libpcre3 libpcre3-dev libssl-dev wget &>/dev/null
if [ $? -eq 0 ];then
color "相关软件安装成功" 0
else
color "相关软件安装失败" 1
exit 1
fi
fi
}
cd /usr/local/src
install_httpd(){
wget ${APR_URL}${APR_FILE}${TAR} &>/dev/null && wget ${APR_UTIL_URL}${APR_UTIL_FILE}${TAR} &>/dev/null && wget ${HTTPD_URL}${HTTPD_FILE}${TAR} &>/dev/null
if [ $? -eq 0 ]&>/dev/nul;then
color "软件包下载成功" 0
else
color "软件包下载失败" 1
exit 1
fi
tar xf ${APR_FILE}${TAR} && tar xf ${APR_UTIL_FILE}${TAR} && tar xf ${HTTPD_FILE}${TAR}
mv ${APR_FILE} ${HTTPD_FILE}/srclib/apr
mv ${APR_UTIL_FILE} ${HTTPD_FILE}/srclib/apr-util
cd ${HTTPD_FILE}
./configure --prefix=${INSTALL_DIR} --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=${MPM} &>/dev/null
if [ $? -eq 0 ];then
color "编译完成" 0
else
color "编译失败" 1
exit 1
fi
{ make -j ${CPUS} && make install &>/dev/null; }
if [ $? -eq 0 ];then
color "安装完成" 0
else
color "安装失败" 1
exit 1
fi
if id apache &>/dev/null;then
color "用户已存在,请删除" 1
exit 1
else
useradd -s /sbin/nologin -r apache
if [ $? -eq 0 ];then
color "用户创建成功" 0
else
color "用户创建失败" 1
exit 1
fi
fi
sed -i 's/daemon/apache/' ${INSTALL_DIR}/conf/httpd.conf
echo "PATH=$INSTALL_DIR/bin:$PATH" > /etc/profile.d/http24.sh
. /etc/profile.d/http24.sh
cat > /lib/systemd/system/httpd.service <<EOF
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
[Service]
Type=forking
ExecStart=${INSTALL_DIR}/bin/apachectl start
ExecReload=${INSTALL_DIR}/bin/apachectl graceful
ExecStop=${INSTALL_DIR}/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now httpd
if [ $? -eq 0 ];then
color "启动完成" 0
else
color "启动失败" 1
exit 1
fi
if `ps aux|gerp 80` &>/dev/null;then
color "服务运行正常" 0
else
color "服务运行失败" 1
exit 1
fi
}
main(){
install_package
install_httpd
}
main