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

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

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

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

[root@localhost xzy]# vim 1.sh
[root@localhost xzy]# chmod +x 1.sh
[root@localhost xzy]# ./1.sh

2.判断服务是否安装

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

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

#!/bin/bash# 1. 接收用户部署的服务名称
read -p "请输入您部署的服务名称: " service_name
service_name=$(echo $service_name | tr -d '\b')  # 去除退格键# 判断 Nginx 是否已安装
if command -v nginx &> /dev/null
thenecho "Nginx 已经安装。"# 2. 自定义网站配置echo "自定义网站配置路径为 /www。"# 创建共享目录和网页文件sudo mkdir -p /www/htmlif [ $? -eq 0 ]; thenecho "共享目录 /www/html 创建成功。"elseecho "创建共享目录 /www/html 失败,请检查权限。"exit 1fisudo echo "<html><head><title>$service_name</title></head><body><h1>Welcome to $service_name</h1></body></html>" > /www/html/index.htmlif [ $? -eq 0 ]; thenecho "网页文件 /www/html/index.html 创建成功。"elseecho "创建网页文件 /www/html/index.html 失败,请检查权限。"exit 1fi# 确保配置目录存在if [ -d /etc/nginx/sites-available ]; thenecho "配置目录 /etc/nginx/sites-available 已存在。"elsesudo mkdir -p /etc/nginx/sites-availableif [ $? -ne 0 ]; thenecho "创建配置目录 /etc/nginx/sites-available 失败,请检查权限。"exit 1fiecho "配置目录 /etc/nginx/sites-available 创建成功。"fiif [ -d /etc/nginx/sites-enabled ]; thenecho "配置目录 /etc/nginx/sites-enabled 已存在。"elsesudo mkdir -p /etc/nginx/sites-enabledif [ $? -ne 0 ]; thenecho "创建配置目录 /etc/nginx/sites-enabled 失败,请检查权限。"exit 1fiecho "配置目录 /etc/nginx/sites-enabled 创建成功。"fi# 配置 Nginx 以使用自定义路径sudo tee /etc/nginx/sites-available/$service_name.conf <<EOF
server {listen 80;server_name localhost;location / {root /www/html;index index.html;}
}
EOFif [ $? -eq 0 ]; thenecho "Nginx 配置文件 /etc/nginx/sites-available/$service_name.conf 创建成功。"elseecho "创建 Nginx 配置文件 /etc/nginx/sites-available/$service_name.conf 失败,请检查路径。"exit 1fi# 创建符号链接使配置生效sudo ln -sf /etc/nginx/sites-available/$service_name.conf /etc/nginx/sites-enabled/if [ $? -eq 0 ]; thenecho "符号链接创建成功。"elseecho "创建符号链接失败,请检查路径。"exit 1fi# 重启 Nginx 服务sudo systemctl restart nginx.serviceecho "已重启 Nginx 服务。"# 3. 测试 Nginx 服务是否成功运行if sudo systemctl status nginx.service | grep -q "active (running)"; thenecho "Nginx 服务已成功启动。"echo "可以通过浏览器访问地址 http://localhost 来查看您的网站。"elseecho "Nginx 服务没有成功启动。"echo "----------- 配置文件内容如下 -----------"sudo cat /etc/nginx/sites-available/$service_name.conffi
else# 2. 没有安装 Nginx,安装对应的软件包echo "Nginx 未安装,开始安装 Nginx。"sudo yum install nginx -y # 判断安装是否成功if command -v nginx &> /dev/nullthenecho "Nginx 安装完成。"# 创建共享目录和网页文件sudo mkdir -p /www/htmlif [ $? -eq 0 ]; thenecho "共享目录 /www/html 创建成功。"elseecho "创建共享目录 /www/html 失败,请检查权限。"exit 1fisudo echo "<html><head><title>$service_name</title></head><body><h1>Welcome to $service_name</h1></body></html>" > /www/html/index.htmlif [ $? -eq 0 ]; thenecho "网页文件 /www/html/index.html 创建成功。"elseecho "创建网页文件 /www/html/index.html 失败,请检查权限。"exit 1fi# 确保配置目录存在if [ -d /etc/nginx/sites-available ]; thenecho "配置目录 /etc/nginx/sites-available 已存在。"elsesudo mkdir -p /etc/nginx/sites-availableif [ $? -ne 0 ]; thenecho "创建配置目录 /etc/nginx/sites-available 失败,请检查权限。"exit 1fiecho "配置目录 /etc/nginx/sites-available 创建成功。"fiif [ -d /etc/nginx/sites-enabled ]; thenecho "配置目录 /etc/nginx/sites-enabled 已存在。"elsesudo mkdir -p /etc/nginx/sites-enabledif [ $? -ne 0 ]; thenecho "创建配置目录 /etc/nginx/sites-enabled 失败,请检查权限。"exit 1fiecho "配置目录 /etc/nginx/sites-enabled 创建成功。"fi# 自定义网站配置sudo tee /etc/nginx/sites-available/$service_name.conf <<EOF
server {listen 80;server_name localhost;location / {root /www/html;index index.html;}
}
EOFif [ $? -eq 0 ]; thenecho "Nginx 配置文件 /etc/nginx/sites-available/$service_name.conf 创建成功。"elseecho "创建 Nginx 配置文件 /etc/nginx/sites-available/$service_name.conf 失败,请检查路径。"exit 1fi# 创建符号链接使配置生效sudo ln -sf /etc/nginx/sites-available/$service_name.conf /etc/nginx/sites-enabled/if [ $? -eq 0 ]; thenecho "符号链接创建成功。"elseecho "创建符号链接失败,请检查路径。"exit 1fi# 启动 Nginx 服务sudo systemctl start nginx.serviceecho "已启动 Nginx 服务。"# 3. 测试 Nginx 服务是否成功运行if sudo systemctl status nginx.service | grep -q "active (running)"; thenecho "Nginx 服务已成功启动。"echo "可以通过浏览器访问地址 http://localhost 来查看您的网站。"elseecho "Nginx 服务没有成功启动。"echo "----------- 配置文件内容如下 -----------"sudo cat /etc/nginx/sites-available/$service_name.conffielseecho "Nginx 安装失败,请检查您的网络连接和 YUM 源配置。"fi
fi

3.测试

判断服务是否成功运行;

已运行,访问网站

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

在这里插入图片描述

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

相关文章:

  • Java基础——继承和多态
  • 长江存储嵌入式面试题及参考答案
  • WordPress设置自动更新CSS版本号
  • 【Qt聊天室】客户端实现总结
  • 服务器数据恢复—raid5阵列故障导致上层系统分区无法识别的数据恢复案例
  • mysql bin log分析
  • 深入理解 Vue 3 中的 emit
  • #lwIP 的 Raw API 使用指南
  • Elasticsearch开启认证及kibana密码登陆
  • 【论文阅读】Large Language Models for Equivalent Mutant Detection: How Far Are We?
  • vue2 面试题带答案,万字总结
  • git的常用用法(最简精华版)
  • 哥德巴赫猜想渐行渐远
  • Spring Boot应用开发实战:构建高效、可维护的Web应用
  • keep-alive多级页面缓存实现
  • ks 小程序sig3
  • 图论之构造完全图
  • RDD触发算子:一些常用的触发算子(count、foreach、saveAsTextFile、first)
  • 搭建RAGFlow
  • css中的box-sizing,记录
  • 使用useCallback引发对闭包的理解
  • gvim添加至右键、永久修改配置、放大缩小快捷键、ctrl + c ctrl +v 直接复制粘贴、右键和还原以前版本(V)冲突
  • 腾讯云-COS
  • 蓝桥杯每日真题 - 第16天
  • 基因组之全局互作热图可视化
  • 基于Lora通讯加STM32空气质量检测WIFI通讯
  • STM32 极速入门第一天基础拓展 驱动i2c屏幕 ( 使用PlatformIO开发STM32单片机 )
  • 【WPF】Prism学习(五)
  • RabbitMQ的基本概念和入门
  • Shell脚本6 -- 条件判断if