一键编译安装httpd-2.4.46(需要连接外网)

代码块注解

注意
1.代码中如果使用了grep 命令判断,不要使用 set -e,会报错,因为 set -e:遇到非0值直接退出代码,剩下的代码不在执行。
2.代码中如果想使用source /etc/init.d/functions,不要使用set -u会报错。两者冲突
代码变量设置
# 变量
URL=https://mirrors.tuna.tsinghua.edu.cn/apache//httpd/
FILE=httpd-2.4.46.tar.gz
INSTALL_DIR=/apps/httpd2
CPU=`lscpu | sed -rn ‘/^CPU\(s\)/s@.* ([0-9]+)@\1@p‘`
RESD="\E[$[RANDOM%7+31];1m"
END="\E[0m"
切割软件包
# 包名
PACKAGE=`echo $FILE |sed -nr ‘s/(.*[0-9.]+)\.[[:alpha:]]+.*/\1/p‘`
# 基名
BASEDIR=`basename $INSTALL_DIR`
# 后缀名
SUFFIX=`echo httpd-2.4.46.tar.bz2 |sed -nr ‘s/.*\.([^.]+)/\1/p‘`
需要的依赖包
cd /usr/local/src/
yum install -y gcc make apr-devel apr-util-devel pcre-devel openssl-devel redhat-rpm-config
关闭selinux和打开防火墙端口
sed -ri ‘s/^(SELINUX=).*/\1disabled/‘ /etc/sysconfig/selinux

#检查firewalld状态

firewall_status=`firewall-cmd --state`
if [ "$firewall_status" = "running" ];then
    echo "防火墙已启用,开放端口"
    firewall-cmd --permanent --add-service=http --add-service=https
    firewall-cmd --reload
fi
测试网络并下载包
ping -W1 -c 2 www.baidu.com &> /dev/null
if [ $? -eq 0 ]; then
    wget $URL$FILE
    case $SUFFIX in
    gz|bz2|xz|tar)
        tar -xvf $FILE
        ;;
    zip)
        unzip $FILE
        ;;
    *)
        echo "后缀识别不了,错误"
        exit 2
    esac
else
    echo -e  "\E[31m网络不通!!\E[0m"
    cp /root/$FLIE /usr/local/src/
fi
编译安装
cd $PACKAGE
./configure --prefix=$INSTALL_DIR --sysconfdir=/etc/$BASEDIR
make -j $CPU && make install
创建启动用户
grep -q apache /etc/passwd
if [ $? -eq 0 ];then
    echo "用户已经存在!"
else
    echo "没有用户,开始创建apache系统用户"
    sleep 5
    useradd -r -s /sbin/nologin -u 50 apache
fi
修改配置文件
sed -ri -e ‘s/^(User).*/\1 apache/‘ -e ‘s/^(Group).*/\1 apache/‘ /etc/$BASEDIR/httpd.conf
echo "PATH=$INSTALL_DIR/bin:$PATH" > /etc/profile.d/$BASEDIR.sh
source /etc/profile.d/$BASEDIR.sh
sed -ri ‘s#(^.*<h1>)(.*)(</h1>.*$)#\16.18 Go! \3 #‘ $INSTALL_DIR/htdocs/index.html
配置启动关闭system文件
cat > /lib/systemd/system/httpd.service <<EOF
[Unit]                                                                                                    
Description=The Apache HTTP Server
Wants=httpd-init.service
After=network.target remote-fs.target nss-lookup.target httpd-init.service
Documentation=man:httpd.service(8)

[Service]
Type=notify
Environment=LANG=C

ExecStart=${INSTALL_DIR}/bin/apachectl start
ExecReload=${INSTALL_DIR}/bin/apachectl graceful
ExecStop=${INSTALL_DIR}/bin/apachectl stop
# Send SIGWINCH for graceful stop
KillSignal=SIGWINCH
KillMode=mixed
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
启动并删除下载的软件源包
systemctl daemon-reload

systemctl start httpd.service
systemctl enable httpd.service

if [ $? -eq 0 ];then

   cd ..
   rm -rf $FILE $PACKAGE
完整脚本
[root@localhost ~]# cat httpds2.sh 
#!/bin/bash
#
#********************************************************************
#Author:		xuanlv
#QQ: 			360956175
#Date: 			2020-06-18
#FileName:		https2.sh
#URL: 			https://www.cnblogs.com/xuanlv-0413/
#Description:		The test script
#Copyright (C): 	2020 All rights reserved
#********************************************************************
#set -u
source /etc/init.d/functions
# 变量
URL=https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/
FILE=httpd-2.4.46.tar.gz
INSTALL_DIR=/apps/httpd2
CPU=`lscpu | sed -rn ‘/^CPU\(s\)/s@.* ([0-9]+)@\1@p‘`
RESD="\E[$[RANDOM%7+31];1m"
END="\E[0m"

# 包名
PACKAGE=`echo $FILE |sed -nr ‘s/(.*[0-9.]+)\.[[:alpha:]]+.*/\1/p‘`
# 基名
BASEDIR=`basename $INSTALL_DIR`
# 后缀名
SUFFIX=`echo httpd-2.4.43.tar.bz2 |sed -nr ‘s/.*\.([^.]+)/\1/p‘`

echo -en "$RESD----------install is httpd.service----------$END"
sleep 3

# 安装依赖包
cd /usr/local/src/
yum install -y gcc make apr-devel apr-util-devel pcre-devel openssl-devel redhat-rpm-config

# 关闭Selinux
sed -ri ‘s/^(SELINUX=).*/\1disabled/‘ /etc/sysconfig/selinux

#检查firewalld状态

firewall_status=`firewall-cmd --state`
if [ "$firewall_status" = "running" ];then
    echo "防火墙已启用,开放端口"
    firewall-cmd --permanent --add-service=http --add-service=https
    firewall-cmd --reload
fi

# 下载包
ping -W 1 -c 2 www.baidu.com &> /dev/null
if [ $? -eq 0 ]; then
    wget $URL$FILE
    case $SUFFIX in
    gz|bz2|xz|tar)
        tar -xvf $FILE
        ;;
    zip)
        unzip $FILE
        ;;
    *)
        echo "后缀识别不了,错误"
        exit 2
    esac
else
    echo -e  "\E[31m网络不通!!\E[0m"
    cp /root/$FLIE /usr/local/src/
fi
 编译安装
cd $PACKAGE
./configure --prefix=$INSTALL_DIR --sysconfdir=/etc/$BASEDIR
make -j $CPU && make install
if [ $? -ne 0 ];then
   echo "编译安装失败"
   exit 
fi

# 创建一个启动Apache用户
grep -q apache /etc/passwd
if [ $? -eq 0 ];then
    echo "用户已经存在!"
else
    echo "没有用户,开始创建apache系统用户"
    sleep 3
    useradd -r -s /sbin/nologin -u 50 apache
fi

# 修改配置文件用户
sed -ri -e ‘s/^(User).*/\1 apache/‘ -e ‘s/^(Group).*/\1 apache/‘ /etc/$BASEDIR/httpd.conf
echo "PATH=$INSTALL_DIR/bin:$PATH" > /etc/profile.d/$BASEDIR.sh
source /etc/profile.d/$BASEDIR.sh
sed -ri ‘s#(^.*<h1>)(.*)(</h1>.*$)#\16.18 Go! \3 #‘ $INSTALL_DIR/htdocs/index.html

# 配置启动关闭system文件
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 start httpd.service &> /dev/null && action "httpd 服务启动成功,httpd信息如下:" || { action "httpd 启动失败" false ;exit; } 
#systemctl start httpd.service
#systemctl enable httpd.service

if [ $? -eq 0 ];then

   cd ..
   rm -rf $FILE $PACKAGE
   echo -e "\E[1;32m安装成功!!!\E[0m"
else
   echo -en "$RESD----------httpd.service安装失败----------$END" 
fi

一键编译安装httpd-2.4.46(需要连接外网)

上一篇:js封装日期格式化函数


下一篇:webpack2项目引入ts后报错@ ./~/vue-style-loader!./~/css-loader!./~/vue-loader/lib/style-compiler?