Files
common-server-public/nginx/updateConfig.sh
wsl-d4 722e15b458 827
2025-08-26 16:44:41 +08:00

36 lines
1.1 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# 检查并创建nginx用户
if ! id -u nginx >/dev/null 2>&1; then
echo "创建nginx用户..."
sudo useradd -r -s /sbin/nologin nginx
fi
# 创建 conf.d 目录(如果不存在)
sudo mkdir -p /etc/nginx/conf.d
# 备份并覆盖 nginx.conf 文件
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
sudo cp nginx.conf /etc/nginx/
# 备份并覆盖 conf.d 目录下的所有 .conf 文件
for conf in conf.d/*.conf; do
filename=$(basename "$conf")
sudo cp "/etc/nginx/conf.d/$filename" "/etc/nginx/conf.d/${filename}.bak" 2>/dev/null || true
sudo cp "$conf" /etc/nginx/conf.d/
done
# 测试并重载配置
if sudo nginx -t; then
sudo systemctl reload nginx
echo "配置已生效HTTPS已启用"
else
echo "配置测试失败,已恢复备份"
# 恢复 nginx.conf 备份
sudo cp /etc/nginx/nginx.conf.bak /etc/nginx/nginx.conf
# 恢复 conf.d 目录下的所有 .conf 文件备份
for conf in conf.d/*.conf; do
filename=$(basename "$conf")
sudo mv -f "/etc/nginx/conf.d/${filename}.bak" "/etc/nginx/conf.d/$filename" 2>/dev/null || true
done
fi