From 777f5367da110548dca38bcd3e68d990e6e1e562 Mon Sep 17 00:00:00 2001 From: Zhengen TANG Date: Fri, 11 Sep 2026 09:22:49 +0800 Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E7=BD=B2=E9=85=8D=E7=BD=AE=EF=BC=9A?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=A3=B8=E6=9C=BA=EF=BC=88=E9=9D=9E=20Docker?= =?UTF-8?q?=EF=BC=89=E9=83=A8=E7=BD=B2=E6=89=80=E9=9C=80=E7=9A=84=20system?= =?UTF-8?q?d/Nginx=20=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - deploy/systemd/wagtailcms-backend.service:gunicorn 后端服务,unix socket 监听 - deploy/systemd/wagtailcms-frontend.service:Next.js 前端服务(npm run start) - deploy/nginx/wagtailcms.conf:反向代理模板,区分管理后台域名与前端站点域名 供阿里云 ECS 等裸机环境不使用 Docker 时直接部署参考。 --- deploy/nginx/wagtailcms.conf | 68 ++++++++++++++++++++++ deploy/systemd/wagtailcms-backend.service | 30 ++++++++++ deploy/systemd/wagtailcms-frontend.service | 22 +++++++ 3 files changed, 120 insertions(+) create mode 100644 deploy/nginx/wagtailcms.conf create mode 100644 deploy/systemd/wagtailcms-backend.service create mode 100644 deploy/systemd/wagtailcms-frontend.service diff --git a/deploy/nginx/wagtailcms.conf b/deploy/nginx/wagtailcms.conf new file mode 100644 index 0000000..658bfbb --- /dev/null +++ b/deploy/nginx/wagtailcms.conf @@ -0,0 +1,68 @@ +# wagtailcms 生产环境 Nginx 反向代理配置模板 +# 使用方法: +# 1. 拷贝到 /etc/nginx/sites-available/wagtailcms.conf(Debian/Ubuntu) +# 或 /etc/nginx/conf.d/wagtailcms.conf(CentOS/Alibaba Cloud Linux) +# 2. 将下面的 example.com / cms.example.com 替换为真实域名 +# 3. Debian/Ubuntu 需要 ln -s ../sites-available/wagtailcms.conf /etc/nginx/sites-enabled/ +# 4. nginx -t && systemctl reload nginx +# +# 架构:浏览器 -> Nginx(TLS 终止) -> {Next.js:3000(页面渲染) | gunicorn unix socket(/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 /srv/wagtailcms/staticfiles/; + access_log off; + expires 30d; + } + + location /media/ { + alias /srv/wagtailcms/media/; + access_log off; + expires 7d; + } + + location / { + proxy_pass http://unix:/run/wagtailcms/gunicorn.sock; + 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; + } +} + +# 提示:拿到证书后(certbot --nginx 或阿里云 SSL 证书服务), +# certbot 会自动把以上两个 server 块改造成 443 + HTTP->HTTPS 跳转; +# 也可以手动加 `listen 443 ssl;` + `ssl_certificate`/`ssl_certificate_key` 指令。 diff --git a/deploy/systemd/wagtailcms-backend.service b/deploy/systemd/wagtailcms-backend.service new file mode 100644 index 0000000..3bbb03d --- /dev/null +++ b/deploy/systemd/wagtailcms-backend.service @@ -0,0 +1,30 @@ +[Unit] +Description=wagtailcms Django/Wagtail backend (gunicorn) +After=network.target postgresql.service redis-server.service +Wants=postgresql.service redis-server.service + +[Service] +Type=simple +User=wagtailcms +Group=www-data +WorkingDirectory=/srv/wagtailcms +EnvironmentFile=/srv/wagtailcms/.env +RuntimeDirectory=wagtailcms +RuntimeDirectoryMode=0755 +ExecStart=/srv/wagtailcms/.venv/bin/gunicorn wagtailcms.wsgi:application \ + --bind unix:/run/wagtailcms/gunicorn.sock \ + --workers 3 \ + --timeout 60 \ + --access-logfile - \ + --error-logfile - +ExecReload=/bin/kill -s HUP $MAINPID +Restart=on-failure +RestartSec=5 + +# 加固(可按需调整) +NoNewPrivileges=true +PrivateTmp=true +ProtectSystem=full + +[Install] +WantedBy=multi-user.target diff --git a/deploy/systemd/wagtailcms-frontend.service b/deploy/systemd/wagtailcms-frontend.service new file mode 100644 index 0000000..15a358c --- /dev/null +++ b/deploy/systemd/wagtailcms-frontend.service @@ -0,0 +1,22 @@ +[Unit] +Description=wagtailcms Next.js frontend +After=network.target wagtailcms-backend.service + +[Service] +Type=simple +User=wagtailcms +Group=wagtailcms +WorkingDirectory=/srv/wagtailcms/frontend +EnvironmentFile=/srv/wagtailcms/frontend/.env.production +Environment=PORT=3000 +Environment=NODE_ENV=production +ExecStart=/usr/bin/npm run start +Restart=on-failure +RestartSec=5 + +NoNewPrivileges=true +PrivateTmp=true +ProtectSystem=full + +[Install] +WantedBy=multi-user.target