Files
blogsite/deploy/README.md
T

526 lines
14 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# an.xiaoan.xyz 生产部署手册
> **当前实际环境(2026-11)**:云端只跑 frps,没有云端 Nginx;本地 Nginx 9001 已切 HTTPS。
> HTTPS 请以 **[frp-https.md](frp-https.md)** 为准。
> 本文第 4 节“云端 Nginx 443”仅适用于云端另有 Nginx 的备选方案。
> 适用环境:Ubuntu 22.04 + Nginx + Gunicorn + MySQL/MariaDB + conda
> 当前架构:**本地 Nginx 9001 作为 origin,云端 Nginx 443 终止 TLS,frp 负责内网穿透。**
## 1. 架构
```text
Browser
│ https://an.xiaoan.xyz:443
▼
Cloud Nginx 443(TLS,证书 an.xiaoan.xyz)
│ http://127.0.0.1:80 (frps vhostHTTPPort)
▼
frps(云服务器,bindPort 7000)
│ frp tunnel
▼
frpc(本地 /home/frp/...,type="http", localPort=9001)
│ http://127.0.0.1:9001
▼
Local Nginx 9001(本项目 origin,HTTP)
│ http://127.0.0.1:8000
▼
Gunicorn 8000(Django WSGI)
│
├── /static/ -> /home/myserver/my-site/staticfiles/
├── /media/ -> /home/myserver/my-site/media/
└── Django / MySQL / Ollama
```
**为什么本地 9001 是 HTTP?**
现有 frpc 的 `blog_sit` 代理是 `type = "http"`,frps 不会终止 TLS;HTTPS 由云端 Nginx 443 终止。因此本地 origin 必须是 HTTP,证书给云端 Nginx 使用。
> 如果你希望本地 9001 自己终止 TLS,需要把 frpc 改为 `type = "https"`(或 TCP),并让云端 frps 使用 `vhostHTTPSPort = 443`;这会改变当前 git/blog 共用云端 Nginx 的结构,本文最后有说明。
## 2. 目录与文件
```text
/home/myserver/my-site/
├── manage.py
├── blogsite/
├── blog/ cabinet/ reflections/ moments/ mindmaps/ digest/ studio/ ...
├── staticfiles/ # collectstatic 输出(Nginx 直接提供)
├── media/ # 用户上传,公开
├── storage/ # 拾匣私有文件,绝不能由 Nginx 暴露
└── deploy/
├── requirements.txt
├── deploy.sh
├── an-xiaoan.env.example
├── nginx/an.xiaoan.xyz.conf
├── cloud-nginx/an.xiaoan.xyz.conf
└── systemd/an-xiaoan.service
```
系统文件:
```text
/etc/an-xiaoan.env # 生产环境变量,600
/etc/systemd/system/an-xiaoan.service # Gunicorn 服务
/etc/nginx/sites-available/an.xiaoan.xyz # 本地 origin 9001
/etc/nginx/sites-enabled/an.xiaoan.xyz
/etc/nginx/ssl/an.xiaoan.xyz/ # 证书(本地副本,云端也要)
/var/log/an-xiaoan/ # Gunicorn 日志
/var/log/nginx/an.xiaoan.xyz.*.log
```
## 3. 从零部署(本地 origin)
### 3.1 基础软件
```bash
sudo apt update
sudo apt install -y nginx mysql-server unzip curl openssl git
sudo systemctl enable --now nginx mysql
# 可选:conda(如果已安装可跳过)
# 本项目使用 Python 3.10
```
### 3.2 代码与 Python 环境
```bash
# 代码放到 /home/myserver/my-site
cd /home/myserver/my-site
# 使用现有 conda 环境,或新建
conda create -n myblog python=3.10 -y
conda activate myblog
pip install -r deploy/requirements.txt
```
当前环境路径:
```text
/root/anaconda3/envs/myblog/bin/python
/root/anaconda3/envs/myblog/bin/gunicorn
```
> 项目通过 `blogsite/__init__.py` 里的 `pymysql.install_as_MySQLdb()` 使用 PyMySQL,不需要 mysqlclient。
### 3.3 数据库
```sql
CREATE DATABASE blog_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'bloguser'@'localhost' IDENTIFIED BY 'your-password';
GRANT ALL PRIVILEGES ON blog_db.* TO 'bloguser'@'localhost';
FLUSH PRIVILEGES;
```
然后按实际信息修改 `blogsite/settings.py` 的 `DATABASES`(当前是 root 账号;生产建议改成专用账号)。
### 3.4 生产环境变量
```bash
cp deploy/an-xiaoan.env.example /etc/an-xiaoan.env
chmod 600 /etc/an-xiaoan.env
nano /etc/an-xiaoan.env
```
关键项:
```bash
DJANGO_DEBUG=0
DJANGO_SECRET_KEY=<用 python -c 'import secrets; print(secrets.token_urlsafe(64))' 生成>
DJANGO_ALLOWED_HOSTS=an.xiaoan.xyz,127.0.0.1,localhost,192.168.1.16
DJANGO_CSRF_TRUSTED_ORIGINS=https://an.xiaoan.xyz,http://192.168.1.16:9001,http://127.0.0.1:9001
DJANGO_SECURE_COOKIES=1
CABINET_PASSWORD=change-me
OLLAMA_BASE_URL=http://192.168.1.19:11434
OLLAMA_MODEL=qwen3.5:9b
DIGEST_MODEL=qwen3.8:27b
DIGEST_INTERVAL_DAYS=10
GITHUB_TOKEN=
PYTHONUNBUFFERED=1
TZ=Asia/Shanghai
```
> 本地直接 HTTP 调试登录后台时,`DJANGO_SECURE_COOKIES=0`;公网 HTTPS 必须为 1。
### 3.5 迁移与静态文件
```bash
set -a; . /etc/an-xiaoan.env; set +a
cd /home/myserver/my-site
python manage.py check
python manage.py migrate --noinput
python manage.py collectstatic --noinput
```
`collectstatic` 输出到 `/home/myserver/my-site/staticfiles/`,由本地 Nginx 提供。
### 3.6 Gunicorn systemd
```bash
sudo install -m 644 deploy/systemd/an-xiaoan.service /etc/systemd/system/an-xiaoan.service
sudo systemctl daemon-reload
sudo systemctl enable an-xiaoan
sudo systemctl restart an-xiaoan
sudo systemctl status an-xiaoan
```
服务关键参数:
```text
--bind 127.0.0.1:8000
--workers 1
--worker-class gthread
--threads 4
--timeout 300
```
> 1 个 worker + 4 线程:个人站点足够,同时保证 `django.core.cache` 的摘要生成锁在单进程内有效;AI 流式请求会占用一个线程。
### 3.7 本地 Nginx 9001
```bash
sudo install -m 644 deploy/nginx/an.xiaoan.xyz.https.conf /etc/nginx/sites-available/an.xiaoan.xyz
sudo ln -sfn /etc/nginx/sites-available/an.xiaoan.xyz /etc/nginx/sites-enabled/an.xiaoan.xyz
sudo nginx -t
sudo systemctl reload nginx
```
要点:
- `listen 9001 ssl http2;`(HTTPS,证书在本地)
- 反代到 `127.0.0.1:8000`
- `/static/` → `staticfiles/`
- `/media/` → `media/`
- `/digest/stream/` 关闭 `proxy_buffering`,保证 AI 摘要真正流式
- `client_max_body_size 220m`,覆盖拾匣 200MB 上传
- 通过 `X-Forwarded-Proto= https` 让 Django 认为请求是 HTTPS
- **绝不代理 `/storage/`**
### 3.8 证书
证书包解压后包含:
```text
an.xiaoan.xyz_bundle.pem # fullchain
an.xiaoan.xyz.key # 私钥
an.xiaoan.xyz_bundle.crt
an.xiaoan.xyz.csr
```
本地安全存放:
```bash
sudo mkdir -p /etc/nginx/ssl/an.xiaoan.xyz
sudo install -m 644 an.xiaoan.xyz_bundle.pem /etc/nginx/ssl/an.xiaoan.xyz/
sudo install -m 600 an.xiaoan.xyz.key /etc/nginx/ssl/an.xiaoan.xyz/
```
校验:
```bash
openssl x509 -in /etc/nginx/ssl/an.xiaoan.xyz/an.xiaoan.xyz_bundle.pem -noout -subject -dates
openssl x509 -in ...bundle.pem -noout -modulus | openssl md5
openssl rsa -in ...key -noout -modulus | openssl md5
```
> 当前本地 9001 已 HTTPS,直接使用这个证书;云端 frps 只做 SNI 转发,不需要证书。
### 3.9 frpc 也必须是 systemd(重要)
Gunicorn 和 Nginx 是 systemd,但如果 frpc 直接在终端里 `./frpc -c frpc.toml`,关闭终端会断掉 frp 隧道,公网就像整个站点挂了(本地 9001 其实还活着)。
安装 frpc 服务:
```bash
sudo install -m 644 deploy/systemd/frpc.service /etc/systemd/system/frpc.service
sudo systemctl daemon-reload
sudo systemctl enable --now frpc
sudo systemctl status frpc
sudo journalctl -u frpc -n 20 --no-pager
```
当前路径:
```text
/home/frp/frp_0.61.0_linux_amd64/frpc
/home/frp/frp_0.61.0_linux_amd64/frpc.toml
```
如果路径不同,改 unit 里的 `WorkingDirectory`/`ExecStart`,或部署时设置 `FRPC_DIR`(`deploy.sh` 会自动替换)。
云端 frps 也建议是 systemd:
```bash
sudo systemctl status frps
sudo systemctl enable --now frps
```
## 4. 云端 Nginx + 证书
> 以下命令在**云服务器**执行(110.42.136.46)。当前环境没有云服务器 SSH,所以本次只准备好了配置与证书,需要你在云端执行。
### 4.1 上传证书与配置
```bash
# 在本地执行
sudo mkdir -p /etc/nginx/ssl/an.xiaoan.xyz # 本地已有
scp /etc/nginx/ssl/an.xiaoan.xyz/an.xiaoan.xyz_bundle.pem root@110.42.136.46:/etc/nginx/ssl/an.xiaoan.xyz/
scp /etc/nginx/ssl/an.xiaoan.xyz/an.xiaoan.xyz.key root@110.42.136.46:/etc/nginx/ssl/an.xiaoan.xyz/
scp deploy/cloud-nginx/an.xiaoan.xyz.conf root@110.42.136.46:/etc/nginx/sites-available/an.xiaoan.xyz
```
### 4.2 云端启用 server 块
```bash
sudo ln -sfn /etc/nginx/sites-available/an.xiaoan.xyz /etc/nginx/sites-enabled/an.xiaoan.xyz
sudo nginx -t
sudo systemctl reload nginx
sudo chmod 600 /etc/nginx/ssl/an.xiaoan.xyz/an.xiaoan.xyz.key
sudo chmod 644 /etc/nginx/ssl/an.xiaoan.xyz/an.xiaoan.xyz_bundle.pem
```
云端配置核心:
```nginx
server {
listen 443 ssl http2;
server_name an.xiaoan.xyz;
ssl_certificate /etc/nginx/ssl/an.xiaoan.xyz/an.xiaoan.xyz_bundle.pem;
ssl_certificate_key /etc/nginx/ssl/an.xiaoan.xyz/an.xiaoan.xyz.key;
location / {
proxy_pass http://127.0.0.1:80; # frps vhostHTTPPort
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 300s;
client_max_body_size 220m;
proxy_request_buffering off;
}
location = /digest/stream/ {
proxy_pass http://127.0.0.1:80;
proxy_buffering off;
proxy_read_timeout 300s;
# 同样设置 Host / X-Forwarded-Proto / client_max_body_size
}
}
```
> 如果云端 frps 的 `vhostHTTPPort` 不是 80,把 `proxy_pass` 改成实际端口。
### 4.3 云端 frps
```toml
# frps.toml
bindPort = 7000
vhostHTTPPort = 80
```
```bash
sudo systemctl restart frps
```
### 4.4 本地 frpc
现有配置已经是正确的(无需修改):
```toml
[[proxies]]
name = "blog_sit"
type = "http"
localIP = "127.0.0.1"
localPort = 9001
customDomains = ["an.xiaoan.xyz"]
```
确认 frpc 已连接:
```bash
ps aux | grep frpc
# 或查看 frpc 日志
```
### 4.5 云端防火墙 / DNS
- 云安全组放行:`443/tcp`(公网 HTTPS)、`7000/tcp`(frps);
- `an.xiaoan.xyz` A 记录指向云服务器公网 IP;
- 本地 9001 不需要公网放行,frpc 走 127.0.0.1。
## 5. 一键部署脚本
```bash
cd /home/myserver/my-site
sudo CERT_ZIP=/path/to/an.xiaoan.xyz_nginx.zip ./deploy/deploy.sh
```
脚本会:
1. 检查/安装 Gunicorn;
2. 创建日志、证书、staticfiles、media 目录;
3. 解压证书到 `/etc/nginx/ssl/an.xiaoan.xyz/`;
4. 创建 `/etc/an-xiaoan.env`(已有则保留);
5. `migrate` + `collectstatic`;
6. 安装 systemd 服务并启动;
7. 停止旧的 `runserver 0.0.0.0:9001`;
8. 安装本地 Nginx 9001 并 reload;
9. 健康检查 `8000` / `9001`。
> 云端证书和 Nginx server 块仍需按第 4 节上传并 reload。
## 6. 验证
### 本地 origin
```bash
curl -I -H 'Host: an.xiaoan.xyz' http://127.0.0.1:9001/
curl -I -H 'Host: an.xiaoan.xyz' http://127.0.0.1:8000/
curl -I -H 'Host: an.xiaoan.xyz' http://127.0.0.1:9001/static/blog/css/style.css
curl -I -H 'Host: an.xiaoan.xyz' http://127.0.0.1:9001/admin/
```
### 公网
```bash
curl -I https://an.xiaoan.xyz/
curl -I https://an.xiaoan.xyz/static/blog/css/style.css
curl -I https://an.xiaoan.xyz/admin/
```
### 服务与日志
```bash
systemctl status an-xiaoan nginx
journalctl -u an-xiaoan -n 50 --no-pager
tail -f /var/log/an-xiaoan/error.log
tail -f /var/log/nginx/an.xiaoan.xyz.error.log
```
### AI 摘要流式
```bash
curl -N -X POST https://an.xiaoan.xyz/digest/stream/ \
-H 'X-CSRFToken: <token>' \
-H 'Accept: text/plain'
```
> 如果摘要还在 10 天有效期内,会直接返回已存内容;过期才会重新调用 Ollama。
> 本地测试可直接访问首页,引导层结束后面板逐字展开。
## 7. 日常更新
```bash
cd /home/myserver/my-site
git pull
source /root/anaconda3/etc/profile.d/conda.sh
conda activate myblog
pip install -r deploy/requirements.txt
set -a; . /etc/an-xiaoan.env; set +a
python manage.py migrate --noinput
python manage.py collectstatic --noinput
sudo systemctl restart an-xiaoan
sudo nginx -t && sudo systemctl reload nginx
```
## 8. 备份
必须备份:
```bash
# 数据库
mysqldump -u bloguser -p --single-transaction blog_db > blog_db-$(date +%F).sql
# 用户上传
tar czf media-$(date +%F).tar.gz /home/myserver/my-site/media
# 私有文件柜(非常重要)
tar czf storage-$(date +%F).tar.gz /home/myserver/my-site/storage
# 配置 / 证书
tar czf config-$(date +%F).tar.gz /etc/an-xiaoan.env /etc/nginx/ssl/an.xiaoan.xyz
```
> `storage/` 是拾匣私有文件,不在 `media/`,也绝不能由 Nginx 暴露。
## 9. 常见问题
### 关闭终端后公网断,但本地 9001 正常
说明 Gunicorn/Nginx 没挂,断的是 frpc:
```bash
systemctl is-active an-xiaoan nginx
systemctl is-active frpc
pgrep -af frpc
```
如果 `frpc` 不是 active,安装/启用 `deploy/systemd/frpc.service`,不要再在终端里手动跑。
### 502 Bad Gateway
```bash
systemctl status an-xiaoan
journalctl -u an-xiaoan -n 100 --no-pager
curl -v -H 'Host: an.xiaoan.xyz' http://127.0.0.1:8000/
```
常见原因:Gunicorn 未启动、8000 被占用、环境变量文件权限/路径错误。
### 403 CSRF
检查:
- 云端 Nginx 是否设置 `X-Forwarded-Proto https`
- 本地 Nginx 是否设置 `X-Forwarded-Proto https`
- `DJANGO_CSRF_TRUSTED_ORIGINS` 是否包含 `https://an.xiaoan.xyz`
- 浏览器地址是否是 `https://an.xiaoan.xyz`
### 413 Request Entity Too Large
三层都要改:
- 云端 Nginx `client_max_body_size 220m`
- 本地 Nginx `client_max_body_size 220m`
- `CABINET_MAX_UPLOAD_MB` / XMind 限制
### 静态文件 404
```bash
python manage.py collectstatic --noinput
ls /home/myserver/my-site/staticfiles/
sudo nginx -t
```
### 媒体文件 404
检查本地 Nginx 的 `/media/` alias 和文件权限;上传文件必须可被 `www-data` 读取。
### AI 摘要不流式
确保三层都没有缓冲:
- Django:`X-Accel-Buffering: no`(代码已设置)
- 本地 Nginx:`/digest/stream/` `proxy_buffering off`
- 云端 Nginx:`/digest/stream/` `proxy_buffering off`
### 证书问题
```bash
openssl s_client -servername an.xiaoan.xyz -connect an.xiaoan.xyz:443 </dev/null 2>/dev/null | openssl x509 -noout -subject -issuer -dates
```
确认云端证书路径、bundle 链完整、私钥匹配。
## 10. 如果想让本地 9001 自己终止 TLS
当前不推荐,因为会改变现有 `frpc type="http"` 与云端 Nginx/git 代理的结构。若要改:
1. 本地 Nginx 改为 `listen 9001 ssl http2;` + 证书;
2. frpc `blog_sit` 改为 `type = "https"`(或 TCP);
3. 云端 frps 增加 `vhostHTTPSPort = 443`;
4. 云端 Nginx 443 需要让位/改端口,否则冲突。
这样证书就在本地 Nginx 使用,云端不再需要 Nginx 终止 TLS。改动前请先确认云端 frps/防火墙/现有 git 站点,避免 443 冲突。