1.接收用户部署的服务名称
2.判断服务是否安装
已安装;自定义网站配置路径为/www;并创建共享目录和网页文件;重启服务
没有安装;安装对应的软件包
3.测试
判断服务是否成功运行;
已运行,访问网站
未运行,提示服务未启动,并显示自定义的配置文件内容
4.以上配置没有问题,请邮件告知我,并将脚本代码(代码文件)邮件发送我
read -p "请输入要部署的服务名称: " service_name
# 检查服务是否安装
if command -v $service_name >/dev/null 2>&1; then
echo "$service_name 已安装。"# 自定义配置路径和共享目录
config_path="/www"
shared_dir="/www/shared"
index_file="$shared_dir/index.html"echo "设置自定义网站配置路径为 $config_path。"
mkdir -p $shared_dir
echo "<h1>欢迎访问 $service_name 服务</h1>" > $index_file
echo "$service_name 主页文件已创建: $index_file"# 检查是否为 nginx 并设置默认配置
if [[ "$service_name" == "nginx" ]]; then
nginx_config="/etc/nginx/conf.d/default.conf"
echo "server {
listen 80;
server_name localhost;
root $shared_dir;
index index.html;
location / {
try_files \$uri \$uri/ =404;
}
}" > $nginx_config
echo "Nginx 配置已更新: $nginx_config"
fi# 重启服务
echo "重启 $service_name 服务..."
systemctl restart $service_nameelse
echo "$service_name 未安装。开始安装..."
if [[ "$service_name" == "nginx" ]]; then
yum install -y nginx || apt-get install -y nginx
else
yum install -y $service_name || apt-get install -y $service_name
fi
echo "$service_name 已成功安装。"
fi
# 测试服务是否运行
if systemctl is-active --quiet $service_name; then
echo "$service_name 服务正在运行。尝试访问网站..."
curl -I localhost
else
echo "$service_name 服务未启动。以下是配置文件的内容:"
if [[ "$service_name" == "nginx" ]]; then
cat $nginx_config
fi
fi# 检查以上步骤是否成功
if [[ $? -eq 0 ]]; then
echo "脚本执行成功,配置无问题。"
# 将脚本文件通过邮件发送
echo "发送脚本文件到指定邮箱..."
mail -s "Nginx 部署脚本" -a "$0" lxx1065372838@163.com <<< "Nginx 部署脚本已完成,请查收。"
else
echo "脚本执行失败,请检查相关日志。"
fi
检验