当前位置: 首页 > news >正文

服务器作业4

[root@localhost ~]# vim 11.sh

#关闭防火墙

systemctl stop firewalld
setenforce  0


#1.接收用户部署的服务名称
read -p "服务名称:(nginx)" server_name
if [ $server_name != nginx ];then
    echo "输入的不是nginx,脚本退出"
    exit 1
fi


# 判断是否安装了指定服务

#command -v和rpm -q 可能无法精确识别服务是否安装,可能是已经安装识别为未安装

#可能未安装识别为已安装

#上面两个命令应该是可以识别,但不知道为什么无法像which一样精确识别

#systemctl status 服务名:当服务未安装是,返回会显示Unit [服务名].service could not be found.

#a=`systemctl status $server`
#if [ $a ==  "Unit $server.service could not be found." ];

#这个命令可能出现“==”号附近会报错

#或者无法精确识别
#if `command -v $service_name &>/dev/null`; then
#if `rpm -q  $service_name &>/dev/null`; then

#&>:是一种重定向操作符。它将标准输出(stdout)和标准错误输出(stderr)合并到一起进行重定向

#which nginx &>/dev/null:这个命令主要用于检查系统中是否能找到nginx命令,并且在检查过程中不显示任何与查找相关的输出内容,脚本可以根据这个命令的退出状态码(如果能找到nginx命令,退出状态码为 0;否则为非 0)来进一步判断nginx是否已安装或者在系统路径中有正确的配置

if which nginx &>/dev/null; then

    #已安装;自定义网站配置路径为/www;并创建共享目录和网页文件;重启服务
        echo "nginx已安装"
        #web_path配置路径
        web_path="/www"
        mkdir -p $web_path
        #配置文件路径
        config_path="/etc/nginx/conf.d"
        #配置文件
        touch $config_path/new_test.conf
        config_path_file="$config_path/new_test.conf"
#配置文件配置

#通过

#cat << EOF > 文件名

#内容

#EOF

#的方式向文件中写入内容
cat << EOF > $config_path_file
server{
        listen 80;
        root $web_path;

}

EOF
        # 创建共享目录和网页文件
        #共享目录
        shared_path="$web_path/shared"
        mkdir -p $shared_path
        #网页文件
        #往网页文件里写入内容,测试用
        echo "this is a test" > $web_path/index.html
        #重启nginx服务
        systemctl restart $server_name
else
    echo "nginx未安装"
    mount /dev/sr0 /mnt
        dnf install nginx -y
        echo "nginx安装成功"
        #web_path配置路径
        web_path="/www"
        mkdir -p $web_path
        #配置文件路径
        config_path="/etc/nginx/conf.d"
        #配置文件
        touch $config_path/new_test.conf
        config_path_file="$config_path/new_test.conf"
#配置文件配置
cat << EOF > $config_path_file
server{
        listen 80;
        root $web_path;

}

EOF
        # 创建共享目录和网页文件
        #共享目录
        shared_path="$web_path/shared"
        mkdir -p $shared_path
        #网页文件
        #往网页文件里写入内容,测试用
        echo "this is a test" > $web_path/index.html
        #重启nginx服务
        systemctl restart $server_name


fi
# 测试服务是否成功运行

#systemctl is-active 服务名

#查看服务是否运行,如果运行返回“active”
live=`systemctl is-active $server_name`
if [ $live == active ];then
    echo "服务成功运行,下面是网站内容"
#已运行,访问网站
    curl `hostname -I`
else
#未运行,提示服务未启动,并显示自定义的配置文件内容
    echo "服务未启动"
    echo "下面是nginx主配置文件内容"

#cat 文件名

#查看文件内容
    cat $config_path_file
fi

echo "我是柏益伟" | mail -s "作业4" -a 11.sh 15380914067@163.com

邮箱配置:
#挂载
[root@localhost ~]# mount /dev/sr0 /mnt
#安装s-nail邮箱服务
[root@localhost ~]# dnf install s-nail -y
#在配置文件的末尾添加配置
[root@localhost ~]# vim /etc/s-nail.rc 

############################
#网易邮箱@163.com
#smtp-auth-password要开启网易邮箱的IMAP/SMTP服务,得到的授权码
set  from=15380914067@163.com
set  smtp=smtp.163.com
set  smtp-auth-user=15380914067@163.com
set  smtp-auth-password=JBntypapSgHPrMX5
set  smtp-auth=login
#                                         自己的邮箱

命令格式:

 #-a  文件名;把文件发送通过邮箱发送

echo "我是柏益伟" | mail -s "作业4" -a 11.sh 15380914067@163.com

http://www.lryc.cn/news/489384.html

相关文章:

  • IOC控制反转---相关的介绍和6大注解解读(类注解+方法注解)
  • SpringBoot(8)-任务
  • 【机器学习】如何配置anaconda环境(无脑版)
  • java 可以跨平台的原因是什么?
  • Solana应用开发常见技术栈
  • npm | Yarn | pnpm Node.js包管理器比较与安装
  • Linux下编译MFEM
  • 【团购核销】抖音生活服务商家应用快速接入②——商家授权
  • django宠物服务管理系统
  • vue2中使用three.js步骤
  • 部落商城App开发笔记 2024.11.21 实现进入app就是短视频
  • 解决.DS_Store 在项目一致无法排除,.gitignore里也不生效
  • MySQL-关键字执行顺序
  • 极客时间《Redis核心技术与实战》开篇词 知识点总结
  • TCP并发服务器
  • Debug-031-近期功能实现小结
  • Consumer Group
  • .NET架构师学习大纲
  • 【代码随想录】贪心
  • Harmony鸿蒙类似与Android中broadcast广播的api使用及释义
  • openGauss 6.0.0主备部署(企业版)
  • 【机器学习】聚类算法原理详解
  • Ubuntu20.04从零安装IsaacSim/IsaacLab
  • 基于Java Springboot大学校园旧物捐赠网站
  • 【Java 集合】Collections 空列表细节处理
  • 大数据实验4-HBase
  • deepin系统下载pnpm cnpm等报错
  • #Js篇:JSON.stringify 和 JSON.parse用法和传参
  • c#通过网上AI大模型实现对话功能
  • pymysql模块