16 KiB
宝塔面板部署指南 - 服务器 121.41.52.85
部署信息
| 项目 | 值 |
|---|---|
| 服务器 IP | 121.41.52.85 |
| 管理员端域名 | dda.gaowenbu.cn |
| 业务员端域名 | ddy.gaowenbu.cn |
| 后端端口 | 127.0.0.1:8000 |
| 项目路径 | /www/wwwroot/order-flow |
一、服务器环境准备
1.1 安装宝塔面板
# CentOS/RHEL
yum install -y wget && wget -O install.sh https://download.bt.cn/install/install_6.0.sh && sh install.sh
# Ubuntu/Debian
wget -O install.sh https://download.bt.cn/install/install-ubuntu_6.0.sh && sudo bash install.sh
安装完成后,记录宝塔面板的外网地址、用户名和密码。
1.2 宝塔面板安装软件
登录宝塔面板后,在「软件商店」中安装以下软件:
| 软件 | 版本建议 | 用途 |
|---|---|---|
| Nginx | 1.24+ | 反向代理 + 前端静态文件 |
| MySQL | 5.7 或 8.0 | 数据库(如果用远程数据库可跳过) |
| Redis | 7.0+ | 缓存 |
| Python 项目管理器 | 最新版 | 管理 Python 虚拟环境和后端进程 |
1.3 DNS 解析配置
在域名管理后台(阿里云/腾讯云/Cloudflare等)添加两条 A 记录:
dda.gaowenbu.cn → 121.41.52.85
ddy.gaowenbu.cn → 121.41.52.85
二、上传代码到服务器
2.1 创建项目目录
通过宝塔面板「文件」功能或 SSH,创建目录:
mkdir -p /www/wwwroot/order-flow
2.2 上传文件
方式一:Git 拉取(推荐)
cd /www/wwwroot/
git clone <你的仓库地址> order-flow
方式二:宝塔面板上传
- 在本地将项目打包为 zip(排除
node_modules、__pycache__、.git) - 在宝塔「文件」页面上传到
/www/wwwroot/order-flow/ - 在线解压
方式三:scp 上传
# 本地执行
scp -r ./dingdanquanliucheng root@121.41.52.85:/www/wwwroot/order-flow
2.3 上传后的目录结构
/www/wwwroot/order-flow/
├── backend/
│ ├── app/
│ ├── migrations/
│ ├── scripts/
│ ├── .env ← 生产环境配置
│ ├── requirements.txt
│ └── main.py
├── frontend/
│ ├── web-admin/
│ │ ├── src/
│ │ ├── .env.production
│ │ └── package.json
│ └── web-sales/
│ ├── src/
│ ├── .env.production
│ └── package.json
└── deploy/
├── dda.gaowenbu.cn.conf
└── ddy.gaowenbu.cn.conf
三、配置后端环境
3.1 修改后端配置文件
编辑 /www/wwwroot/order-flow/backend/.env:
如果使用本机 MySQL 数据库,修改以下配置:
MYSQL_HOST=127.0.0.1
MYSQL_PORT=3306
MYSQL_DATABASE=order_flow
MYSQL_USER=order_flow
MYSQL_PASSWORD=你的数据库密码
如果继续使用远程数据库(fn.taisan.online),保持现有配置不变。
3.2 创建数据库(如果使用本机数据库)
-
宝塔面板 → 数据库 → MySQL → 添加数据库
-
填写:
- 数据库名:
order_flow - 用户名:
order_flow - 密码:
(设置一个强密码) - 访问权限:
本地服务器
- 数据库名:
-
确保
backend/.env中的数据库配置与上一步一致
四、部署后端(Python 项目管理器)
4.1 安装 Python 项目管理器
宝塔面板 → 软件商店 → 搜索「Python项目管理器」→ 安装
4.2 创建 Python 项目
- 打开「Python项目管理器」
- 点击「添加项目」,填写:
| 配置项 | 值 |
|---|---|
| 项目名称 | order-flow |
| 项目路径 | /www/wwwroot/order-flow/backend |
| Python版本 | 3.10 或 3.11 |
| 启动文件 | main.py |
| 启动方式 | uvicorn |
| 端口 | 8000 |
| 绑定地址 | 127.0.0.1 |
| 开机自启 | ✅ 勾选 |
- 点击「确定」创建项目
4.3 安装 Python 依赖
项目创建后,点击项目右侧「终端」按钮,执行:
pip install -r requirements.txt
或者在 Python 项目管理器中:
- 点击项目 → 「模块」→ 「安装依赖」→ 选择
requirements.txt
4.4 安装 Redis
- 宝塔面板 → 软件商店 → 搜索「Redis」→ 安装
- 默认配置即可(127.0.0.1:6379)
4.5 启动后端服务
在 Python 项目管理器中点击「启动」按钮。
验证后端是否正常:
curl http://127.0.0.1:8000/api/health
如果返回 JSON 响应,说明后端启动成功。
五、部署前端
5.1 方式一:在本地构建后上传(推荐)
在本地执行:
# 构建管理员端
cd frontend/web-admin
npm install
npm run build
# 产物在 frontend/web-admin/dist/
# 构建业务员端
cd ../web-sales
npm install
npm run build
# 产物在 frontend/web-sales/dist/
然后将 dist 目录上传到服务器:
web-admin/dist/→/www/wwwroot/order-flow/frontend/web-admin/dist/web-sales/dist/→/www/wwwroot/order-flow/frontend/web-sales/dist/
5.2 方式二:在服务器上构建
# 安装 Node.js(如果未安装)
# 宝塔面板 → 软件商店 → 搜索「Node.js版本管理器」→ 安装
cd /www/wwwroot/order-flow/frontend/web-admin
npm install
npm run build
cd /www/wwwroot/order-flow/frontend/web-sales
npm install
npm run build
六、配置 Nginx 站点
6.1 创建管理员端站点(dda.gaowenbu.cn)
- 宝塔面板 → 网站 → 添加站点
- 填写:
- 域名:
dda.gaowenbu.cn - 根目录:
/www/wwwroot/order-flow/frontend/web-admin/dist - PHP版本:
纯静态
- 域名:
- 点击「提交」
6.2 配置管理员端 Nginx
点击站点 dda.gaowenbu.cn 右侧「设置」→「配置文件」,将内容替换为:
server
{
listen 80;
listen 443 ssl;
listen 443 quic;
http2 on;
http3 on;
server_name dda.gaowenbu.cn;
index index.html;
root /www/wwwroot/order-flow/frontend/web-admin/dist;
# SSL 证书路径(宝塔自动申请后会生成)
ssl_certificate /www/server/panel/vhost/cert/dda.gaowenbu.cn/fullchain.pem;
ssl_certificate_key /www/server/panel/vhost/cert/dda.gaowenbu.cn/privkey.pem;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_tickets on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
add_header Strict-Transport-Security "max-age=31536000";
# HTTP 跳转 HTTPS
if ($server_port != 443) {
rewrite ^(/.*)$ https://$host$1 permanent;
}
# 禁止访问敏感文件
location ~* \.(env|git|svn|htaccess|htpasswd)$ {
return 404;
}
# 后端 API 反向代理
location /api/ {
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;
client_max_body_size 20m;
}
# WebSocket 反向代理
location /ws/ {
proxy_pass http://127.0.0.1:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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;
proxy_read_timeout 86400s;
proxy_send_timeout 86400s;
}
# 健康检查
location = /health {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
}
# 上传文件代理
location /uploads/ {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
}
# Vue SPA 路由
location / {
try_files $uri $uri/ /index.html;
}
# 静态资源缓存
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$ {
expires 30d;
error_log /dev/null;
access_log /dev/null;
}
location ~ .*\.(js|css)?$ {
expires 12h;
error_log /dev/null;
access_log /dev/null;
}
access_log /www/wwwlogs/dda.gaowenbu.cn.log;
error_log /www/wwwlogs/dda.gaowenbu.cn.error.log;
}
6.3 创建业务员端站点(ddy.gaowenbu.cn)
- 宝塔面板 → 网站 → 添加站点
- 填写:
- 域名:
ddy.gaowenbu.cn - 根目录:
/www/wwwroot/order-flow/frontend/web-sales/dist - PHP版本:
纯静态
- 域名:
- 点击「提交」
6.4 配置业务员端 Nginx
点击站点 ddy.gaowenbu.cn 右侧「设置」→「配置文件」,将内容替换为:
server
{
listen 80;
listen 443 ssl;
listen 443 quic;
http2 on;
http3 on;
server_name ddy.gaowenbu.cn;
index index.html;
root /www/wwwroot/order-flow/frontend/web-sales/dist;
# SSL 证书路径
ssl_certificate /www/server/panel/vhost/cert/ddy.gaowenbu.cn/fullchain.pem;
ssl_certificate_key /www/server/panel/vhost/cert/ddy.gaowenbu.cn/privkey.pem;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_tickets on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
add_header Strict-Transport-Security "max-age=31536000";
# HTTP 跳转 HTTPS
if ($server_port != 443) {
rewrite ^(/.*)$ https://$host$1 permanent;
}
# 禁止访问敏感文件
location ~* \.(env|git|svn|htaccess|htpasswd)$ {
return 404;
}
# 后端 API 反向代理
location /api/ {
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;
client_max_body_size 20m;
}
# WebSocket 反向代理
location /ws/ {
proxy_pass http://127.0.0.1:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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;
proxy_read_timeout 86400s;
proxy_send_timeout 86400s;
}
# 健康检查
location = /health {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
}
# 上传文件代理
location /uploads/ {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
}
# Vue SPA 路由
location / {
try_files $uri $uri/ /index.html;
}
# 静态资源缓存
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$ {
expires 30d;
error_log /dev/null;
access_log /dev/null;
}
location ~ .*\.(js|css)?$ {
expires 12h;
error_log /dev/null;
access_log /dev/null;
}
access_log /www/wwwlogs/ddy.gaowenbu.cn.log;
error_log /www/wwwlogs/ddy.gaowenbu.cn.error.log;
}
七、配置 SSL 证书(HTTPS)
7.1 方式一:宝塔自动申请 Let's Encrypt 证书(推荐)
- 宝塔面板 → 网站 → 点击站点
dda.gaowenbu.cn→ 设置 → SSL - 选择「Let's Encrypt」→ 勾选域名 → 点击「申请」
- 同样操作
ddy.gaowenbu.cn - 申请成功后,开启「强制HTTPS」开关
7.2 方式二:使用已有证书
如果已有 SSL 证书文件(项目中的 dda.gaowenbu.cn_nginx/ 和 ddy.gaowenbu.cn_nginx/ 目录):
-
将证书文件上传到服务器:
dda.gaowenbu.cn的证书 →/www/server/panel/vhost/cert/dda.gaowenbu.cn/ddy.gaowenbu.cn的证书 →/www/server/panel/vhost/cert/ddy.gaowenbu.cn/
-
证书文件重命名:
*_bundle.pem或*_bundle.crt→fullchain.pem*.key→privkey.pem
八、防火墙配置
宝塔面板 → 安全 → 防火墙,确保放行以下端口:
| 端口 | 协议 | 用途 |
|---|---|---|
| 80 | TCP | HTTP |
| 443 | TCP | HTTPS |
| 8888 | TCP | 宝塔面板(建议改端口) |
后端 8000 端口不需要对外开放,只通过 Nginx 反向代理访问。
九、部署验证
9.1 服务验证命令
# 1. 检查后端是否运行
curl http://127.0.0.1:8000/api/health
# 2. 检查 Nginx 是否正常
curl -I https://dda.gaowenbu.cn
curl -I https://ddy.gaowenbu.cn
# 3. 检查 API 代理是否正常
curl https://dda.gaowenbu.cn/api/health
# 4. 检查 WebSocket 连接
curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" https://dda.gaowenbu.cn/ws/test
9.2 浏览器访问验证
https://dda.gaowenbu.cn能打开管理员登录页https://ddy.gaowenbu.cn能打开业务员登录页- 登录功能正常(默认管理员账号:
admin01) - API 请求正常(F12 Network 检查,无 CORS 错误)
- WebSocket 连接正常
9.3 微信小程序验证
- 登录微信公众平台:https://mp.weixin.qq.com
- 开发管理 → 服务器域名 → 添加:
- request合法域名:
https://dda.gaowenbu.cn - uploadFile合法域名:
https://dda.gaowenbu.cn - downloadFile合法域名:
https://dda.gaowenbu.cn
- request合法域名:
- 小程序重新提交审核
十、常见问题排查
Q1: 页面打开是 404 或空白
# 检查前端 dist 目录是否存在
ls -la /www/wwwroot/order-flow/frontend/web-admin/dist/
ls -la /www/wwwroot/order-flow/frontend/web-sales/dist/
# 检查是否有 index.html
cat /www/wwwroot/order-flow/frontend/web-admin/dist/index.html
Q2: API 请求 502 Bad Gateway
# 检查后端进程是否运行
ps aux | grep uvicorn
# 查看后端日志
tail -f /www/wwwroot/order-flow/backend/logs/*.log
# 在 Python 项目管理器中重启项目
Q3: API 请求 CORS 错误
检查 backend/.env 中 CORS_ALLOW_ORIGINS 是否包含当前域名:
grep CORS /www/wwwroot/order-flow/backend/.env
应包含:https://dda.gaowenbu.cn,https://ddy.gaowenbu.cn
Q4: 数据库连接失败
# 测试数据库连接
mysql -h 127.0.0.1 -u order_flow -p order_flow
# 检查 MySQL 服务状态
systemctl status mysql
# 或
bt mysql status
Q5: Redis 连接失败
# 测试 Redis 连接
redis-cli ping
# 检查 Redis 服务状态
systemctl status redis
Q6: WebSocket 连接失败
检查 Nginx 配置中 WebSocket 代理是否正确,确保:
proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "upgrade";
Q7: 上传文件失败
# 创建本地上传目录
mkdir -p /tmp/order-flow-oss/local-uploads
chmod 755 /tmp/order-flow-oss/local-uploads
十一、日常维护
更新代码
cd /www/wwwroot/order-flow
git pull
# 重新构建前端
cd frontend/web-admin && npm run build
cd ../web-sales && npm run build
# 重启后端
# 在宝塔 Python 项目管理器中点击「重启」
查看日志
# 后端日志
tail -f /www/wwwlogs/dda.gaowenbu.cn.log
tail -f /www/wwwlogs/dda.gaowenbu.cn.error.log
# Nginx 日志
tail -f /www/server/nginx/logs/error.log
数据库备份
- 宝塔面板 → 数据库 → 选择
order_flow→ 备份 - 设置定时备份(建议每天备份一次)
十二、部署检查清单
完成部署后,逐项检查:
- DNS 解析已生效(
ping dda.gaowenbu.cn返回121.41.52.85) - MySQL 服务运行正常
- Redis 服务运行正常
- 后端 Python 项目已启动
curl http://127.0.0.1:8000/api/health返回正常- Nginx 配置已加载(宝塔面板重启 Nginx)
- SSL 证书已申请并配置
https://dda.gaowenbu.cn可访问https://ddy.gaowenbu.cn可访问- 登录功能正常
- 微信小程序域名已配置
部署完成! 如有问题,请查看「常见问题排查」部分或检查日志文件。