shell练习---apache服务部署静态网站

#!/bin/bash
install_httpd(){
   yum install -y httpd &> /dev/null
   if [ $? -ne 0 ]
	 then
	 	cp ./rpm.repo /etc/yum.repos.d
	 	yum install -y httpd &> /dev/null
		if [ $? -ne 0 ]
		then
			echo "下载httpd服务失败,请检查原因"
			exit
		fi
	 else 
		 echo "httpd 下载成功"
	fi
}
makedir(){

if [ -d "/www" ];then
  echo "目录/www存在"
 else
   echo "目录/www不存在,现在开始创建"
	 mkdir /www
fi
if [ $? -ne 0 ]
then
	echo "创建/www有问题,请检查"
fi
}
modify_httpd_config(){
	listen=`grep -w "Listen 8080" /etc/httpd/conf/httpd.conf`
	if [ -z "$listen" ]
	then
		echo Listen 8080 >> /etc/httpd/conf/httpd.conf
		sed -i 's/\/var\/www\/html/\/www/g' /etc/httpd/conf/httpd.conf
	fi
	if [ $? -ne 0 ]
	then
		echo "修改/etc/httpd/conf/httpd.conf有问题,请检查"
	fi
}

firewall(){
service=`firewall-cmd --list-all|grep -w "http"`
port=`firewall-cmd --list-all | grep -w "8080"`
if [ -z "$service" ]
then
	firewall-cmd --zone=public --add-service=http --permanent
fi

if [ -z "$port" ]
then
firewall-cmd --permanent --add-port=8080/tcp
fi

firewall-cmd --reload	&>/dev/null 
if [ $? -ne 0 ]
then
	echo "防火墙添加服务失败,请检查"
fi
}
selinux(){
	semanage port -m -t http_port_t -p tcp 8080
	chcon -R -t httpd_sys_content_t /www
	if [ $? -ne 0 ]
	then
		echo "设置selinux有问题,请检查"
	fi
}

httpd_start(){
systemctl start httpd &>/dev/null
if [ $? -ne 0 ]
then
	echo "启动httpd失败,请检查"
else
	echo "启动httpd成功"
	systemctl enable httpd &>/dev/null
fi
}


rpm -qa | grep httpd &>/dev/null
if [ $? -ne 0 ]
then
	install_httpd
fi
makedir
modify_httpd_config
firewall
selinux
httpd_start

你可能感兴趣的:(shell,apache,linux,服务器,shell)