🔧 优化install.sh的Nginx处理逻辑
改进 restart_nginx_safe(): - systemctl 改用 reload 代替 restart,避免不必要的服务中断 - 优先使用 reload,如未运行才使用 start - 添加端口占用检测提示 改进 ensure_nginx_installed(): - 不再强制自动安装 Nginx - 改为友好提示:支持用户使用已有反向代理(如宝塔/1Panel等) - 允许用户选择手动安装或跳过 增强宝塔面板支持: - 修复模式支持读取宝塔 Nginx 配置路径 - 支持备份宝塔配置文件 - 清理宝塔配置备份文件 改进服务检测: - Nginx 检测兼容多种运行方式(systemctl/进程/宝塔) - 更准确的进程检测逻辑 这些优化使脚本更灵活,兼容更多部署场景,减少端口冲突问题。 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
75
install.sh
75
install.sh
@@ -2297,13 +2297,20 @@ build_upload_tool() {
|
||||
restart_nginx_safe() {
|
||||
print_info "尝试重启/重载 Nginx..."
|
||||
|
||||
# 如果有systemd并存在nginx服务,优先使用
|
||||
# 如果有systemd并存在nginx服务,优先使用(不强制重启,避免80/443冲突)
|
||||
if command -v systemctl &> /dev/null && systemctl list-unit-files | grep -q "^nginx.service"; then
|
||||
if systemctl restart nginx 2>/dev/null; then
|
||||
print_success "已通过 systemctl 重启 Nginx"
|
||||
return 0
|
||||
if systemctl is-active --quiet nginx; then
|
||||
if systemctl reload nginx 2>/dev/null; then
|
||||
print_success "已通过 systemctl reload 重载 Nginx"
|
||||
return 0
|
||||
fi
|
||||
else
|
||||
print_warning "systemctl 重启失败,尝试使用 nginx 命令重载..."
|
||||
if systemctl start nginx 2>/dev/null; then
|
||||
print_success "已通过 systemctl start 启动 Nginx"
|
||||
return 0
|
||||
else
|
||||
print_warning "systemctl 启动失败,可能端口被占用或已有其他反代在跑"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
@@ -2354,21 +2361,8 @@ ensure_nginx_installed() {
|
||||
return 0
|
||||
fi
|
||||
|
||||
print_step "未检测到 Nginx,开始安装..."
|
||||
|
||||
case "$PKG_MANAGER" in
|
||||
apt) install_nginx_apt ;;
|
||||
yum) install_nginx_yum ;;
|
||||
dnf) install_nginx_dnf ;;
|
||||
zypper) install_nginx_zypper ;;
|
||||
*) print_error "无法自动安装Nginx,请手动安装"; return 1 ;;
|
||||
esac
|
||||
|
||||
# 安装后再次检测
|
||||
if ! command -v nginx &> /dev/null && [[ ! -x /www/server/nginx/sbin/nginx ]]; then
|
||||
print_error "Nginx安装失败,请手动安装"
|
||||
return 1
|
||||
fi
|
||||
print_warning "未检测到 Nginx。若你已有其它反向代理占用80/443,可跳过安装并手动配置;如需本脚本自动配置,请先安装Nginx后再运行。"
|
||||
return 0
|
||||
}
|
||||
|
||||
# 步骤1: 先配置HTTP Nginx(为SSL证书验证做准备)
|
||||
@@ -3762,11 +3756,8 @@ update_restart_services() {
|
||||
print_success "后端服务已重启"
|
||||
fi
|
||||
|
||||
# 重载Nginx
|
||||
if systemctl is-active --quiet nginx; then
|
||||
systemctl reload nginx
|
||||
print_success "Nginx已重载"
|
||||
fi
|
||||
# 重载Nginx(兼容宝塔/自管Nginx)
|
||||
restart_nginx_safe || print_warning "Nginx重载失败,请检查端口占用或手动重载"
|
||||
|
||||
echo ""
|
||||
}
|
||||
@@ -4128,15 +4119,22 @@ repair_load_existing_config() {
|
||||
BACKEND_PORT="40001"
|
||||
fi
|
||||
|
||||
# 检查现有nginx配置
|
||||
# 检查现有nginx配置(兼容宝塔/标准)
|
||||
local nginx_conf_path=""
|
||||
if [[ -f "/etc/nginx/sites-enabled/${PROJECT_NAME}.conf" ]]; then
|
||||
nginx_conf_path="/etc/nginx/sites-enabled/${PROJECT_NAME}.conf"
|
||||
elif [[ -f "/www/server/panel/vhost/nginx/${PROJECT_NAME}.conf" ]]; then
|
||||
nginx_conf_path="/www/server/panel/vhost/nginx/${PROJECT_NAME}.conf"
|
||||
fi
|
||||
|
||||
if [[ -n "$nginx_conf_path" ]]; then
|
||||
# 尝试从现有配置读取端口
|
||||
EXISTING_HTTP_PORT=$(grep "listen" "/etc/nginx/sites-enabled/${PROJECT_NAME}.conf" | grep -v "ssl" | head -1 | awk '{print $2}' | tr -d ';' || echo "80")
|
||||
EXISTING_HTTP_PORT=$(grep "listen" "$nginx_conf_path" | grep -v "ssl" | head -1 | awk '{print $2}' | tr -d ';' || echo "80")
|
||||
HTTP_PORT=${EXISTING_HTTP_PORT:-80}
|
||||
|
||||
# 检查是否有HTTPS配置
|
||||
if grep -q "listen.*ssl" "/etc/nginx/sites-enabled/${PROJECT_NAME}.conf"; then
|
||||
EXISTING_HTTPS_PORT=$(grep "listen.*ssl" "/etc/nginx/sites-enabled/${PROJECT_NAME}.conf" | head -1 | awk '{print $2}' | tr -d ';' || echo "443")
|
||||
if grep -q "listen.*ssl" "$nginx_conf_path"; then
|
||||
EXISTING_HTTPS_PORT=$(grep "listen.*ssl" "$nginx_conf_path" | head -1 | awk '{print $2}' | tr -d ';' || echo "443")
|
||||
HTTPS_PORT=${EXISTING_HTTPS_PORT:-443}
|
||||
SSL_METHOD="existing"
|
||||
print_success "检测到HTTPS配置,端口: $HTTPS_PORT"
|
||||
@@ -4146,7 +4144,7 @@ repair_load_existing_config() {
|
||||
fi
|
||||
|
||||
# 检查是否有域名
|
||||
SERVER_NAME=$(grep "server_name" "/etc/nginx/sites-enabled/${PROJECT_NAME}.conf" | head -1 | awk '{print $2}' | tr -d ';' || echo "_")
|
||||
SERVER_NAME=$(grep "server_name" "$nginx_conf_path" | head -1 | awk '{print $2}' | tr -d ';' || echo "_")
|
||||
if [[ "$SERVER_NAME" != "_" ]] && [[ "$SERVER_NAME" != "localhost" ]]; then
|
||||
DOMAIN="$SERVER_NAME"
|
||||
USE_DOMAIN=true
|
||||
@@ -4174,11 +4172,15 @@ repair_regenerate_nginx_config() {
|
||||
# 清理旧的备份文件(避免nginx读取到错误配置)
|
||||
rm -f /etc/nginx/sites-enabled/${PROJECT_NAME}.conf.backup.* 2>/dev/null || true
|
||||
rm -f /etc/nginx/sites-available/${PROJECT_NAME}.conf.backup.* 2>/dev/null || true
|
||||
rm -f /www/server/panel/vhost/nginx/${PROJECT_NAME}.conf.backup.* 2>/dev/null || true
|
||||
|
||||
# 备份当前配置到 /root/
|
||||
if [[ -f "/etc/nginx/sites-enabled/${PROJECT_NAME}.conf" ]]; then
|
||||
cp "/etc/nginx/sites-enabled/${PROJECT_NAME}.conf" "/root/nginx-backup-${PROJECT_NAME}.conf.$(date +%Y%m%d%H%M%S)"
|
||||
print_success "已备份现有配置到 /root/"
|
||||
elif [[ -f "/www/server/panel/vhost/nginx/${PROJECT_NAME}.conf" ]]; then
|
||||
cp "/www/server/panel/vhost/nginx/${PROJECT_NAME}.conf" "/root/nginx-backup-${PROJECT_NAME}.conf.$(date +%Y%m%d%H%M%S)"
|
||||
print_success "已备份宝塔配置到 /root/"
|
||||
fi
|
||||
|
||||
# 调用现有的configure_nginx函数
|
||||
@@ -4204,15 +4206,8 @@ repair_restart_services() {
|
||||
fi
|
||||
fi
|
||||
|
||||
# 重载Nginx
|
||||
if systemctl is-active --quiet nginx; then
|
||||
systemctl reload nginx
|
||||
print_success "Nginx已重载"
|
||||
else
|
||||
print_warning "Nginx未运行,尝试启动..."
|
||||
systemctl start nginx
|
||||
print_success "Nginx已启动"
|
||||
fi
|
||||
# 重载Nginx(兼容宝塔/自管Nginx)
|
||||
restart_nginx_safe || print_warning "Nginx未能启动/重载,请检查端口占用或手动重载"
|
||||
|
||||
echo ""
|
||||
}
|
||||
@@ -4232,7 +4227,7 @@ repair_verify_services() {
|
||||
fi
|
||||
|
||||
# 检查Nginx
|
||||
if systemctl is-active --quiet nginx; then
|
||||
if systemctl is-active --quiet nginx 2>/dev/null || pgrep -x nginx > /dev/null || [[ -x /www/server/nginx/sbin/nginx ]] && pgrep -f "/www/server/nginx/sbin/nginx" > /dev/null; then
|
||||
print_success "Nginx服务运行正常"
|
||||
else
|
||||
print_error "Nginx服务异常"
|
||||
|
||||
Reference in New Issue
Block a user