Files

149 lines
5.1 KiB
Bash
Executable File
Raw Permalink 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.
#!/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"