服务器作业4

[root@localhost day04]# vim 10.sh

[root@localhost day04]# cat 10.sh

#通过shell脚本分析部署nginx网络服务

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

read -p "服务名称:(nginx)" server

if [ $server != nginx ];then

echo "输入的不是nginx,脚本退出"

exit 1

fi

#2.判断服务是否安装

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

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

#systemctl status [服务名],可以查看服务是否安装,如果没有安装,

#会显示Unit [服务名].service could not be found.

a=`systemctl status $server`

if [ $a == "Unit $server.service could not be found." ];then

echo "Nginx服务未安装,开始安装..."

dnf install nginx -y

echo "Nginx服务安装完成。"

mkdir -p /www/shared

echo "创建共享目录 /www/shared"

echo "这是一个测试网页文件内容" > /www/shared/index.html

启动服务

systemctl start nginx

echo "Nginx服务已启动。"

else

echo "Nginx服务已安装。"

自定义网站配置路径为/www

mkdir -p /www

echo "已创建自定义网站配置路径 /www"

创建共享目录和网页文件

mkdir -p /www/shared

echo "创建共享目录 /www/shared"

echo "这是一个测试网页文件内容" > /www/shared/index.html

启动服务

systemctl restart nginx

echo "Nginx服务已重启。"

fi

#3.测试

#判断服务是否成功运行;

测试服务是否成功运行

#已运行,访问网站

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

测试服务是否成功运行

#systemctl is-active 服务名,判断服务是否运行,返回active是运行

isactive=` systemctl is-active $server`

if [ $isactive == active ]; then

echo "Nginx服务已成功运行,可通过浏览器访问网站。"

else

echo "Nginx服务未启动,以下是自定义的配置文件内容:"

cat /etc/nginx/nginx.conf

fi

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

#我的邮箱账号:lxx1065372838@163.com

mail_sub="nginx部署情况报告"

mial_body="nginx部署已经完成,详情如下:

$(if [ isactive != active ];then

echo "Nginx服务已安装但未成功启动,可查看配置文件内容排查问题。"

else

echo "Nginx服务已安装并配置完成,已成功运行。"

fi)

脚本代码:

$(cat 10.sh)"

echo $mail_body

echo "mail_body" \| mail -s "mail_sub" 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

自己的邮箱

命令格式:

[root@server ~]# echo 123 | mail -s "123" lxx1065372838@163.com

相关推荐
Christal_pyy12 分钟前
树莓派4基于Debian GNU/Linux 12 (Bookworm)添加多个静态ipv4网络
linux·网络·debian
csbDD1 小时前
2025年网络安全(黑客技术)三个月自学手册
linux·网络·python·安全·web安全
李狗蛋儿啊1 小时前
zero自动化框架搭建---Git安装详解
运维·git·自动化
小金的学习笔记2 小时前
如何在本地和服务器新建mysql用户和密码
运维·服务器·mysql
s_fox_2 小时前
nginx ngx_http_module(7) 指令详解
运维·nginx·http
EasyNVR2 小时前
EasyRTC智能硬件:实时畅联、沉浸互动、消音护航
运维·服务器·网络·安全·音视频·webrtc·p2p
CarryBest3 小时前
Jenkins 环境搭建---基于 Docker
运维·jenkins
若云止水3 小时前
Ubuntu 下 nginx-1.24.0 源码分析 - ngx_process_options
运维·nginx
s_fox_3 小时前
nginx ngx_http_module(9) 指令详解
运维·nginx·http
风口上的猪20153 小时前
thingboard告警信息格式美化
java·服务器·前端