要求

1.接收用户部署的服务名称

2.判断服务是否安装

​ 已安装;自定义网站配置路径为/www;并创建共享目录和网页文件;重启服务

​ 没有安装;安装对应的软件包

3.测试

判断服务是否成功运行;

​ 已运行,访问网站

​ 未运行,提示服务未启动,并显示自定义的配置文件内容

4.以上配置没有问题,请邮件告知我,并将脚本代码(代码文件)邮件发送我

1.进行挂载 mount /dev/sr0 /mnt

2.防火墙关闭   systemctl stop firewalld

                         setenforce  0

3.接收用户部署的服务名称进行配置  

                   vim    1.sh

#!/bin/bash
read -p "请输入要部署的服务名称: " service_name
if [ "$service_name" != "nginx" ]; then(判断f服务名称是否为nginx)
    echo "请输入正确的服务名称(nginx)"
    exit 1    
fi

4.判断服务是否安装

if which nginx &>/dev/null; then
    echo "Nginx已安装"
else
    echo "Nginx未安装,将开始安装nginx"
    dnf install nginx -y(未安装进行安装)

fi

5.已安装;自定义网站配置路径为/www;并创建网页文件配置

if which nginx &>/dev/null; then
        web_path="/www"
        mkdir -p $web_path
        sub_config_file="/etc/nginx/conf.d/new_domain.conf"(修改配置文件中路径)
        cat > $sub_config_file  <<EOF
        server {
                listen       80;
        root   $web_path;  
        }
EOF   
    
        touch $web_path/index.html
    echo "this is test" > $web_path/index.html
    systemctl restart nginx
fi

6. 创建共享目录(nfs);重启服务


if rpm -q nfs-utils &>/dev/null; then(使用rpm命令检查nfs是否已经安装)
    echo "nfs已安装" 
else
    echo "nfs未安装,将开始安装nfs"
    dnf install nfs-utils  -y(未安装进行安装)
fi

if rpm -q nfs-utils &>/dev/null; then(建立共享文件目录,设置权限)
        nfs_path="/pub"
        mkdir  -p $nfs_path
        touch $nfs_path$/{1..10}
        chmod o+w $nfs_path
        echo " $nfs_path     *(rw) "  >> /etc/exports  
        systemctl restart  nfs-server
fi
 

7.判断服务是否成功运行;已运行,访问网站;未运行,提示服务未启动,并显示自定义的配置文件内容

if systemctl is-active nginx &>/dev/null; then
    echo "Nginx服务已运行,可通过浏览器访问网站"
    IP=`ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | head -n 1`
    curl_code=`curl -o /dev/null -s -w "%{http_code}\n" $IP`
    if [ $curl_code -eq 200 ];then
        echo "访问网站成功"
    else
        echo “访问出现了问题”
    fi
else
    echo "Nginx服务未启动"
    echo "以下是自定义配置文件内容:"
    cat $web_path/index.html
fi

8.邮箱配置  dnf install s-nail -y

         vim /etc/s-nail.rc

(最后面加上以下的代码)

set from=(自己的邮箱)
set smtp=smtp.163.com
set smtp-auth-user=(自己的邮箱)
set smtp-auth-password=(自己的访问码)
set smtp-auth=logi

9.在1.sh最后加上

echo "脚本代码在附件" | s-nail -s "分析部署nginx网络服务的脚本代码" -a "$0"                    @ 163.com(邮箱)

10.验证是否成功

Logo

助力广东及东莞地区开发者,代码托管、在线学习与竞赛、技术交流与分享、资源共享、职业发展,成为松山湖开发者首选的工作与学习平台

更多推荐