前后端目录重命名: - reading-platform-java/ → lesingle-edu-reading-platform-backend/ - reading-platform-frontend/ → lesingle-edu-reading-platform-frontend/ 更新相关文件: - 所有 shell 脚本中的目录引用 - pom.xml 和 application.yml 中的项目名称 - package.json 中的项目名称 - .claude/CLAUDE.md 中的路径引用 - README 文档中的路径引用
311 lines
5.6 KiB
Markdown
311 lines
5.6 KiB
Markdown
# 宝塔面板部署指南
|
||
|
||
## 快速开始
|
||
|
||
### 方式一:使用部署脚本(推荐)
|
||
|
||
```bash
|
||
# 1. 配置环境变量
|
||
export REMOTE_HOST=你的服务器 IP
|
||
export REMOTE_USER=root
|
||
|
||
# 2. 部署后端
|
||
./deploy-backend.sh
|
||
|
||
# 3. 部署前端
|
||
./deploy-frontend.sh
|
||
```
|
||
|
||
### 方式二:手动部署
|
||
|
||
---
|
||
|
||
## 第一步:在宝塔安装基础服务
|
||
|
||
1. 登录宝塔面板(`http://你的IP:8888`)
|
||
2. 安装 LNMP 套件:
|
||
- Nginx 1.24+
|
||
- MySQL 8.0+
|
||
- PHP 纯静态(前端不需要 PHP)
|
||
- Redis 7.x
|
||
|
||
3. 安装 JDK 17:
|
||
- 软件商店 → 搜索 "Java"
|
||
- 安装 JDK 17 版本
|
||
|
||
---
|
||
|
||
## 第二步:创建数据库
|
||
|
||
1. 宝塔面板 → 数据库 → 添加
|
||
2. 填写信息:
|
||
- 数据库名:`lesingle-edu-reading-platform`
|
||
- 用户名:`reading_platform`
|
||
- 密码:设置一个强密码(保存好)
|
||
- 权限:全部
|
||
|
||
3. 记录数据库信息,后面要用
|
||
|
||
---
|
||
|
||
## 第三步:部署后端
|
||
|
||
### 3.1 上传 JAR 包
|
||
|
||
```bash
|
||
# 本地构建
|
||
cd /f/LesingleProject/lesingle-kindergarten-course/kindergarten_java/lesingle-edu-reading-platform-backend
|
||
mvn clean package -DskipTests
|
||
|
||
# 上传到服务器
|
||
scp target/reading-platform-1.0.0.jar root@你的 IP:/www/wwwroot/reading-platform/app.jar
|
||
```
|
||
|
||
### 3.2 创建启动脚本
|
||
|
||
在服务器上创建 `/www/wwwroot/reading-platform/start.sh`:
|
||
|
||
```bash
|
||
#!/bin/bash
|
||
export SPRING_PROFILES_ACTIVE=prod
|
||
export SERVER_PORT=8480
|
||
export DB_HOST=localhost
|
||
export DB_PASSWORD=你的数据库密码
|
||
export JWT_SECRET=你的 JWT 密钥
|
||
|
||
nohup java -jar -Xms512m -Xmx1024m /www/wwwroot/reading-platform/app.jar \
|
||
--spring.profiles.active=prod > /www/wwwroot/reading-platform/logs/app.log 2>&1 &
|
||
echo $! > /www/wwwroot/reading-platform/app.pid
|
||
```
|
||
|
||
```bash
|
||
chmod +x /www/wwwroot/reading-platform/start.sh
|
||
```
|
||
|
||
### 3.3 创建环境变量文件
|
||
|
||
`/www/wwwroot/reading-platform/.env`:
|
||
|
||
```bash
|
||
# 数据库
|
||
DB_HOST=localhost
|
||
DB_PASSWORD=你的数据库密码
|
||
|
||
# JWT
|
||
JWT_SECRET=你的-production-jwt-secret-至少-32-字符
|
||
JWT_EXPIRATION=86400000
|
||
|
||
# Redis
|
||
REDIS_HOST=localhost
|
||
REDIS_PORT=6379
|
||
|
||
# OSS
|
||
OSS_ACCESS_KEY_ID=LTAI5tKZhPofbThbSzDSiWoK
|
||
OSS_ACCESS_KEY_SECRET=FtcsC7oQX3T0NaChaa9FYq2aoysQFM
|
||
OSS_BUCKET_NAME=lesingle-kid-course
|
||
```
|
||
|
||
### 3.4 启动后端
|
||
|
||
```bash
|
||
cd /www/wwwroot/reading-platform
|
||
./start.sh
|
||
|
||
# 查看日志
|
||
tail -f logs/app.log
|
||
```
|
||
|
||
---
|
||
|
||
## 第四步:部署前端
|
||
|
||
### 4.1 本地构建
|
||
|
||
```bash
|
||
cd /f/LesingleProject/lesingle-kindergarten-course/kindergarten_java/lesingle-edu-reading-platform-frontend
|
||
npm run build
|
||
```
|
||
|
||
### 4.2 上传到服务器
|
||
|
||
```bash
|
||
# 上传到宝塔站点目录
|
||
scp -r dist/* root@你的 IP:/www/wwwroot/你的域名/
|
||
```
|
||
|
||
---
|
||
|
||
## 第五步:配置 Nginx 反向代理
|
||
|
||
1. 宝塔面板 → 网站 → 添加站点
|
||
2. 填写域名,根目录设为 `/www/wwwroot/你的域名`
|
||
3. 确定后,点击网站 → 设置 → 配置文件
|
||
4. 添加以下配置:
|
||
|
||
```nginx
|
||
# 前端页面
|
||
location / {
|
||
try_files $uri $uri/ /index.html;
|
||
}
|
||
|
||
# 后端 API 反向代理
|
||
location /api {
|
||
proxy_pass http://localhost:8480;
|
||
proxy_set_header Host $host;
|
||
proxy_set_header X-Real-IP $remote_addr;
|
||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||
}
|
||
|
||
# 文件上传代理
|
||
location /uploads {
|
||
proxy_pass http://localhost:8480;
|
||
}
|
||
```
|
||
|
||
5. 保存配置
|
||
|
||
---
|
||
|
||
## 第六步:配置防火墙
|
||
|
||
1. 宝塔面板 → 安全 → 放行端口
|
||
2. 放行以下端口:
|
||
- 80(HTTP)
|
||
- 443(HTTPS,如果用 SSL)
|
||
- 8480(后端 API,可选,如果通过 Nginx 代理就不需要)
|
||
|
||
---
|
||
|
||
## 第七步:验证部署
|
||
|
||
```bash
|
||
# 检查后端健康状态
|
||
curl http://localhost:8480/actuator/health
|
||
|
||
# 访问前端
|
||
curl http://localhost
|
||
|
||
# 测试 API
|
||
curl http://localhost:8480/api/v1
|
||
```
|
||
|
||
---
|
||
|
||
## 第八步:申请 SSL 证书(可选)
|
||
|
||
1. 宝塔面板 → 网站 → 点击对应网站 → SSL
|
||
2. 选择 "Let's Encrypt" 免费证书
|
||
3. 填写邮箱,点击申请
|
||
4. 申请成功后,开启"强制 HTTPS"
|
||
|
||
---
|
||
|
||
## 管理命令
|
||
|
||
### 后端管理
|
||
|
||
```bash
|
||
# 启动
|
||
cd /www/wwwroot/reading-platform && ./start.sh
|
||
|
||
# 停止
|
||
cd /www/wwwroot/reading-platform && ./stop.sh
|
||
|
||
# 重启
|
||
cd /www/wwwroot/reading-platform && ./stop.sh && ./start.sh
|
||
|
||
# 查看日志
|
||
tail -f /www/wwwroot/reading-platform/logs/app.log
|
||
|
||
# 查看进程
|
||
ps aux | grep reading-platform
|
||
```
|
||
|
||
### 前端更新
|
||
|
||
```bash
|
||
# 本地重新构建并上传
|
||
cd lesingle-edu-reading-platform-frontend
|
||
npm run build
|
||
scp -r dist/* root@你的 IP:/www/wwwroot/你的域名/
|
||
```
|
||
|
||
---
|
||
|
||
## 常见问题
|
||
|
||
### 1. 后端启动失败
|
||
|
||
检查日志:
|
||
```bash
|
||
tail -100 /www/wwwroot/reading-platform/logs/app.log
|
||
```
|
||
|
||
常见原因:
|
||
- JDK 版本不对(必须是 17)
|
||
- 数据库连接失败
|
||
- 端口被占用
|
||
|
||
### 2. 前端页面空白
|
||
|
||
- 检查 Nginx 配置是否正确
|
||
- 打开浏览器 Console 查看错误
|
||
- 检查 API 请求是否 404
|
||
|
||
### 3. API 请求失败
|
||
|
||
- 检查 Nginx 反向代理配置
|
||
- 确认后端服务是否运行
|
||
- 检查防火墙端口
|
||
|
||
### 4. 文件上传失败
|
||
|
||
- 检查 OSS 配置
|
||
- 检查 Nginx 上传大小限制
|
||
- 查看后端日志
|
||
|
||
---
|
||
|
||
## 性能优化
|
||
|
||
### JVM 优化参数
|
||
|
||
```bash
|
||
java -jar \
|
||
-Xms1g \
|
||
-Xmx2g \
|
||
-XX:+UseG1GC \
|
||
-XX:MaxGCPauseMillis=200 \
|
||
-XX:+HeapDumpOnOutOfMemoryError \
|
||
app.jar
|
||
```
|
||
|
||
### Nginx 优化
|
||
|
||
```nginx
|
||
# 开启 gzip 压缩
|
||
gzip on;
|
||
gzip_types text/plain text/css application/json application/javascript;
|
||
|
||
# 上传大小限制
|
||
client_max_body_size 100M;
|
||
```
|
||
|
||
---
|
||
|
||
## 备份与恢复
|
||
|
||
### 数据库备份
|
||
|
||
```bash
|
||
# 宝塔面板 → 数据库 → 备份
|
||
# 或命令行
|
||
mysqldump -u reading_platform -p lesingle-edu-reading-platform > backup.sql
|
||
```
|
||
|
||
### 文件备份
|
||
|
||
```bash
|
||
tar -czf backup-$(date +%Y%m%d).tar.gz /www/wwwroot/reading-platform
|
||
```
|