1.接收用户部署的服务名称
read -p "请输入要部署的服务器名称:" service_name
2.判断服务是否安装
已安装;自定义网站配置路径为/www;并创建共享目录和网页文件;重启服务
没有安装;安装对应的软件包
if command -v $service_name &>/dev/null; thenecho "$service_name 已安装"web_path="/www"mkdir -p $web_pathsub_config_file=" /etc/nginx/conf.d/new_domain.conf"cat > $sub_config_file <<EOFserver {listen 80;root $web_path;}
EOFtouch $web_path/index.htmlecho "这是一个测试内容" > $web_path/index.htmlsystemctl restart $service_nameecho "$service_name 服务已重启"
elseecho "$service_name 未安装,现在开始安装"mount /dev/sr0 /mntdnf install -y $service_nameif [ $? -eq 0 ];thenecho "$service_name 完成安装"web_path="/www"mkdir -p $web_pathsub_config_file=" /etc/nginx/conf.d/new_domain.conf"cat > $sub_config_file <<EOFserver {listen 80;root $web_path;}
EOFtouch $web_path/index.htmlecho "这是一个测试内容" > $web_path/index.htmlsystemctl restart $service_nameecho "$service_name 服务已重启"elseecho "$service_name 安装失败,请检查错误"exit 1fi
fi
3.测试
判断服务是否成功运行;
已运行,访问网站
未运行,提示服务未启动,并显示自定义的配置文件内容
if rpm -q nfs-utils &>/dev/null;thenecho "nfs已安装"
elseecho "nfs未安装,现在开始安装"mount /dev/sr0 /mntdnf install nfs-utils -y
fiif rpm -q nfs-utils &>/dev/null;thennfs_path="/pub"mkdir -p $nfs_pathtouch $nfs_path/{1..10}chmod o+w $nfs_pathecho " $nfs_path *(rw) " >> /etc/exportssystemctl restart nfs-server
fiif systemctl is-active nginx &> /dev/null;thenecho "$service_name服务已运行,可通过浏览器访问网站。"curl www.nginx.com
elseecho "$service_name服务未启动。"echo "以下为配置信息:"cat $web_path/index.html
fi
4.以上配置没有问题,请邮件告知我,并将脚本代码(代码文件)邮件发送我
我的邮箱账号:lxx1065372838@163.com
echo "脚本代码在附件" | s-nail -s "分析部署nginx网络服务的脚本代码" -a "$0" lxx1065372838@163.com
完整代码:
#!/bin/bash
read -p "请输入要部署的服务器名称:" service_name
if command -v $service_name &>/dev/null; thenecho "$service_name 已安装"web_path="/www"mkdir -p $web_pathsub_config_file=" /etc/nginx/conf.d/new_domain.conf"cat > $sub_config_file <<EOFserver {listen 80;root $web_path;}
EOFtouch $web_path/index.htmlecho "这是一个测试内容" > $web_path/index.htmlsystemctl restart $service_nameecho "$service_name 服务已重启"
elseecho "$service_name 未安装,现在开始安装"mount /dev/sr0 /mntdnf install -y $service_nameif [ $? -eq 0 ];thenecho "$service_name 完成安装"web_path="/www"mkdir -p $web_pathsub_config_file=" /etc/nginx/conf.d/new_domain.conf"cat > $sub_config_file <<EOFserver {listen 80;root $web_path;}
EOFtouch $web_path/index.htmlecho "这是一个测试内容" > $web_path/index.htmlsystemctl restart $service_nameecho "$service_name 服务已重启"elseecho "$service_name 安装失败,请检查错误"exit 1fi
fiif rpm -q nfs-utils &>/dev/null;thenecho "nfs已安装"
elseecho "nfs未安装,现在开始安装"mount /dev/sr0 /mntdnf install nfs-utils -y
fiif rpm -q nfs-utils &>/dev/null;thennfs_path="/pub"mkdir -p $nfs_pathtouch $nfs_path/{1..10}chmod o+w $nfs_pathecho " $nfs_path *(rw) " >> /etc/exportssystemctl restart nfs-server
fiif systemctl is-active nginx &> /dev/null;thenecho "$service_name服务已运行,可通过浏览器访问网站。"curl www.nginx.com
elseecho "$service_name服务未启动。"echo "以下为配置信息:"cat $web_path/index.html
fi
echo "脚本代码在附件" | s-nail -s "分析部署nginx网络服务的脚本代码" -a "$0" lxx1065372838@163.com