From 495bd3ebafd2622e1d983397a470e2b4cc259ea5 Mon Sep 17 00:00:00 2001 From: huanghaosheng <386998068@qq.com> Date: Thu, 15 Oct 2026 00:04:04 +0800 Subject: [PATCH] =?UTF-8?q?docs:=20=E8=A1=A5=20DEPLOY.md=EF=BC=8C=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=2020000=20=E7=AB=AF=E5=8F=A3=E9=9D=99=E6=80=81?= =?UTF-8?q?=E7=AB=99=E7=82=B9=E7=9A=84=E5=AE=8C=E6=95=B4=E9=83=A8=E7=BD=B2?= =?UTF-8?q?=E6=B5=81=E7=A8=8B=E4=B8=8E=E6=8E=92=E9=9A=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DEPLOY.md | 324 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 324 insertions(+) create mode 100644 DEPLOY.md diff --git a/DEPLOY.md b/DEPLOY.md new file mode 100644 index 0000000..ad7f6f7 --- /dev/null +++ b/DEPLOY.md @@ -0,0 +1,324 @@ +# materials 部署文档 + +把本仓库整体部署到阿里云 ECS(`8.161.227.145`),访问 **http://8.161.227.145:20000** 直接展示 +根目录的 `index.html`,并由它 hash 路由到各板块页面。 + +- 站点类型:**纯静态、零依赖、无构建**(就是仓库内容本身) +- Web 服务器:复用服务器上 **1Panel 自带的 OpenResty**,不新装、不新起进程 +- 内容更新:服务器上 `git pull`(见 [日常更新](#日常更新)) + +--- + +## 1. 目标环境 + +部署当天服务器的实际状况(换机器时按这几条对照即可): + +| 项目 | 值 | +|---|---| +| 系统 | Ubuntu 22.04.5 LTS,内核 5.15,2 vCPU / 3.5G 内存 | +| 磁盘 | 根分区 `/dev/vda3` 40G(部署时已用 88%,剩 4.8G) | +| 面板 | 1Panel **v2.1.8**(`29524`) | +| Web 服务 | 容器 `1Panel-openresty-XOJH`,镜像 `1panel/openresty:1.27.1.2-5-1-focal` | +| 代码托管 | 容器 `1Panel-gitea-VbdV`,镜像 `commitgo/gitea-ee:25.5.0`(Web `3000`、SSH `222`) | +| 运行环境 | `git 2.34.1`、`node`(`/usr/local/bin/node`)、`openssl 3.0.2`、`python3` | +| 端口 | 公网 `20000` 已在**阿里云安全组**放行;主机 `ufw` 未启用、`iptables` INPUT ACCEPT | +| 仓库 remote | `ssh://git@8.161.227.145:222/huanghaosheng/materials.git`(Gitea 里匿名可读) | + +### 关键前提(决定方案为什么这么简单) + +1. **OpenResty 容器用的是 `host` 网络模式** + `docker inspect 1Panel-openresty-XOJH --format '{{.HostConfig.NetworkMode}}'` → `host`。 + 所以**只需要在配置里加一个 `listen 20000` 的 server 块并 reload,端口立刻生效**—— + 不需要改 docker 端口映射、不需要重建容器。 +2. **主配置会 include 站点目录** + `nginx.conf` 里有 `include /usr/local/openresty/nginx/conf/conf.d/*.conf;`, + 而该目录 bind-mount 自宿主机 `/opt/1panel/www/conf.d`,所以往宿主机那个目录丢 `.conf` 就等于生效。 +3. **容器内 `/www` = 宿主机 `/opt/1panel/www`** + → **写进 conf 的路径必须是容器视角的 `/www/...`**,不是宿主机路径。这是最容易踩的坑。 +4. **站点是 hash 路由**(`index.html#/knowledge/docker`) + → 不需要 `try_files` 回退、不需要 rewrite 规则,`root` + `index` 就够了。 + +--- + +## 2. 部署步骤 + +### 步骤 0:确保内容已推到 Gitea + +本地工作区干净、且不比远端落后(否则服务器上拉到的是旧版本): + +```bash +git status -sb # 期望 "## main...origin/main",没有 ahead +git push origin main +``` + +### 步骤 1:服务器上准备目录并拉取内容 + +```bash +SITE_ROOT=/opt/1panel/www/sites/materials + +mkdir -p "$SITE_ROOT/log" "$SITE_ROOT/auth" + +git clone http://127.0.0.1:3000/huanghaosheng/materials.git "$SITE_ROOT/index" +``` + +> 用 `http://127.0.0.1:3000/...` 走 Gitea 的 Web 口,避免在服务器上再配一把 SSH key; +> 该仓库匿名可读,所以不需要凭据。内容目录刻意和 1Panel 的站点目录约定保持一致 +> (`/www/sites/<名字>/index`),将来若改用面板托管可以直接复用。 + +### 步骤 2:写入站点配置 + +新建 `/opt/1panel/www/conf.d/materials.conf`(**注意 root 写容器路径 `/www/...`**): + +```nginx +# ============================================================================ +# materials · 资料库导航 纯静态站点,仅监听 20000 +# +# 内容目录 : /www/sites/materials/index +# (宿主机 /opt/1panel/www/sites/materials/index,来自 Gitea 的 git clone) +# 更新内容 : bash /root/deploy-materials.sh (纯静态,改内容无需 reload nginx) +# +# 访问控制 : 无,公开访问。 +# 如需恢复 Basic Auth,把下面 auth_basic 两行的注释去掉即可,密码文件: +# /www/sites/materials/auth/basic.pass(账号 materials) +# 重设密码: +# printf 'materials:%s\n' "$(openssl passwd -apr1 '新密码')" \ +# > /opt/1panel/www/sites/materials/auth/basic.pass +# +# 注意:本文件为手工维护,未登记在 1Panel 面板的「网站」列表中。 +# 若以后改用面板托管,请先在面板里创建静态网站(端口 20000)再删除本文件, +# 避免同端口出现两份 server 块。 +# ============================================================================ +server { + listen 20000; + server_name _; + + charset utf-8; + root /www/sites/materials/index; + index index.html; + + access_log /www/sites/materials/log/access.log main; + error_log /www/sites/materials/log/error.log; + + # 整站 Basic Auth —— 当前已关闭 + # auth_basic "materials"; + # auth_basic_user_file /www/sites/materials/auth/basic.pass; + + # 版本库与隐藏文件不可访问(.git / .gitattributes / .sre-s23 等) + location ~ /\.(?!well-known) { + deny all; + access_log off; + } + + # 纯静态 + hash 路由:不需要任何 rewrite / History 回退规则, + # 目录请求由 index 指令落到 index.html,其余交给默认静态处理。 + + # HTML 不做强缓存,改完内容刷新即见;js/css 走 ETag 重新校验 + location ~* \.html?$ { + add_header Cache-Control "no-cache"; + } +} +``` + +几点说明: + +- `gzip` 已经在主配置的 `http` 段全局打开(含 `gzip_types`),server 块里不用重复写。 +- `location ~ /\.(?!well-known)` 挡住所有隐藏文件/目录 —— 因为内容是 `git clone` 到 webroot 的, + 必须保证 `/.git/config` 之类取不到(实测返回 **403**)。 +- `server_name _;` 是占位。用 `8.161.227.145:20000` 访问时 Host 头并不匹配任何名字, + 但它是该端口上**唯一**的 server 块,nginx 会把它当默认 server 用,所以不影响访问。 + +> 传文件建议直接 `scp` 到服务器再 `install`,比在 ssh 里嵌 heredoc 稳: +> ```bash +> scp materials.conf root@8.161.227.145:/root/materials.conf.new +> ssh root@8.161.227.145 'install -m 644 /root/materials.conf.new \ +> /opt/1panel/www/conf.d/materials.conf && rm -f /root/materials.conf.new' +> ``` + +### 步骤 3:(可选)开启 Basic Auth + +默认**关闭**。需要关起来时,生成密码文件并取消 conf 里两行注释: + +```bash +AUTH=/opt/1panel/www/sites/materials/auth/basic.pass +printf 'materials:%s\n' "$(openssl passwd -apr1 '你想设的密码')" > "$AUTH" +chmod 640 "$AUTH" +``` + +密码文件放在**站点根目录之外**(站点根是 `.../materials/index`),所以 HTTP 取不到它 +(实测 `/auth/basic.pass` → 404)。取消注释后 reload 即可,**不需要重启容器**。 + +### 步骤 4:校验并重载 + +```bash +docker exec 1Panel-openresty-XOJH /usr/local/openresty/nginx/sbin/nginx -t +docker exec 1Panel-openresty-XOJH /usr/local/openresty/nginx/sbin/nginx -s reload +ss -tlnp | grep -w 20000 # 期望看到 openresty 监听 0.0.0.0:20000 +``` + +### 步骤 5:安装一键更新脚本 + +`/root/deploy-materials.sh`,`chmod +x`: + +```bash +#!/usr/bin/env bash +# ============================================================================ +# materials 一键更新脚本 +# 用法:在服务器上执行 bash /root/deploy-materials.sh +# 做三件事:把 Gitea 上最新代码对齐到站点目录 → 跑导航页自检 → 打印当前版本 +# 纯静态站点,改内容不需要 reload nginx。 +# ============================================================================ +set -euo pipefail + +SITE_DIR=/opt/1panel/www/sites/materials/index +REPO=http://127.0.0.1:3000/huanghaosheng/materials.git +URL=http://8.161.227.145:20000 + +if [ -d "$SITE_DIR/.git" ]; then + echo "==> 拉取最新代码" + git -C "$SITE_DIR" remote set-url origin "$REPO" + git -C "$SITE_DIR" fetch --prune origin + git -C "$SITE_DIR" reset --hard origin/main +else + echo "==> 首次克隆" + git clone "$REPO" "$SITE_DIR" +fi + +echo "==> 当前版本:$(git -C "$SITE_DIR" rev-parse --short HEAD) $(git -C "$SITE_DIR" log -1 --pretty=%s)" +echo " 提交时间:$(git -C "$SITE_DIR" log -1 --pretty=%ci)" + +echo "==> 导航页自检(注册表里的入口在磁盘上是否都存在)" +if command -v node >/dev/null 2>&1; then + if ( cd "$SITE_DIR" && node tools/check-portal.js ); then + echo " 自检通过" + else + echo " !! 自检未通过:上面列出的死链需要修(站点已更新,只是有坏链接)" + fi +else + echo " (未安装 node,跳过自检)" +fi + +echo "==> 完成:$URL" +``` + +用 `reset --hard origin/main` 而不是 `pull --ff-only`:服务器上这份是**只读产物**, +本地怎么改都不该在服务器上编辑,直接对齐远端最新最省心(也能吸收 force push)。 + +### 步骤 6:日志轮转 + +站点日志不在 1Panel 的托管范围内,需要自己切,否则长期会撑大日志文件 +(部署时根分区已用 88%)。新建 `/etc/logrotate.d/materials`: + +``` +/opt/1panel/www/sites/materials/log/*.log { + weekly + rotate 4 + missingok + notifempty + compress + delaycompress + copytruncate +} +``` + +`copytruncate` 是必须的 —— nginx 一直握着日志文件句柄,不能靠 `create` 方式切。 + +--- + +## 3. 验证清单 + +从**公网**(不是服务器本机)用 `curl` 逐个确认,全部应当通过: + +```bash +BASE=http://8.161.227.145:20000 + +# 首页:200,Content-Type 带 charset=utf-8,标题为「资料库导航 · materials」 +curl -s -D - -o /dev/null "$BASE/" | grep -iE 'HTTP/|content-type|cache-control' +curl -s "$BASE/" | grep '