apache安装shell脚本

该bash脚本用于自动化安装Apache服务器,包括下载指定版本的Apache源码,安装依赖包,编译和配置,并设置开机启动。脚本中包含了检查系统信息、下载wget、处理Apache安装包、创建软链接等步骤。
摘要由CSDN通过智能技术生成

#!/bin/bash

#Author WuZixiao

#Apache安装脚本

#2022-12-29 19:19:19

##################################################################

#定义变量

packages="gcc gcc-c++ cyrus-sasl-devel expat-devel libdb-devel openldap-devel apr* pcre* openssl* net-tools curl lynx"

apache_version="httpd-2.4.54"

src_dir="/usr/local/src/"

apache_dir="/usr/local/apache"

httpd_conf="/etc/init.d/httpd"

cpinfo="$(cat /proc/cpuinfo | grep processor | wc -l)"

#ip="$(ifconfig ens32 | awk '/netmask/{print $2}')"

ip="$(ip a | awk '/scope global/{print $2}' | cut -d "/" -f 1 | sed -n "1p")"

profile="/etc/profile"

apache_log="$(pwd)/apache_install.log"

httpd_ser="/usr/lib/systemd/system/httpd.service"

options="--prefix=/usr/local/apache --enable-so --enable-rewrite --enable-charset-lite --enable-cgi --enable-ssl --enable-mpms-shared=all --enable-mpm-with=event"

messages=("安装wget" "下载Apache安装包" "Apache安装包已存在,请解压" "解压Apache安装包" "安装依赖包" "预编译" "编译" "编译安装" "设置软链接" "修改主配置文件" "启动Apache")

colour=(31 34)

result=("失败!!!" "成功!!!")

##################################################################

#定义函数

delimiter(){#定义分隔符

echo -e "\033[32;1m※※※※※※※※※※※※※※※※※※※※※※※※※※\033[0m"

}

echoinfo(){#定义输入选项

echo -e "\033[35;1m#$1\033[0m"

}

echoresult(){#定义输出选项

echo -e "\033[$1;1m$2$3\033[0m"

}

if_test(){#if语句判断

if [ $? -eq 0 ];then

echoresult ${colour[1]} $1 ${result[1]}

else

echoresult ${colour[0]} $1 ${result[0]}

exit

fi

}

clean(){#清理缓存并重新加载缓存

yum clean all &>> $apache_log

yum makecache &>> $apache_log

sleep 2

}

wget_get(){#安装wget

delimiter | tee -a $apache_log

echo -e "\033[35;1m#安装wget \033[0m"

yum install -y wget &>> $apache_log

sleep 2

}

httpd_get(){#下载Apache安装包

num1="$(ls $(pwd) | grep httpd-2.4.54.tar.gz | wc -l)"

if [ $num1 -eq 0 ];then

delimiter | tee -a $apache_log

echoinfo ${messages[1]}

wget http://archive.apache.org/dist/httpd/$apache_version.tar.gz &>> $apache_log

elif [ $num1 -eq 1 ];then

#Apache安装包已存在,请解压

delimiter | tee -a $apache_log

echoinfo ${messages[2]} | tee -a $apache_log

fi

sleep 2

}

decom(){#解压Apache安装包

delimiter | tee -a $apache_log

echoinfo ${messages[3]} | tee -a $apache_log

tar -zxvf $apache_version.tar.gz -C $src_dir &>> $apache_log

sleep 2

if_test ${messages[3]} | tee -a $apache_log

}

insta(){#安装依赖包

delimiter | tee -a $apache_log

echoinfo ${messages[4]} | tee -a $apache_log

yum install -y $packages &>> $apache_log

sleep 2

if_test ${messages[4]} | tee -a $apache_log

}

preco(){#预编译

delimiter | tee -a $apache_log

echoinfo ${messages[5]} | tee -a $apache_log

cd $src_dir$apache_version/

./configure $options &>> $apache_log

sleep 2

if_test ${messages[5]} | tee -a $apache_log

}

comp(){#编译

delimiter | tee -a $apache_log

echoinfo ${messages[6]} | tee -a $apache_log

make -j $cpinfo &>> $apache_log

sleep 2

if_test ${messages[6]} | tee -a $apache_log

}

comins(){#编译安装

delimiter | tee -a $apache_log

echoinfo ${messages[7]} | tee -a $apache_log

make install &>> $apache_log

sleep 2

if_test ${messages[7]} | tee -a $apache_log

cd /root/ &>> $apache_log

}

ln_set(){#设置软链接

delimiter | tee -a $apache_log

echoinfo ${messages[8]}

ls $apache_dir &>> $apache_log

ln -s $apache_dir/bin/* /usr/local/bin/ &>> $apache_log

echo 'PATH=$PATH:/usr/local/apache/bin' &>> $profile | tee -a $apache_log

source $profile &>> $apache_log

sleep 2

if_test ${messages[8]} | tee -a $apache_log

httpd -v &>> $apache_log

}

start_set(){#启动Apache并设置开机自启

delimiter | tee -a $apache_log

echoinfo ${messages[9]} | tee -a $apache_log

cp $apache_dir/bin/apachectl $httpd_conf &>> $apache_log

chmod a+x $httpd_conf &>> $apache_log

sed -i 'N;2a#despriction:apache web server' $httpd_conf &>> $apache_log

sed -i 'N;2a#chkconfig:2345 11 88' $httpd_conf &>> $apache_log

sleep 2

if_test ${messages[9]} | tee -a $apache_log

delimiter | tee -a $apache_log

echoinfo ${messages[10]} | tee -a $apache_log

chkconfig --add httpd &>> $apache_log

chkconfig httpd on &>> $apache_log

chkconfig --list httpd &>> $apache_log

chkconfig --del httpd &>> $apache_log

sleep 2

echo "[Unit]

Desccription=httpd

After=network.target

[Service]

Type=forking

ExecStart=/usr/local/apache/bin/apachectl start

ExecReload=/usr/local/apache/bin/apachectl restart

ExecStop=/usr/local/apache/bin/apachectl stop

PrivateTmp=true

[Install]

WantedBy=multi-user.target" &> $httpd_ser | tee -a $apache_log

systemctl start httpd &>> $apache_log

sleep 2

systemctl enable httpd &>> $apache_log

sleep 2

systemctl status httpd &>> $apache_log

netstat -antup | grep 80 &>> $apache_log

ps -elf | grep httpd &>> $apache_log

echo 'Welcome to Apache!!!' &> $apache_dir/htdocs/index.html | tee -a $apache_log

curl -l -s $ip | tee -a $apache_log

if_test ${messages[10]} | tee -a $apache_log

sleep 2

rm -rf $src_dir$apache_version/ &>> $apache_log

}

##################################################################

echo "################BEGIN!!!$(date)################" | tee -a $apache_log

clean

wait

wget_get

wait

httpd_get

wait

decom

wait

insta

wait

preco

wait

comp

wait

comins

wait

ln_set

wait

num2="$(ls /etc/init.d/ | grep httpd | wc -l)"

if [ $num2 -eq 0 ];then

start_set

wait

elif [ $num2 -eq 1 ];then

rm -rf $httpd_conf &>> $apache_log

start_set

wait

else

echo -e "\033[41;37m Apache安装失败,请重新操作!!! \033[0m \n"

exit

fi

##################################################################

echo "################END!!!$(date)################" | tee -a $apache_log

##################################################################

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值