From 0f53d0638c857002a75c8d758245f758a43a32f0 Mon Sep 17 00:00:00 2001 From: WanWanYun Date: Thu, 13 Nov 2025 09:32:20 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B7=BB=E5=8A=A0=E5=AE=9D=E5=A1=94?= =?UTF-8?q?=E9=9D=A2=E6=9D=BF=EF=BC=88BT=20Panel=EF=BC=89Nginx=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题: - 用户使用宝塔面板,Nginx配置路径为 /www/server/nginx/ - 标准路径 /etc/nginx/ 不适用于宝塔 - systemctl restart nginx 导致宝塔Nginx启动失败 宝塔特征: - 配置文件:/www/server/nginx/conf/nginx.conf - 虚拟主机:/www/server/panel/vhost/nginx/*.conf - 需要使用reload而不是restart 修复: 1. 添加宝塔面板检测逻辑(检查 /www/server/nginx 目录) 2. 使用宝塔专用配置目录:/www/server/panel/vhost/nginx/ 3. 宝塔环境使用reload,避免影响其他站点 4. 配置文件优先级: - 宝塔面板 > Debian/Ubuntu > CentOS/RHEL 5. 所有三个Nginx配置函数都已更新 修复的函数: - configure_nginx_http_first() - 检测宝塔并使用专用目录 - configure_nginx_http() - 同上 - configure_nginx_https() - 同上 预期效果: - 宝塔用户:配置写入 /www/server/panel/vhost/nginx/wanwanyun.conf - 宝塔用户:使用 systemctl reload nginx - 标准Nginx:行为不变 相关错误: Job for nginx.service failed because the control process exited with error code --- install.sh | 71 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 56 insertions(+), 15 deletions(-) diff --git a/install.sh b/install.sh index 8b0deca..df50125 100644 --- a/install.sh +++ b/install.sh @@ -1962,11 +1962,22 @@ configure_nginx_http_first() { local server_name="${DOMAIN:-_}" # 检测Nginx配置目录结构并创建必要的目录 - if [[ -d /etc/nginx/sites-available ]] || [[ "$PKG_MANAGER" == "apt" ]]; then + if [[ -d /www/server/nginx ]]; then + # 宝塔面板 (BT Panel) + NGINX_CONF_DIR="/www/server/panel/vhost/nginx" + NGINX_ENABLED_DIR="" + USE_SYMLINK=false + IS_BT_PANEL=true + + # 确保目录存在 + mkdir -p ${NGINX_CONF_DIR} + print_info "检测到宝塔面板,使用宝塔Nginx配置目录" + elif [[ -d /etc/nginx/sites-available ]] || [[ "$PKG_MANAGER" == "apt" ]]; then # Debian/Ubuntu: 使用sites-available NGINX_CONF_DIR="/etc/nginx/sites-available" NGINX_ENABLED_DIR="/etc/nginx/sites-enabled" USE_SYMLINK=true + IS_BT_PANEL=false # 确保目录存在 mkdir -p ${NGINX_CONF_DIR} @@ -1976,6 +1987,7 @@ configure_nginx_http_first() { NGINX_CONF_DIR="/etc/nginx/conf.d" NGINX_ENABLED_DIR="" USE_SYMLINK=false + IS_BT_PANEL=false # 确保目录存在 mkdir -p ${NGINX_CONF_DIR} @@ -2042,10 +2054,11 @@ EOF ln -sf ${NGINX_CONF_DIR}/${PROJECT_NAME}.conf ${NGINX_ENABLED_DIR}/${PROJECT_NAME}.conf # 删除默认站点 rm -f ${NGINX_ENABLED_DIR}/default - else - # CentOS/RHEL: conf.d中的.conf文件会自动加载,删除默认配置 + elif [[ "$IS_BT_PANEL" != "true" ]]; then + # CentOS/RHEL (非宝塔): conf.d中的.conf文件会自动加载 rm -f /etc/nginx/conf.d/default.conf fi + # 宝塔面板:配置文件已自动包含,无需额外操作 # 测试nginx配置 if ! nginx -t; then @@ -2053,8 +2066,20 @@ EOF return 1 fi - # 启动Nginx - systemctl restart nginx + # 启动或重载Nginx + if [[ "$IS_BT_PANEL" == "true" ]]; then + # 宝塔面板:使用reload而不是restart + if systemctl is-active --quiet nginx; then + systemctl reload nginx + print_info "已重载宝塔Nginx配置" + else + systemctl start nginx + print_info "已启动宝塔Nginx" + fi + else + # 标准Nginx:重启 + systemctl restart nginx + fi if ! systemctl is-active --quiet nginx; then print_error "Nginx启动失败" return 1 @@ -2132,17 +2157,25 @@ configure_nginx_http() { local server_name="${DOMAIN:-_}" # 检测Nginx配置目录结构并创建必要的目录 - if [[ -d /etc/nginx/sites-available ]] || [[ "$PKG_MANAGER" == "apt" ]]; then - # Debian/Ubuntu: 使用sites-available + if [[ -d /www/server/nginx ]]; then + # 宝塔面板 + NGINX_CONF_DIR="/www/server/panel/vhost/nginx" + USE_SYMLINK=false + IS_BT_PANEL=true + mkdir -p ${NGINX_CONF_DIR} + elif [[ -d /etc/nginx/sites-available ]] || [[ "$PKG_MANAGER" == "apt" ]]; then + # Debian/Ubuntu NGINX_CONF_DIR="/etc/nginx/sites-available" NGINX_ENABLED_DIR="/etc/nginx/sites-enabled" USE_SYMLINK=true + IS_BT_PANEL=false mkdir -p ${NGINX_CONF_DIR} mkdir -p ${NGINX_ENABLED_DIR} else - # CentOS/RHEL: 使用conf.d + # CentOS/RHEL NGINX_CONF_DIR="/etc/nginx/conf.d" USE_SYMLINK=false + IS_BT_PANEL=false mkdir -p ${NGINX_CONF_DIR} fi @@ -2207,25 +2240,33 @@ EOF ln -sf ${NGINX_CONF_DIR}/${PROJECT_NAME}.conf ${NGINX_ENABLED_DIR}/${PROJECT_NAME}.conf # 删除默认站点 rm -f ${NGINX_ENABLED_DIR}/default - else - # CentOS/RHEL: conf.d中的.conf文件会自动加载 + elif [[ "$IS_BT_PANEL" != "true" ]]; then + # CentOS/RHEL (非宝塔): conf.d中的.conf文件会自动加载 rm -f /etc/nginx/conf.d/default.conf fi } configure_nginx_https() { # 检测Nginx配置目录结构并创建必要的目录 - if [[ -d /etc/nginx/sites-available ]] || [[ "$PKG_MANAGER" == "apt" ]]; then - # Debian/Ubuntu: 使用sites-available + if [[ -d /www/server/nginx ]]; then + # 宝塔面板 + NGINX_CONF_DIR="/www/server/panel/vhost/nginx" + USE_SYMLINK=false + IS_BT_PANEL=true + mkdir -p ${NGINX_CONF_DIR} + elif [[ -d /etc/nginx/sites-available ]] || [[ "$PKG_MANAGER" == "apt" ]]; then + # Debian/Ubuntu NGINX_CONF_DIR="/etc/nginx/sites-available" NGINX_ENABLED_DIR="/etc/nginx/sites-enabled" USE_SYMLINK=true + IS_BT_PANEL=false mkdir -p ${NGINX_CONF_DIR} mkdir -p ${NGINX_ENABLED_DIR} else - # CentOS/RHEL: 使用conf.d + # CentOS/RHEL NGINX_CONF_DIR="/etc/nginx/conf.d" USE_SYMLINK=false + IS_BT_PANEL=false mkdir -p ${NGINX_CONF_DIR} fi @@ -2303,8 +2344,8 @@ EOF ln -sf ${NGINX_CONF_DIR}/${PROJECT_NAME}.conf ${NGINX_ENABLED_DIR}/${PROJECT_NAME}.conf # 删除默认站点 rm -f ${NGINX_ENABLED_DIR}/default - else - # CentOS/RHEL: conf.d中的.conf文件会自动加载 + elif [[ "$IS_BT_PANEL" != "true" ]]; then + # CentOS/RHEL (非宝塔): conf.d中的.conf文件会自动加载 rm -f /etc/nginx/conf.d/default.conf fi }