部署配置:新增 Windows Server 裸机部署流程

- requirements/production-windows.txt:Windows 专用生产依赖,用 waitress 替代
  不支持 Windows 的 gunicorn
- deploy/windows/serve-backend.ps1:waitress WSGI 启动脚本
- deploy/windows/install-nssm-services.ps1:用 NSSM 将前后端注册为 Windows 服务
- deploy/windows/nginx-wagtailcms.conf:nginx for Windows 反向代理模板(TCP 端口,
  不用 unix socket)
- documents/Windows服务器部署指南.md:完整部署步骤文档,并说明与 Linux 裸机
  部署(systemd/gunicorn/unix socket/Redis)的差异点
This commit is contained in:
2026-09-11 09:39:30 +08:00
parent 777f5367da
commit 1686794ad7
5 changed files with 384 additions and 0 deletions
+64
View File
@@ -0,0 +1,64 @@
<#
使用 NSSM(Non-Sucking Service Manager,https://nssm.cc/download)将后端/前端
注册为 Windows 服务,实现开机自启 + 崩溃自动重启。
前置条件:
- 已下载 nssm.exe 并加入 PATH(或把完整路径填到 $Nssm 变量)
- 后端虚拟环境已就绪:<repo>\.venv
- 前端依赖已安装 + 已 `npm run build`:<repo>\frontend
用法(管理员 PowerShell):
cd C:\wagtailcms
.\deploy\windows\install-nssm-services.ps1
#>
$ErrorActionPreference = "Stop"
$Nssm = "nssm.exe" # 若 nssm 不在 PATH,改成绝对路径,如 "C:\tools\nssm-2.24\win64\nssm.exe"
$RepoRoot = Resolve-Path (Join-Path $PSScriptRoot "..\..")
$Powershell = (Get-Command powershell.exe).Source
function Install-NssmService {
param(
[string]$Name,
[string]$Application,
[string]$Arguments,
[string]$WorkingDirectory
)
& $Nssm stop $Name 2>$null | Out-Null
& $Nssm remove $Name confirm 2>$null | Out-Null
& $Nssm install $Name $Application $Arguments
& $Nssm set $Name AppDirectory $WorkingDirectory
& $Nssm set $Name AppStdout (Join-Path $WorkingDirectory "logs\$Name-stdout.log")
& $Nssm set $Name AppStderr (Join-Path $WorkingDirectory "logs\$Name-stderr.log")
& $Nssm set $Name AppRotateFiles 1
& $Nssm set $Name AppRotateBytes 10485760
& $Nssm set $Name Start SERVICE_AUTO_START
& $Nssm set $Name AppRestartDelay 5000
Write-Host "已注册服务:$Name" -ForegroundColor Green
}
New-Item -ItemType Directory -Force -Path (Join-Path $RepoRoot "logs") | Out-Null
New-Item -ItemType Directory -Force -Path (Join-Path $RepoRoot "frontend\logs") | Out-Null
# --- 后端:wagtailcms-backend(waitress WSGI,监听 127.0.0.1:8000) -------------
Install-NssmService `
-Name "wagtailcms-backend" `
-Application $Powershell `
-Arguments "-NoProfile -ExecutionPolicy Bypass -File `"$RepoRoot\deploy\windows\serve-backend.ps1`"" `
-WorkingDirectory $RepoRoot
# --- 前端:wagtailcms-frontend(Next.js, npm run start,监听 3000) -------------
$npmCmd = (Get-Command npm.cmd).Source
Install-NssmService `
-Name "wagtailcms-frontend" `
-Application $npmCmd `
-Arguments "run start" `
-WorkingDirectory (Join-Path $RepoRoot "frontend")
Write-Host ""
Write-Host "启动服务:Start-Service wagtailcms-backend; Start-Service wagtailcms-frontend" -ForegroundColor Yellow
Write-Host "查看状态:Get-Service wagtailcms-*" -ForegroundColor Yellow
+72
View File
@@ -0,0 +1,72 @@
# wagtailcms Windows Server 生产环境 Nginx 反向代理配置模板
# (Windows 版 nginx 不支持 Unix Domain Socket 反向代理到 gunicorn,
# 后端改用监听 127.0.0.1:8000 的 waitress,因此这里全部走 TCP 端口。)
#
# 使用方法:
# 1. 下载 nginx for Windows(http://nginx.org/en/download.html),解压到 C:\nginx
# 2. 将本文件内容追加/替换到 C:\nginx\conf\nginx.conf 的 http{} 块内
# (或用 include 语法:在 http{} 块中加一行 include wagtailcms.conf;,
# 并把本文件放到 C:\nginx\conf\wagtailcms.conf)
# 3. 将下面的 example.com / cms.example.com 替换为真实域名
# 4. nginx.exe -t 测试配置,nginx.exe -s reload 重载
#
# 架构:浏览器 -> Nginx(TLS 终止) -> {Next.js:3000(页面渲染) | waitress:8000(/admin /api /static /media 等后端路径)}
# ---------------------------------------------------------------------------
# 后端管理域名(Wagtail 管理后台 / API),对应 WAGTAILADMIN_BASE_URL
# ---------------------------------------------------------------------------
server {
listen 80;
server_name cms.example.com;
client_max_body_size 20m;
location /static/ {
alias C:/wagtailcms/staticfiles/;
access_log off;
expires 30d;
}
location /media/ {
alias C:/wagtailcms/media/;
access_log off;
expires 7d;
}
location / {
proxy_pass http://127.0.0.1:8000;
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-Proto $scheme;
}
}
# ---------------------------------------------------------------------------
# 前端站点域名(Next.js 页面),公众访问入口
# ---------------------------------------------------------------------------
server {
listen 80;
server_name www.example.com example.com;
client_max_body_size 20m;
location /_next/static/ {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
expires 30d;
access_log off;
}
location / {
proxy_pass http://127.0.0.1:3000;
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-Proto $scheme;
}
}
# 提示:申请到证书后(win-acme / 阿里云 SSL 证书服务下载 Nginx 格式证书),
# 手动加 `listen 443 ssl;` + `ssl_certificate`/`ssl_certificate_key` 指令,
# 并在 80 端口 server 块中加 `return 301 https://$host$request_uri;` 强制跳转。
+21
View File
@@ -0,0 +1,21 @@
# 后端 WSGI 启动脚本(waitress,供 NSSM 注册为 Windows 服务时调用)。
#
# 用法(供 NSSM 服务的 Application 字段指向):
# powershell.exe -NoProfile -ExecutionPolicy Bypass -File C:\wagtailcms\deploy\windows\serve-backend.ps1
#
# 也可以直接双击/命令行手动运行,用于本地验证生产配置是否能正常启动。
$ErrorActionPreference = "Stop"
# 仓库根目录(本脚本位于 <repo>\deploy\windows\ 下)
$RepoRoot = Resolve-Path (Join-Path $PSScriptRoot "..\..")
Set-Location $RepoRoot
$env:DJANGO_SETTINGS_MODULE = "wagtailcms.settings.production"
$python = Join-Path $RepoRoot ".venv\Scripts\python.exe"
if (-not (Test-Path $python)) {
throw "找不到虚拟环境解释器:$python,请先创建虚拟环境并安装 requirements/production-windows.txt"
}
& $python -m waitress --listen=127.0.0.1:8000 --threads=8 wagtailcms.wsgi:application