Skip to content

Commit 9dac400

Browse files
committed
deploy and trpc outport
1 parent 5e05f28 commit 9dac400

File tree

5 files changed

+494
-2
lines changed

5 files changed

+494
-2
lines changed

docx/tutorial/40_deploy_to_your_server.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,72 @@
1+
### 推荐:一键部署包(Windows/Linux 一键 deploy)
2+
3+
本文档已更新,原有的“手动拉代码 + PM2 + Nginx”步骤部分过时。现推荐使用项目内置的“一键部署包”,更像整合包:本地打包,服务器解压后点击 `deploy.bat`(Windows)或执行 `deploy.sh`(Linux)即可完成停服→装依赖(可选)→迁移(可选)→启动。
4+
5+
#### 本地打包
6+
7+
```bash
8+
npm i
9+
# 任选其一
10+
npm run pack:win # 只打 Windows 包(full:包含 node_modules)
11+
npm run pack:linux # 只打 Linux 包
12+
npm run pack:all # 两个平台都打
13+
```
14+
15+
打包产物位于 `dist/packages/`,名称形如:`bwb-win-full-vYYYYMMDD_HHMMSS.zip``bwb-linux-full-...zip`
16+
17+
#### 服务器部署(Windows)
18+
19+
1) 解压 zip 到目标目录
20+
2) 在包目录准备 `.env.publish`(存在则优先)或 `.env`
21+
- 最小示例:
22+
```env
23+
NODE_ENV=production
24+
HOST=0.0.0.0
25+
PORT=8088
26+
JWT_SECRET=请改为强随机
27+
# 任选其一
28+
DATABASE_URL=file:./data/prod.db
29+
# DATABASE_URL=postgresql://user:pass@host:5432/dbname?schema=public
30+
MIGRATE_ON_DEPLOY=1
31+
```
32+
- 若用 SQLite:先 `mkdir data`
33+
3) 双击 `deploy.bat`
34+
4) 防火墙放行新端口(示例 8088)
35+
36+
验证:`http://服务器IP:8088/health``/test.html`
37+
38+
#### 服务器部署(Linux)
39+
40+
1) 解压 zip 到目标目录
41+
2) 准备 `.env.publish`(同上)并(如需)`mkdir -p data`
42+
3) 执行:
43+
```bash
44+
bash deploy.sh
45+
```
46+
47+
验证:`curl http://127.0.0.1:8088/health`
48+
49+
#### 端口与 CORS
50+
51+
- 改端口:在 `.env.publish` 设置 `PORT=你想要的端口`,同时放行防火墙;若有反代,更新转发目标端口。
52+
- 跨域:如有额外前端域名,设置 `CORS_ADDITIONAL_ORIGINS=https://a.com,https://b.com`
53+
54+
#### HTTPS/证书(简述)
55+
56+
- Windows:建议 IIS 反向代理 443 → `http://127.0.0.1:PORT`,绑定证书。
57+
- Linux:用 Nginx/Caddy 反代。Nginx + Certbot:
58+
```bash
59+
sudo apt install -y nginx certbot python3-certbot-nginx
60+
sudo certbot --nginx -d api.yourdomain.com
61+
```
62+
63+
#### 包类型选择
64+
65+
- full:包含 `node_modules`,服务器更省事,包更大。
66+
- server:不含 `node_modules`,上线机安装依赖,包更小。
67+
- modules:仅 `node_modules`,用于只更新依赖的场景。
68+
69+
—— 下文为“传统手动部署(保留)”,供需要精细化控制时参考 ——
170

271
-----
372

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
### 41|为你的后端启用 HTTPS(证书获取与自动续期)
2+
3+
本篇讲清楚如何用你自己的域名,免费签发并自动续期 HTTPS 证书。分 Windows Server 与 Linux 两套做法。
4+
5+
#### 前置条件
6+
7+
- 已拥有一个域名
8+
- 域名 DNS 已添加 A 记录指向你的服务器公网 IP
9+
- 放行 80/443 端口
10+
11+
---
12+
13+
## Windows Server:IIS + win-acme(推荐)
14+
15+
目标:用 IIS 反向代理到 Node(监听内网端口,如 8088),证书由 win-acme 自动签发与续期。
16+
17+
### 步骤 1:安装 IIS 与必须组件
18+
19+
1) 打开“添加角色和功能”,勾选 Web 服务器(IIS)
20+
2) 额外安装:URL Rewrite、Application Request Routing(ARR)
21+
- 可在微软官网/微软 Web 平台安装程序中安装
22+
23+
### 步骤 2:创建站点与反向代理
24+
25+
1) 在 IIS 管理器中新建网站
26+
- 站点名:你的域名
27+
- 物理路径:任意空目录
28+
- 绑定:先只绑定 HTTP:80 + 你的域名
29+
2) 启用反向代理
30+
- 在根节点“Application Request Routing Cache”→ Server Proxy Settings → 勾选 Enable Proxy
31+
3) 配置 URL Rewrite 反向代理到 Node
32+
- 在站点下“URL Rewrite”→ Add Rule(s) → Reverse Proxy → 填写内网目标 `127.0.0.1:你的PORT`
33+
34+
此时用 `http://你的域名/health` 应可访问到 Node。
35+
36+
### 步骤 3:安装并运行 win-acme(wacs)签发证书
37+
38+
1) 下载 win-acme(wacs.exe)
39+
- 访问 `https://www.win-acme.com/` → Downloads → x64 便携版
40+
2) 解压到如 `C:\tools\win-acme`,以管理员运行 `wacs.exe`
41+
3) 交互选择
42+
- N:Create new certificate (simple)
43+
- 选择你的站点或手动输入域名
44+
- HTTP-01 验证(默认),程序会在 80 口自动完成验证
45+
- 自动将证书安装到 IIS 并创建 HTTPS:443 绑定
46+
- 创建计划任务,实现自动续期
47+
48+
完成后,`https://你的域名` 生效。
49+
50+
### 步骤 4:强制跳转 HTTPS(可选)
51+
52+
在站点下“URL Rewrite”添加 HTTPS 重定向规则,将 HTTP 重定向到 HTTPS。
53+
54+
### 常见问题
55+
56+
- 80 端口必须对公网可达,否则 HTTP-01 无法验证
57+
- 若使用 CDN/Cloudflare 且橙云代理,颁发时建议临时灰云,或使用 DNS-01(win-acme 也支持)
58+
- 续期由计划任务自动完成;如需手动续期,可再次运行 `wacs.exe`
59+
- 导出 PFX:打开“计算机证书”MMC → 个人 → 证书 → 找到你的域名证书 → 右键“所有任务”→ 导出(包含私钥)
60+
61+
---
62+
63+
## Linux:Nginx + Certbot(推荐)
64+
65+
目标:Nginx 反向代理 443/80,Certbot 自动签发与续期。
66+
67+
### 步骤 1:安装 Nginx 与 Certbot
68+
69+
```bash
70+
sudo apt update
71+
sudo apt install -y nginx certbot python3-certbot-nginx
72+
```
73+
74+
### 步骤 2:配置站点并反代到 Node
75+
76+
```bash
77+
sudo nano /etc/nginx/sites-available/api.yourdomain.com
78+
```
79+
80+
```nginx
81+
server {
82+
listen 80;
83+
server_name api.yourdomain.com;
84+
location / {
85+
proxy_pass http://127.0.0.1:8088;
86+
proxy_http_version 1.1;
87+
proxy_set_header Upgrade $http_upgrade;
88+
proxy_set_header Connection "upgrade";
89+
proxy_set_header Host $host;
90+
}
91+
}
92+
```
93+
94+
```bash
95+
sudo ln -s /etc/nginx/sites-available/api.yourdomain.com /etc/nginx/sites-enabled/
96+
sudo nginx -t
97+
sudo systemctl reload nginx
98+
```
99+
100+
确保 `http://api.yourdomain.com/health` 可达。
101+
102+
### 步骤 3:一键签发与自动续期
103+
104+
```bash
105+
sudo certbot --nginx -d api.yourdomain.com
106+
```
107+
108+
选择强制跳转 HTTPS。完成后自动创建定时续期任务。
109+
110+
验证:`https://api.yourdomain.com/health`
111+
112+
### 证书位置与续期
113+
114+
- 证书目录:`/etc/letsencrypt/live/你的域名/`
115+
- 常用文件:`fullchain.pem``privkey.pem`
116+
- 自动续期:每日检查,到期前会自动续期
117+
118+
```bash
119+
sudo systemctl list-timers | grep certbot
120+
sudo certbot renew --dry-run
121+
```
122+
123+
### 备选:不改 Nginx,仅临时签证书
124+
125+
```bash
126+
sudo systemctl stop nginx
127+
sudo certbot certonly --standalone -d api.yourdomain.com
128+
sudo systemctl start nginx
129+
```
130+
131+
或使用 DNS-01(适合在 80/443 受限、或需通配符证书):
132+
133+
- 安装对应 DNS 插件(如 Cloudflare:`python3-certbot-dns-cloudflare`
134+
- 按插件文档配置 API Token 后执行带 `--dns-...` 的 certbot 命令
135+
136+
---
137+
138+
## Node 直挂 443 与导出证书
139+
140+
- 生产建议用反向代理托管证书,不建议 Node 进程直接监听 443
141+
- 如确需 Node TLS,Linux 下使用 `fullchain.pem``privkey.pem`
142+
- Windows 下可在证书管理器导出 PFX,并在 Node 中加载
143+
144+
---
145+
146+
## 排错
147+
148+
- 域名未解析或解析未生效:检查 DNS A 记录与 TTL
149+
- 80/443 未通:检查云服务商安全组与系统防火墙
150+
- 被占用端口:调整 Nginx 端口或释放占用
151+
- Cloudflare 橙云:签发阶段临时灰云,或改用 DNS-01
152+
153+
---
154+
155+
## 最佳实践
156+
157+
- 后端仅监听内网端口,如 8088
158+
- 所有外部访问通过 IIS/Nginx 走 HTTPS
159+
- 自动续期要么由 win-acme 计划任务,要么由 certbot 定时任务
160+
- 生产前用 `https://www.ssllabs.com/ssltest/` 检查评分
161+
162+

package.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@
2525
"template:apply:blocks": "APPLY=1 BLOCKS_ONLY=1 bash scripts/update-from-template.sh",
2626
"hooks:install": "bash scripts/install-git-hooks.sh",
2727
"test": "echo \"Error: no test specified\" && exit 1",
28-
"vercel-build": "npm run build"
28+
"vercel-build": "npm run build",
29+
"pack:win": "node scripts/pack.js win full",
30+
"pack:win:server": "node scripts/pack.js win server",
31+
"pack:win:modules": "node scripts/pack.js win modules",
32+
"pack:linux": "node scripts/pack.js linux full",
33+
"pack:linux:server": "node scripts/pack.js linux server",
34+
"pack:linux:modules": "node scripts/pack.js linux modules",
35+
"pack:all": "node scripts/pack.js all full"
2936
},
3037
"repository": {
3138
"type": "git",
@@ -46,7 +53,6 @@
4653
"@fastify/cors": "^11.1.0",
4754
"@fastify/static": "^8.2.0",
4855
"@prisma/client": "^5.17.0",
49-
"@trpc/server": "^11.4.3",
5056
"@types/jsonwebtoken": "^9.0.6",
5157
"@types/node": "^20.14.12",
5258
"fastify": "^5.4.0",
@@ -55,8 +61,14 @@
5561
"resend": "^3.5.0",
5662
"zod": "^3.23.8"
5763
},
64+
"peerDependencies": {
65+
"@trpc/server": "^11.4.3",
66+
"zod": "^3.23.8"
67+
},
5868
"devDependencies": {
5969
"dotenv-cli": "^7.4.4",
70+
"@trpc/server": "^11.4.3",
71+
"archiver": "^6.0.1",
6072
"ts-node-dev": "^2.0.0",
6173
"typescript": "^5.9.2"
6274
}

0 commit comments

Comments
 (0)