diff --git a/install.sh b/install.sh index ba2dd7b..5a9fcda 100644 --- a/install.sh +++ b/install.sh @@ -2293,6 +2293,44 @@ build_upload_tool() { # Nginx配置 - 分步骤执行 ################################################################################ +# 安全重启/重载Nginx(带回退) +restart_nginx_safe() { + print_info "尝试重启/重载 Nginx..." + + # 如果有systemd并存在nginx服务,优先使用 + 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 + else + print_warning "systemctl 重启失败,尝试使用 nginx 命令重载..." + fi + fi + + # 直接使用nginx命令 + if command -v nginx &> /dev/null; then + if ! nginx -t 2>/dev/null; then + print_error "nginx -t 配置测试失败,请检查配置" + nginx -t 2>&1 || true + return 1 + fi + + if nginx -s reload 2>/dev/null; then + print_success "已使用 nginx -s reload 重载配置" + return 0 + fi + + # reload失败,尝试直接启动 + if nginx 2>/dev/null; then + print_success "已直接启动 Nginx" + return 0 + fi + fi + + print_error "未能成功启动/重载 Nginx,请手动检查(端口占用/安装状态)" + return 1 +} + # 步骤1: 先配置HTTP Nginx(为SSL证书验证做准备) configure_nginx_http_first() { print_step "配置基础HTTP Nginx(用于SSL证书验证)..." @@ -2466,8 +2504,8 @@ EOF systemctl reload nginx 2>/dev/null && print_info "已使用systemctl重载配置" || true fi else - # 标准Nginx:重启 - systemctl restart nginx + # 标准Nginx:重启(带回退) + restart_nginx_safe || return 1 fi @@ -2485,7 +2523,12 @@ EOF fi else # 标准Nginx:使用systemctl检查 - if ! systemctl is-active --quiet nginx; then + if command -v systemctl &> /dev/null && systemctl list-unit-files | grep -q "^nginx.service"; then + if ! systemctl is-active --quiet nginx; then + print_error "Nginx启动失败" + return 1 + fi + elif ! pgrep -x nginx > /dev/null; then print_error "Nginx启动失败" return 1 fi @@ -2553,8 +2596,8 @@ configure_nginx_final() { systemctl reload nginx 2>/dev/null && print_info "已使用systemctl重载配置" || true fi else - # 标准Nginx:重载 - systemctl reload nginx + # 标准Nginx:重载(带回退) + restart_nginx_safe || return 1 fi @@ -2582,7 +2625,7 @@ configure_nginx() { nginx -t # 重启nginx - systemctl restart nginx + restart_nginx_safe || return 1 print_success "Nginx配置完成" echo ""