refactor(deploy): ?????????? Nginx ????
Made-with: Cursor
This commit is contained in:
64
scripts/lib-yh-compose-deploy.sh
Normal file
64
scripts/lib-yh-compose-deploy.sh
Normal file
@@ -0,0 +1,64 @@
|
||||
# shellcheck shell=bash
|
||||
# 由 pull-and-restart.sh / restart.sh 在定义好 ROOT、compose_cmd、run_sudo 之后 source。
|
||||
# 依赖:项目根存在 docker-compose.host-nginx.yml(与主 compose 合并使用)。
|
||||
|
||||
YH_COMPOSE_FILES="-f docker-compose.yml -f docker-compose.host-nginx.yml"
|
||||
|
||||
# 宿主机 Nginx(systemd)是否在运行;不检测 443 归属,以免误判。
|
||||
host_nginx_online() {
|
||||
command -v systemctl >/dev/null 2>&1 || return 1
|
||||
systemctl is-active nginx >/dev/null 2>&1 && return 0
|
||||
systemctl is-active nginx.service >/dev/null 2>&1 && return 0
|
||||
return 1
|
||||
}
|
||||
|
||||
# 停止本项目的 Compose 栈(含 yh_nginx 等),不操作宿主机 Nginx 服务。
|
||||
yh_compose_down() {
|
||||
if [ ! -f "$ROOT/docker-compose.host-nginx.yml" ]; then
|
||||
compose_cmd down --remove-orphans 2>/dev/null || true
|
||||
return 0
|
||||
fi
|
||||
compose_cmd $YH_COMPOSE_FILES down --remove-orphans 2>/dev/null || true
|
||||
}
|
||||
|
||||
# 按宿主机 Nginx 是否在线,选择仅起业务容器或起完整栈(含 yh_nginx)。
|
||||
yh_compose_up() {
|
||||
if [ ! -f "$ROOT/docker-compose.host-nginx.yml" ]; then
|
||||
echo "未找到 docker-compose.host-nginx.yml,使用默认 compose up."
|
||||
compose_cmd up -d --force-recreate
|
||||
return 0
|
||||
fi
|
||||
if host_nginx_online; then
|
||||
echo "检测到宿主机 Nginx 已运行:不启动容器 yh_nginx;mongo/api/web/admin 绑定本机回环(见 nginx/yuheng.host.conf)。"
|
||||
compose_cmd $YH_COMPOSE_FILES up -d --force-recreate mongo api web admin
|
||||
else
|
||||
echo "未检测到宿主机 Nginx 运行:启动完整栈(含容器 yh_nginx 终止 TLS)。"
|
||||
compose_cmd $YH_COMPOSE_FILES --profile compose-internal-nginx up -d --force-recreate
|
||||
fi
|
||||
}
|
||||
|
||||
# 仅当宿主机 Nginx 在线时:从模板写入站点配置并重载(不执行 systemctl start/stop nginx)。
|
||||
yh_install_host_nginx_site_conf() {
|
||||
local domain="${NGINX_DOMAIN:-yuheng.yuxindazhineng.com}"
|
||||
local tpl="$ROOT/nginx/yuheng.host.conf"
|
||||
local out="/etc/nginx/conf.d/${domain}.conf"
|
||||
|
||||
host_nginx_online || {
|
||||
echo "宿主机 Nginx 未运行,跳过写入站点配置(由容器 yh_nginx 对外)。"
|
||||
return 0
|
||||
}
|
||||
|
||||
if [ ! -f "$tpl" ]; then
|
||||
echo "未找到 $tpl,跳过宿主机站点配置生成。"
|
||||
return 0
|
||||
fi
|
||||
|
||||
mkdir -p "$ROOT/verify-root"
|
||||
sed "s|__VERIFY_ROOT__|$ROOT/verify-root|g" "$tpl" | run_sudo tee "$out" >/dev/null
|
||||
|
||||
if run_sudo nginx -t 2>/dev/null; then
|
||||
run_sudo systemctl reload nginx 2>/dev/null && echo "宿主机 Nginx 已重载($out)。" || true
|
||||
else
|
||||
echo "警告: 宿主机 nginx -t 失败,请检查 $out" >&2
|
||||
fi
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# 宿主机 Nginx 单实例部署:启动 api/web/admin/mongo(不启动容器 yh_nginx),并可选安装站点配置后 reload。
|
||||
# 用法(在项目根目录):
|
||||
# chmod +x scripts/start-with-host-nginx.sh
|
||||
# ./scripts/start-with-host-nginx.sh
|
||||
#
|
||||
# 依赖:已安装 Docker Compose、宿主机 Nginx、证书目录 /etc/ssl/yh_web/yuheng.yuxindazhineng.com/
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
cd "$ROOT"
|
||||
|
||||
echo "==> 启动容器:mongo api web admin(不启动 compose 内 yh_nginx)"
|
||||
docker compose -f docker-compose.yml -f docker-compose.host-nginx.yml up -d mongo api web admin
|
||||
|
||||
echo "==> 若曾用旧方案启动过 yh_nginx,可手动停止释放 443: docker rm -f yh_nginx 2>/dev/null || true"
|
||||
docker rm -f yh_nginx 2>/dev/null || true
|
||||
|
||||
VERIFY_ROOT="${VERIFY_ROOT:-$ROOT/verify-root}"
|
||||
mkdir -p "$VERIFY_ROOT"
|
||||
|
||||
CONF_OUT="${NGINX_CONF_OUT:-/etc/nginx/conf.d/yuheng.yuxindazhineng.com.conf}"
|
||||
TEMPLATE="$ROOT/nginx/yuheng.host.conf"
|
||||
|
||||
if [[ ! -f "$TEMPLATE" ]]; then
|
||||
echo "缺少模板:$TEMPLATE" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "==> 生成宿主机 Nginx 配置(VERIFY_ROOT=$VERIFY_ROOT)"
|
||||
if [[ "${INSTALL_NGINX_CONF:-1}" == "1" ]]; then
|
||||
if ! command -v sudo >/dev/null 2>&1; then
|
||||
echo "未找到 sudo,请手动执行:" >&2
|
||||
echo " sed \"s|__VERIFY_ROOT__|$VERIFY_ROOT|g\" \"$TEMPLATE\" | sudo tee \"$CONF_OUT\"" >&2
|
||||
echo " sudo nginx -t && sudo systemctl reload nginx" >&2
|
||||
exit 0
|
||||
fi
|
||||
sed "s|__VERIFY_ROOT__|$VERIFY_ROOT|g" "$TEMPLATE" | sudo tee "$CONF_OUT" >/dev/null
|
||||
sudo nginx -t
|
||||
sudo systemctl reload nginx
|
||||
echo "==> 已写入 $CONF_OUT 并重载 Nginx。请确认 443 仅由宿主机占用: ss -tlnp | grep 443"
|
||||
else
|
||||
echo "已跳过安装(INSTALL_NGINX_CONF=0)。可手动:"
|
||||
echo " sed \"s|__VERIFY_ROOT__|$VERIFY_ROOT|g\" nginx/yuheng.host.conf | sudo tee $CONF_OUT"
|
||||
echo " sudo nginx -t && sudo systemctl reload nginx"
|
||||
fi
|
||||
|
||||
echo "==> 本机回环端口(宿主机 Nginx 应反代到这些地址,与 nginx/yuheng.host.conf 一致):"
|
||||
echo " API http://127.0.0.1:8088"
|
||||
echo " 前台 http://127.0.0.1:9080"
|
||||
echo " 后台 http://127.0.0.1:9081"
|
||||
Reference in New Issue
Block a user