您现在的位置是:首页 >技术教程 >Shell脚本完成web服务器的建设网站首页技术教程
Shell脚本完成web服务器的建设
一,要求
1,安装httpd软件
2,定义防火墙规则
3,设置selinux为强制模式
4,web服务器的端口为8080
5,web服务器的内容目录为 /www/8080
6,所有操作开机有效
7,所有操作必须进行判断
8,在合适的实际输出合适的信息,如果有日志就更好了
二,具体实施
vim_repo() {
touch /etc/yum.repos.d/rpm.repo
echo -e "[baseos]
name=baseos
baseurl=/mnt/BaseOS
gpgcheck=0
[appstream]
name=appstrean
baseurl=/mnt/AppStream
gpgcheck=0" > /etc/yum.repos.d/rpm.repo
}
repod() {
yum=`yum repolist all | grep enabled | wc -l`
if [ $yum -eq 0 ]
then
echo "没有仓库,mount"
vim_repo
mount /dev/sr0 /mnt
else
echo "有仓库,并挂载"
mount /dev0 /mnt
fi
}
httpd_whether_installed() {
httpd_server=`rpm -qa | grep httpd | wc -l`
if [ $httpd_server -eq 0 ];then
echo "Httpd is not exists and insatll"
yum install httpd
else
echo "Httpd is exists"
fi
}
httpd_whether_enabled () {
httpd_server=`netstat -lntup | grep -w 80 | wc -l`
if [ $httpd_server -eq 0 ];then
echo "httpd is not active and start httpd"
systemctl start httpd
else
echo "httpd is active and make httpd enable"
systemctl enable httpd
fi
}
open_firewall_server() {
firewall_listall=`firewall-cmd --list-all | grep http | wc -l`
if [ $firewall_listall -eq 0 ];then
echo "未开放http服务,并开始放行http服务,并重启"
firewall-cmd --add-service=http --permanent
firewall-cmd --reload
else
echo "已放行http服务"
fi
}
set_selinux() {
while :
do
getenforce=`getenforce`
case $getenforce in
"Enforcing")
echo "SElinux为强制模式"
break
;;
"Permissive")
echo "SElinux为宽容模式,正在换强制模式"
setenforce 1
getenforce
break
;;
*)
echo "SElinux关闭"
break
esac
done
}
open_firewall_port() {
firewall_port=`firewall-cmd --list-ports | wc -l`
if [ $firewall_port -eq 0 ];then
echo "未开放8080端口,正在开启并重启防火墙"
firewall-cmd --zone=public --add-port=8080/tcp
firewall-cmd --reload
else
echo "8080端口已经被放行!"
fi
}
httpd_conf() {
echo "正在配置httpd服务配置文件"
echo -e " <directory /www>
allowoverride
require all granted
</directory>
listen 8080
<virtualhost 0.0.0.0:8080>
documentroot /www/8080
servername 192.168.80.131
</virtualhost>" >/etc/httpd/conf.d/host.conf
echo "启动httpd并检查状态"
systemctl restart httpd
}
semanage_port() {
#取出80端口的类型
se_port=`semanage port -l | grep -w 80 | tr -s " " | cut -d " " -f1`
echo "正在为selinux添加新端口"
#更改 SELinux 类型端口 8080 使其与端口 80 匹配
semanage port -a -t $se_port -p tcp 8080
echo "添加端口完成,并查看修改对象安全上下文"
se_content=`ls /var/www/html -Z | tr -s " " | cut -d ":" -f3`
chcon -t $se_content /www/8080/index.html
echo "修改成功,并检查/www/8080的安全上下文"
ls /www/8080/index.html -lZ
echo "检查完毕,正在重启httpd服务并测试服务能否访问"
systemctl restart httpd
wget 192.168.80.131:8080
}
repod
httpd_whether_installed
httpd_whether_enabled
open_firewall_server
set_selinux
open_firewall_port
httpd_conf
semanage_port
三,结果展示