diff --git a/deploy/windows/install-nssm-services.ps1 b/deploy/windows/install-nssm-services.ps1 new file mode 100644 index 0000000..2789311 --- /dev/null +++ b/deploy/windows/install-nssm-services.ps1 @@ -0,0 +1,64 @@ +<# +使用 NSSM(Non-Sucking Service Manager,https://nssm.cc/download)将后端/前端 +注册为 Windows 服务,实现开机自启 + 崩溃自动重启。 + +前置条件: + - 已下载 nssm.exe 并加入 PATH(或把完整路径填到 $Nssm 变量) + - 后端虚拟环境已就绪:\.venv + - 前端依赖已安装 + 已 `npm run build`:\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 diff --git a/deploy/windows/nginx-wagtailcms.conf b/deploy/windows/nginx-wagtailcms.conf new file mode 100644 index 0000000..30f156c --- /dev/null +++ b/deploy/windows/nginx-wagtailcms.conf @@ -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;` 强制跳转。 diff --git a/deploy/windows/serve-backend.ps1 b/deploy/windows/serve-backend.ps1 new file mode 100644 index 0000000..3d8b183 --- /dev/null +++ b/deploy/windows/serve-backend.ps1 @@ -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" + +# 仓库根目录(本脚本位于 \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 diff --git a/documents/Windows服务器部署指南.md b/documents/Windows服务器部署指南.md new file mode 100644 index 0000000..5b8b021 --- /dev/null +++ b/documents/Windows服务器部署指南.md @@ -0,0 +1,217 @@ +# Windows Server 部署指南(裸机,非 Docker) + +配套文件: +- [`deploy/windows/nginx-wagtailcms.conf`](../deploy/windows/nginx-wagtailcms.conf):nginx for Windows 反向代理模板 +- [`deploy/windows/serve-backend.ps1`](../deploy/windows/serve-backend.ps1):waitress 启动脚本 +- [`deploy/windows/install-nssm-services.ps1`](../deploy/windows/install-nssm-services.ps1):用 NSSM 把前后端注册为 Windows 服务 +- [`requirements/production-windows.txt`](../requirements/production-windows.txt):Windows 专用生产依赖(用 waitress 替代不支持 Windows 的 gunicorn) + +架构:浏览器 → Nginx for Windows(TLS 终止)→ { Next.js(127.0.0.1:3000) | waitress(127.0.0.1:8000) }, +两个进程均由 NSSM 注册为 Windows 服务,实现开机自启 + 崩溃自动重启。 + +## 1. 服务器基础环境 + +- Windows Server 2019/2022 +- [Python 3.12+](https://www.python.org/downloads/windows/)(安装时勾选 "Add to PATH") +- [Node.js 20 LTS](https://nodejs.org/) +- [PostgreSQL 16](https://www.postgresql.org/download/windows/)(安装向导里设置好 `postgres` 超级用户密码) +- [Git for Windows](https://git-scm.com/download/win) +- [nginx for Windows](http://nginx.org/en/download.html)(解压即用,无需安装) +- [NSSM](https://nssm.cc/download)(解压后把 `win64\nssm.exe` 所在目录加入系统 `PATH`) +- Redis:Windows 官方不再维护 Redis,推荐用 [Memurai](https://www.memurai.com/)(Redis 协议兼容,提供 Windows 原生安装包和 Windows 服务)替代,或者改用 [WSL2](https://learn.microsoft.com/windows/wsl/) 内跑 Redis。若暂时没有 Redis/Memurai,也可以先让 `REDIS_URL` 指向一台已有的 Redis(如局域网内的 Linux 机器),后续再补齐。 + +## 2. 拉取代码 + +```powershell +New-Item -ItemType Directory -Force -Path C:\wagtailcms +cd C:\wagtailcms +git clone https://git.shiytech.cn/Zhengen/wagtailcms.git . +git clone <前端仓库地址> frontend +``` + +## 3. 后端 Python 环境 + +```powershell +cd C:\wagtailcms +python -m venv .venv +.\.venv\Scripts\pip install -U pip +.\.venv\Scripts\pip install -r requirements\production-windows.txt +``` + +> `production-windows.txt` 用 `waitress` 代替 `gunicorn`——gunicorn 依赖 `fcntl`/`os.fork`,不支持 Windows。 + +## 4. PostgreSQL + +用 `psql`(安装 PostgreSQL 时自带,在开始菜单 "SQL Shell (psql)")或 pgAdmin 执行: + +```sql +CREATE DATABASE enterprise_cms; +CREATE USER wagtailcms_user WITH PASSWORD '换成强密码'; +GRANT ALL PRIVILEGES ON DATABASE enterprise_cms TO wagtailcms_user; +``` + +## 5. 配置 `.env` + +在 `C:\wagtailcms\.env` 基于 [`.env.example`](../.env.example) 填写真实值: + +```ini +SECRET_KEY=<用 python -c "import secrets;print(secrets.token_urlsafe(50))" 生成> +ALLOWED_HOSTS=cms.example.com +WAGTAILADMIN_BASE_URL=https://cms.example.com + +DATABASE_URL=postgres://wagtailcms_user:换成强密码@localhost:5432/enterprise_cms +REDIS_URL=redis://localhost:6379/1 + +EMAIL_HOST=smtp.shiytech.cn +EMAIL_PORT=587 +EMAIL_HOST_USER=support@shiytech.cn +EMAIL_HOST_PASSWORD=<真实密码> +DEFAULT_FROM_EMAIL=support@shiytech.cn + +CORS_ALLOWED_ORIGINS=https://www.example.com +CSRF_TRUSTED_ORIGINS=https://www.example.com,https://cms.example.com + +OSS_ACCESS_KEY_ID=... +OSS_SECRET_ACCESS_KEY=... +OSS_BUCKET_NAME=... +OSS_ENDPOINT_URL=... +OSS_CDN_DOMAIN=... + +FRONTEND_REVALIDATE_URL=https://www.example.com/api/revalidate +REVALIDATE_SECRET=<强随机值> +FRONTEND_BASE_URL=https://www.example.com + +DJANGO_SUPERUSER_USERNAME=admin +DJANGO_SUPERUSER_EMAIL=admin@example.com +DJANGO_SUPERUSER_PASSWORD=<强密码> +``` + +## 6. 初始化数据库和内容 + +```powershell +cd C:\wagtailcms +$env:DJANGO_SETTINGS_MODULE = "wagtailcms.settings.production" +.\.venv\Scripts\python manage.py migrate +.\.venv\Scripts\python manage.py collectstatic --noinput +.\.venv\Scripts\python manage.py bootstrap_deployment +``` + +最后一步会自动创建超级管理员账号、RBAC 权限组、首页与隐私政策页面。 + +## 7. 前端构建 + +```powershell +cd C:\wagtailcms\frontend +# 配置 frontend\.env.production: +# NEXT_PUBLIC_API_URL=https://cms.example.com +# REVALIDATE_SECRET=<与后端 .env 中一致> +npm ci +npm run build +``` + +## 8. 用 NSSM 注册 Windows 服务 + +管理员权限打开 PowerShell: + +```powershell +cd C:\wagtailcms +.\deploy\windows\install-nssm-services.ps1 + +Start-Service wagtailcms-backend +Start-Service wagtailcms-frontend +Get-Service wagtailcms-* +``` + +服务日志分别写在 `C:\wagtailcms\logs\` 和 `C:\wagtailcms\frontend\logs\`(自动按 10MB 滚动)。 + +> `serve-backend.ps1` 内部用 `waitress-serve` 监听 `127.0.0.1:8000`,`wagtailcms-frontend` +> 服务直接执行 `npm run start`(Next.js 默认监听 3000)。两者都设置了 +> `Start=SERVICE_AUTO_START`(开机自启)+ `AppRestartDelay=5000`(崩溃 5 秒后自动重启)。 + +## 9. Nginx for Windows 反向代理 + +```powershell +# 解压 nginx 到 C:\nginx +Copy-Item C:\wagtailcms\deploy\windows\nginx-wagtailcms.conf C:\nginx\conf\wagtailcms.conf +``` + +编辑 `C:\nginx\conf\nginx.conf`,在 `http {}` 块内加一行: +``` +include wagtailcms.conf; +``` +再编辑 `C:\nginx\conf\wagtailcms.conf`,把 `example.com`/`cms.example.com` 替换为真实域名, +并确认 `alias` 路径(静态/媒体文件目录)与实际部署路径一致。 + +将 nginx 本身也注册为 Windows 服务(同样用 NSSM,或使用 nginx 社区提供的 +[nssm 包装/其它 Windows 服务化方案](http://nginx.org/en/docs/windows.html)): + +```powershell +nssm install nginx C:\nginx\nginx.exe +nssm set nginx AppDirectory C:\nginx +Start-Service nginx +``` + +测试配置、重载: +```powershell +C:\nginx\nginx.exe -t +C:\nginx\nginx.exe -s reload +``` + +## 10. HTTPS 证书 + +Windows 上常用方案: +- [win-acme](https://www.win-acme.com/)(Let's Encrypt 客户端,原生支持 Windows/IIS,也可手动导出证书给 nginx 用) +- 阿里云 SSL 证书服务:申请后下载 "Nginx" 格式证书(`.pem` + `.key`), + 在 `nginx-wagtailcms.conf` 的两个 `server` 块补充: + ``` + listen 443 ssl; + ssl_certificate C:/nginx/ssl/cms.example.com.pem; + ssl_certificate_key C:/nginx/ssl/cms.example.com.key; + ``` + 并在对应的 `listen 80` 块中加 `return 301 https://$host$request_uri;` 强制跳转。 + +## 11. 防火墙 / 安全组 + +```powershell +New-NetFirewallRule -DisplayName "HTTP" -Direction Inbound -LocalPort 80 -Protocol TCP -Action Allow +New-NetFirewallRule -DisplayName "HTTPS" -Direction Inbound -LocalPort 443 -Protocol TCP -Action Allow +``` +云服务商控制台安全组同样放行 80/443;`8000`/`3000` 只监听 `127.0.0.1`,无需(也不应该)对公网开放。 + +## 12. 验证清单 + +- `Get-Service wagtailcms-*, nginx, postgresql-x64-16` 均为 Running +- 浏览器访问 `https://cms.example.com/admin/` 能登录 +- 访问 `https://www.example.com` 首页正常 +- 提交线索表单能收到通知邮件 +- 发布页面后前端能通过 revalidate webhook 刷新缓存 + +## 13. 升级 / 重新部署 + +```powershell +cd C:\wagtailcms +git pull +.\.venv\Scripts\pip install -r requirements\production-windows.txt +$env:DJANGO_SETTINGS_MODULE = "wagtailcms.settings.production" +.\.venv\Scripts\python manage.py migrate +.\.venv\Scripts\python manage.py collectstatic --noinput +Restart-Service wagtailcms-backend + +cd frontend +git pull +npm ci +npm run build +Restart-Service wagtailcms-frontend +``` + +## 已知差异 / 注意事项(相对 Linux 部署) + +- **gunicorn → waitress**:gunicorn 不支持 Windows,改用纯 Python 实现的 waitress WSGI 服务器, + 性能对中小流量站点足够,无需 Unix Domain Socket。 +- **systemd → NSSM**:Windows 没有 systemd,用 NSSM 把两个长驻进程注册为 Windows 服务, + 效果等价(开机自启、崩溃重启、日志滚动)。 +- **Nginx unix socket → TCP**:Windows 版 nginx 反向代理统一走 `127.0.0.1:端口`,不用 unix socket。 +- **Redis → Memurai(或远程 Redis)**:Redis 官方不提供 Windows 版本,需要用 Memurai 或指向已有的 + Linux Redis 实例。 +- 其余(.env 配置项、`bootstrap_deployment` 行为、Nginx 反代规则本身)与 + [`documents/设计方案分析与完善版.md`](设计方案分析与完善版.md) 和 Linux 裸机部署流程一致。 diff --git a/requirements/production-windows.txt b/requirements/production-windows.txt new file mode 100644 index 0000000..3d248c9 --- /dev/null +++ b/requirements/production-windows.txt @@ -0,0 +1,10 @@ +# Windows Server 部署专用(gunicorn 依赖 fcntl/os.fork,不支持 Windows, +# 改用纯 Python 实现的 WSGI 服务器 waitress)。 +# 其余依赖与 production.txt 一致。 +-r base.txt + +psycopg[binary]>=3.2 +django-storages[s3]>=1.14 +waitress>=3.0,<4.0 +sentry-sdk>=2.0 +whitenoise>=6.7,<7.0