feat: 项目库上线(七阶段流程 + Gitea 同步 + 首页入口)

This commit is contained in:
2026-11-25 02:49:26 +08:00
parent e3270bcd94
commit 0c4ff1df5e
41 changed files with 4830 additions and 6 deletions
+525
View File
@@ -0,0 +1,525 @@
# 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 冲突。
+27
View File
@@ -0,0 +1,27 @@
# /etc/an-xiaoan.env
# 复制为 /etc/an-xiaoan.env 后 chmod 600,并替换 SECRET_KEY。
# 不要把本文件提交到 git。
DJANGO_DEBUG=0
DJANGO_SECRET_KEY=replace-with-a-long-random-secret
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
# 拾匣密码(不设置则使用 settings 里的默认值)
# CABINET_PASSWORD=change-me
# 本地 Ollama
OLLAMA_BASE_URL=http://192.168.1.19:11434
OLLAMA_MODEL=qwen3.5:9b
OLLAMA_TIMEOUT=90
OLLAMA_THINK=0
# 拾语
DIGEST_MODEL=qwen3.8:27b
DIGEST_INTERVAL_DAYS=10
# 可选
GITHUB_TOKEN=
PYTHONUNBUFFERED=1
TZ=Asia/Shanghai
+79
View File
@@ -0,0 +1,79 @@
# =========================================================
# an.xiaoan.xyz - 云端 Nginx(443,TLS)
#
# 放在云服务器 /etc/nginx/sites-available/an.xiaoan.xyz
# 依赖:
# - 云服务器 frps 开启 vhostHTTPPort = 80
# - frpc 的 blog_sit 代理:type="http", localPort=9001,
# customDomains=["an.xiaoan.xyz"]
# - 证书放到 /etc/nginx/ssl/an.xiaoan.xyz/
#
# 链路:
# Browser 443 -> 本 Nginx (TLS 终止) -> 127.0.0.1:80 (frps)
# -> frpc -> 内网 Nginx:9001 -> Gunicorn:8000
# =========================================================
server {
listen 443 ssl http2;
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;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets off;
server_tokens off;
client_max_body_size 220m;
client_body_timeout 300s;
send_timeout 300s;
access_log /var/log/nginx/an.xiaoan.xyz.access.log;
error_log /var/log/nginx/an.xiaoan.xyz.error.log warn;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Content-Type-Options nosniff always;
add_header X-Frame-Options SAMEORIGIN always;
add_header Referrer-Policy strict-origin-when-cross-origin always;
# AI 摘要流式接口:关闭缓冲
location = /digest/stream/ {
proxy_pass http://127.0.0.1:80;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Port 443;
proxy_set_header Connection "";
proxy_connect_timeout 10s;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
proxy_buffering off;
proxy_cache off;
}
location / {
proxy_pass http://127.0.0.1:80;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Port 443;
proxy_set_header Connection "";
proxy_connect_timeout 10s;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
proxy_request_buffering off;
}
}
+148
View File
@@ -0,0 +1,148 @@
#!/usr/bin/env bash
# =========================================================
# an.xiaoan.xyz 本地 origin 一键部署
#
# 用法:
# sudo CERT_ZIP=/path/an.xiaoan.xyz_nginx.zip ./deploy/deploy.sh
# 或 sudo ./deploy/deploy.sh /path/an.xiaoan.xyz_nginx.zip
#
# 说明:
# - 本脚本只部署本机:Gunicorn + 本地 Nginx 9001(HTTP origin)。
# - 云端 Nginx 443 的证书与 server 块需要上传到云服务器后手动 reload,
# 配置模板在 deploy/cloud-nginx/an.xiaoan.xyz.conf。
# - 现有 frpc 是 type="http"(本地 9001 -> 云端 frps:80 -> 云端 Nginx 443)。
# =========================================================
set -euo pipefail
APP_DIR="${APP_DIR:-/home/myserver/my-site}"
DOMAIN="${DOMAIN:-an.xiaoan.xyz}"
PYTHON="${PYTHON:-/root/anaconda3/envs/myblog/bin/python}"
GUNICORN="${GUNICORN:-/root/anaconda3/envs/myblog/bin/gunicorn}"
CERT_ZIP="${CERT_ZIP:-${1:-}}"
NGINX_CONF="${NGINX_CONF:-deploy/nginx/${DOMAIN}.https.conf}"
FRPC_DIR="${FRPC_DIR:-/home/frp/frp_0.61.0_linux_amd64}"
CERT_DIR="/etc/nginx/ssl/${DOMAIN}"
ENV_FILE="/etc/an-xiaoan.env"
LOG_DIR="/var/log/an-xiaoan"
if [ "$(id -u)" -ne 0 ]; then
echo "请用 root 运行:sudo $0" >&2
exit 1
fi
cd "$(dirname "$0")/.."
echo "==> 1/9 检查 Gunicorn"
if [ ! -x "$GUNICORN" ]; then
"$PYTHON" -m pip install "gunicorn==27.0.1"
fi
"$GUNICORN" --version
echo "==> 2/9 创建日志 / 证书 / 静态目录"
mkdir -p "$LOG_DIR" "$CERT_DIR" "$APP_DIR/staticfiles" "$APP_DIR/media"
if [ -n "$CERT_ZIP" ]; then
if [ ! -f "$CERT_ZIP" ]; then
echo "找不到证书包:$CERT_ZIP" >&2
exit 1
fi
TMP_CERT="$(mktemp -d)"
trap 'rm -rf "$TMP_CERT"' EXIT
unzip -q "$CERT_ZIP" -d "$TMP_CERT"
BUNDLE="$(find "$TMP_CERT" -name '*_bundle.pem' | head -1)"
KEY="$(find "$TMP_CERT" -name '*.key' | head -1)"
if [ -z "$BUNDLE" ] || [ -z "$KEY" ]; then
echo "证书包里没找到 _bundle.pem / .key" >&2
exit 1
fi
install -m 644 "$BUNDLE" "$CERT_DIR/${DOMAIN}_bundle.pem"
install -m 600 "$KEY" "$CERT_DIR/${DOMAIN}.key"
echo "证书已安装到 $CERT_DIR(本地 9001 不直接用,供云端 Nginx 使用)"
fi
echo "==> 3/9 环境变量文件"
if [ ! -f "$ENV_FILE" ]; then
cp deploy/an-xiaoan.env.example "$ENV_FILE"
SECRET="$("$PYTHON" -c 'import secrets; print(secrets.token_urlsafe(64))')"
"$PYTHON" - "$ENV_FILE" "$SECRET" <<'PYGEN'
import sys
path, secret = sys.argv[1], sys.argv[2]
text = open(path, encoding="utf-8").read()
open(path, "w", encoding="utf-8").write(
text.replace("replace-with-a-long-random-secret", secret)
)
PYGEN
chmod 600 "$ENV_FILE"
echo "已生成 $ENV_FILE(请按需修改 CABINET_PASSWORD / OLLAMA_BASE_URL)"
else
echo "$ENV_FILE 已存在,保留不覆盖"
fi
echo "==> 4/9 数据库迁移与 collectstatic"
set -a
# shellcheck disable=SC1090
. "$ENV_FILE"
set +a
"$PYTHON" manage.py check
"$PYTHON" manage.py migrate --noinput
"$PYTHON" manage.py collectstatic --noinput
echo "==> 5/9 安装 systemd 服务"
install -m 644 deploy/systemd/an-xiaoan.service /etc/systemd/system/an-xiaoan.service
systemctl daemon-reload
systemctl enable an-xiaoan
echo "==> 6/9 停止旧 runserver(如果还在 9001)"
if pgrep -f "manage.py runserver 0.0.0.0:9001" >/dev/null 2>&1; then
echo "停止旧 runserver"
pkill -f "manage.py runserver 0.0.0.0:9001" || true
sleep 2
fi
echo "==> 7/9 启动 Gunicorn + 安装本地 Nginx 9001"
systemctl restart an-xiaoan
for _ in $(seq 1 15); do
if curl -fsS -H "Host: ${DOMAIN}" http://127.0.0.1:8000/ -o /dev/null 2>/dev/null; then
break
fi
sleep 1
done
systemctl --no-pager status an-xiaoan | head -8
install -m 644 "$NGINX_CONF" "/etc/nginx/sites-available/${DOMAIN}"
ln -sfn "/etc/nginx/sites-available/${DOMAIN}" "/etc/nginx/sites-enabled/${DOMAIN}"
nginx -t
systemctl reload nginx
echo "==> 8/9 健康检查"
sleep 1
curl -fsS -H "Host: ${DOMAIN}" http://127.0.0.1:8000/ -o /dev/null
curl -kfsS -H "Host: ${DOMAIN}" https://127.0.0.1:9001/ -o /dev/null
echo "本地 Gunicorn 8000 / Nginx 9001 (HTTPS) 正常"
echo "==> 9/9 安装 frpc systemd(防止关闭终端断线)"
if [ -x "$FRPC_DIR/frpc" ] && [ -f "$FRPC_DIR/frpc.toml" ]; then
sed "s|/home/frp/frp_0.61.0_linux_amd64|$FRPC_DIR|g" deploy/systemd/frpc.service > /etc/systemd/system/frpc.service
chmod 644 /etc/systemd/system/frpc.service
systemctl daemon-reload
if pgrep -x frpc >/dev/null 2>&1 && ! systemctl is-active --quiet frpc; then
pkill -x frpc || true
sleep 1
fi
systemctl enable frpc
systemctl restart frpc
echo "frpc.service 已启用($FRPC_DIR)"
else
echo "未找到 $FRPC_DIR/frpc 或 frpc.toml,跳过 frpc.service"
fi
echo
echo "完成。"
echo "本地链路:Nginx 9001 (TLS) -> Gunicorn 8000 -> Django"
echo "公网链路:frps:443 (SNI) -> frpc(type=https) -> 本机 Nginx 9001 (TLS)"
echo
echo "云端 / frpc 要求:"
echo "1) 云端 frps.toml: vhostHTTPSPort = 443,并重启 frps"
echo "2) 本地 frpc.toml: blog_sit type = \"https\", localPort = 9001,并重启 frpc"
echo "3) 证书只在本机 /etc/nginx/ssl/${DOMAIN}/,云端不需要证书,也不需要云端 Nginx"
echo "4) 详细步骤见 deploy/frp-https.md"
+245
View File
@@ -0,0 +1,245 @@
# 云端只跑 frps 时的 HTTPS 配置
适用于:云端服务器只做 frps 转发,没有云端 Nginx;所有端口可用。
## 结论
需要改三处:
1. **云端 frps.toml**:增加 `vhostHTTPSPort = 443`
2. **本地 frpc.toml**:`blog_sit` 从 `type = "http"` 改为 `type = "https"`
3. **本地 Nginx 9001**:从 HTTP 改为 HTTPS,使用 `an.xiaoan.xyz` 证书
Django 不需要改:项目已经设置了:
```python
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
CSRF_TRUSTED_ORIGINS = ["https://an.xiaoan.xyz", ...]
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
```
> frp 的 HTTPS 代理**不会在 frps 终止 TLS**,证书必须放在本地 Nginx。
> 云端 frps 不需要证书,也不需要云端 Nginx。
## 最终链路
```text
Browser
│ https://an.xiaoan.xyz:443
▼
云服务器 frps:443(vhostHTTPSPort,按 SNI 路由,不解密)
│ frp tunnel
▼
本地 frpc(type="https", localPort=9001)
│ https://127.0.0.1:9001
▼
本地 Nginx 9001(TLS 终止,证书 an.xiaoan.xyz)
│ http://127.0.0.1:8000
▼
Gunicorn 8000 -> Django
```
git 代理保持原样:
```toml
[[proxies]]
name = "Git_sit_"
type = "http"
localPort = 3000
customDomains = ["git.xiaoan.xyz"]
```
## 1. 云端 frps
云服务器 `/etc/frp/frps.toml`(或实际路径):
```toml
bindPort = 7000
vhostHTTPPort = 80 # 保留,给 git 等 HTTP 代理
vhostHTTPSPort = 443 # 新增,给 an.xiaoan.xyz 的 HTTPS
```
重启:
```bash
sudo systemctl restart frps
# 或实际服务名:systemctl restart frps.service
```
确认 443 已监听:
```bash
sudo ss -lntp | grep ':443'
```
> 云安全组/防火墙放行 443/tcp;你说所有端口可用,通常不需要额外操作。
## 2. 本地 Nginx 9001 改为 HTTPS
把:
```nginx
listen 9001;
```
改成:
```nginx
listen 9001 ssl http2;
```
并在 `server {` 内加入:
```nginx
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;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets off;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
```
完整文件已放在:
```text
deploy/nginx/an.xiaoan.xyz.https.conf
```
安装并 reload:
```bash
sudo cp 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
```
证书已在本机:
```text
/etc/nginx/ssl/an.xiaoan.xyz/an.xiaoan.xyz_bundle.pem
/etc/nginx/ssl/an.xiaoan.xyz/an.xiaoan.xyz.key (600)
```
验证本地 TLS:
```bash
curl -k -I -H 'Host: an.xiaoan.xyz' https://127.0.0.1:9001/
```
## 3. 本地 frpc
把 `blog_sit` 代理改成:
```toml
[[proxies]]
name = "blog_sit"
type = "https"
localIP = "127.0.0.1"
localPort = 9001
customDomains = ["an.xiaoan.xyz"]
```
完整片段:`deploy/frpc-blog-https.toml`。
重启 frpc。**frpc 必须做成 systemd**,不要再用终端里的 `./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
```
如果已经装了 frpc.service,改完配置后:
```bash
sudo systemctl restart frpc
```
确认 frpc 日志没有 `connect to local service error` 或 TLS 相关错误。
## 4. 验证
```bash
# 云端 443 是否开着
nc -vz 110.42.136.46 443
# 本地 Nginx TLS
curl -k -I -H 'Host: an.xiaoan.xyz' https://127.0.0.1:9001/
# 公网 HTTPS
curl -I https://an.xiaoan.xyz/
# 证书
echo | openssl s_client -servername an.xiaoan.xyz -connect an.xiaoan.xyz:443 2>/dev/null \
| openssl x509 -noout -subject -issuer -dates
```
预期:公网返回 200,证书 subject 为 `CN = an.xiaoan.xyz`。
## 5. 可选:HTTP 跳转 HTTPS
改完后 `http://an.xiaoan.xyz` 不再有代理。如果想保留跳转:
本地新增一个 HTTP server(9002):
```nginx
server {
listen 9002;
server_name an.xiaoan.xyz;
return 301 https://$host$request_uri;
}
```
文件:`deploy/nginx/an.xiaoan.xyz.http-redirect.conf`。
frpc 增加:
```toml
[[proxies]]
name = "blog_http"
type = "http"
localIP = "127.0.0.1"
localPort = 9002
customDomains = ["an.xiaoan.xyz"]
```
frps 继续使用 `vhostHTTPPort = 80`;git 和 an 的 HTTP 会按域名分流。
## 6. 回滚
如果 HTTPS 切换失败:
```toml
# frpc
type = "http"
```
```nginx
# 本地 nginx
listen 9001;
```
```bash
sudo nginx -t && sudo systemctl reload nginx
# 重启 frpc
```
云端 frps 的 `vhostHTTPSPort` 可以保留,不影响 HTTP 代理。
## 7. 注意事项
- **frps 不终止 TLS**,证书在本地 Nginx;云端不需要证书。
- `type="https"` 时本地服务必须是 HTTPS;如果本地 Nginx 仍是 HTTP,frpc 会连接失败。
- `vhostHTTPSPort = 443` 与云端已有的 443 服务不能冲突;你说云端只有 frp,通常没有冲突。
- 切换期间会短暂中断:建议顺序为 **云端 frps 先加 443 → 本地 Nginx 改 TLS → 本地 frpc 改 https**。
- 当前本地 Nginx 9001 还是 HTTP、frpc 还是 `type="http"`;按本文切换后才是 HTTPS。
- 证书续期后只需替换本地两个文件并 `systemctl reload nginx`,frps/frpc 不用动。
+7
View File
@@ -0,0 +1,7 @@
# 替换 frpc.toml 里原来的 blog_sit 代理(name 保持不变)
[[proxies]]
name = "blog_sit"
type = "https"
localIP = "127.0.0.1"
localPort = 9001
customDomains = ["an.xiaoan.xyz"]
+4
View File
@@ -0,0 +1,4 @@
# 云端 frps.toml:在原有基础上增加 vhostHTTPSPort
bindPort = 7000
vhostHTTPPort = 80
vhostHTTPSPort = 443
+85
View File
@@ -0,0 +1,85 @@
# =========================================================
# an.xiaoan.xyz - 本地 origin Nginx(9001,HTTP)
#
# 公网链路:
# Browser -> 云端 Nginx 443 (TLS, 证书) -> frps:80
# -> frpc -> 本机 Nginx:9001 -> Gunicorn:8000 -> Django
#
# 注意:现有 frpc 的 blog_sit 代理是 type="http",所以本机 9001 必须是 HTTP。
# 如果你改成 type="https" / TCP,请把本文件改为 listen 9001 ssl 并使用证书。
# =========================================================
upstream an_xiaoan_app {
server 127.0.0.1:8000;
keepalive 16;
}
server {
listen 9001;
server_name an.xiaoan.xyz;
server_tokens off;
client_max_body_size 220m;
client_body_timeout 300s;
send_timeout 300s;
access_log /var/log/nginx/an.xiaoan.xyz.access.log;
error_log /var/log/nginx/an.xiaoan.xyz.error.log warn;
# 云端 Nginx 会传 X-Forwarded-Proto: https;若没有(例如直接访问本机),
# 也按 HTTPS 处理,因为公网入口固定是云端 443。
set $forwarded_proto $http_x_forwarded_proto;
if ($forwarded_proto = "") {
set $forwarded_proto https;
}
# AI 摘要流式接口:关闭缓冲,保证逐段到达浏览器
location = /digest/stream/ {
proxy_pass http://an_xiaoan_app;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $forwarded_proto;
proxy_set_header Connection "";
proxy_connect_timeout 10s;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
proxy_buffering off;
proxy_cache off;
}
location /static/ {
alias /home/myserver/my-site/staticfiles/;
expires 30d;
access_log off;
}
location /media/ {
alias /home/myserver/my-site/media/;
expires 7d;
access_log off;
}
location / {
proxy_pass http://an_xiaoan_app;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $forwarded_proto;
proxy_set_header Connection "";
proxy_connect_timeout 10s;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
proxy_request_buffering off;
}
add_header X-Content-Type-Options nosniff always;
add_header X-Frame-Options SAMEORIGIN always;
add_header Referrer-Policy strict-origin-when-cross-origin always;
}
@@ -0,0 +1,7 @@
# 可选:HTTP -> HTTPS 跳转
# 需要 frpc 额外增加一个 type="http" 的代理,localPort=9002
server {
listen 9002;
server_name an.xiaoan.xyz;
return 301 https://$host$request_uri;
}
+95
View File
@@ -0,0 +1,95 @@
# =========================================================
# an.xiaoan.xyz - 本地 origin Nginx(9001,HTTPS)
#
# 公网链路(云端只跑 frps):
# Browser -> frps:443 (SNI, 不终止 TLS)
# -> frpc (type=https) -> 本机 Nginx:9001 (TLS, 本证书)
# -> Gunicorn:8000 -> Django
#
# 注意:frps 的 vhostHTTPSPort = 443;frpc 的 blog_sit 必须 type="https"。
# =========================================================
upstream an_xiaoan_app {
server 127.0.0.1:8000;
keepalive 16;
}
server {
listen 9001 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;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets off;
server_tokens off;
client_max_body_size 220m;
client_body_timeout 300s;
send_timeout 300s;
access_log /var/log/nginx/an.xiaoan.xyz.access.log;
error_log /var/log/nginx/an.xiaoan.xyz.error.log warn;
# 云端 Nginx 会传 X-Forwarded-Proto: https;若没有(例如直接访问本机),
# 也按 HTTPS 处理,因为公网入口固定是云端 443。
set $forwarded_proto $http_x_forwarded_proto;
if ($forwarded_proto = "") {
set $forwarded_proto https;
}
# AI 摘要流式接口:关闭缓冲,保证逐段到达浏览器
location = /digest/stream/ {
proxy_pass http://an_xiaoan_app;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $forwarded_proto;
proxy_set_header Connection "";
proxy_connect_timeout 10s;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
proxy_buffering off;
proxy_cache off;
}
location /static/ {
alias /home/myserver/my-site/staticfiles/;
expires 30d;
access_log off;
}
location /media/ {
alias /home/myserver/my-site/media/;
expires 7d;
access_log off;
}
location / {
proxy_pass http://an_xiaoan_app;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $forwarded_proto;
proxy_set_header Connection "";
proxy_connect_timeout 10s;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
proxy_request_buffering off;
}
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Content-Type-Options nosniff always;
add_header X-Frame-Options SAMEORIGIN always;
add_header Referrer-Policy strict-origin-when-cross-origin always;
}
+9
View File
@@ -0,0 +1,9 @@
# an.xiaoan.xyz / Django blogsite production dependencies
# Python 3.10
Django==5.2.18
djangorestframework==3.15.2
PyMySQL==1.1.1
Pillow==12.3.0
Markdown==3.10.3
xmindparser==1.2.2
gunicorn==27.0.1
+19
View File
@@ -0,0 +1,19 @@
[Unit]
Description=an.xiaoan.xyz Django app (Gunicorn)
After=network-online.target mysql.service
Wants=network-online.target
[Service]
Type=simple
User=root
Group=root
WorkingDirectory=/home/myserver/my-site
EnvironmentFile=/etc/an-xiaoan.env
ExecStart=/root/anaconda3/envs/myblog/bin/gunicorn blogsite.wsgi:application --bind 127.0.0.1:8000 --workers 1 --threads 4 --worker-class gthread --timeout 300 --graceful-timeout 30 --access-logfile /var/log/an-xiaoan/access.log --error-logfile /var/log/an-xiaoan/error.log --log-level info
Restart=always
RestartSec=3
KillSignal=SIGTERM
TimeoutStopSec=35
[Install]
WantedBy=multi-user.target
+18
View File
@@ -0,0 +1,18 @@
[Unit]
Description=frp client (an.xiaoan.xyz / git.xiaoan.xyz)
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=root
Group=root
WorkingDirectory=/home/frp/frp_0.61.0_linux_amd64
ExecStart=/home/frp/frp_0.61.0_linux_amd64/frpc -c /home/frp/frp_0.61.0_linux_amd64/frpc.toml
Restart=always
RestartSec=5
LimitNOFILE=1048576
Environment=TZ=Asia/Shanghai
[Install]
WantedBy=multi-user.target