218 lines
4.4 KiB
Markdown
218 lines
4.4 KiB
Markdown
|
|
# 锡林郭勒盟智慧养殖平台 - 生产环境部署指南
|
|||
|
|
|
|||
|
|
## 服务器信息
|
|||
|
|
- **服务器地址**: www.jiebanke.com
|
|||
|
|
- **服务器系统**: CentOS
|
|||
|
|
- **部署目录**: /data/nodejs/xlxumu/
|
|||
|
|
- **API域名**: xlapi.jiebanke.com
|
|||
|
|
- **服务端口**: 3350
|
|||
|
|
|
|||
|
|
## 环境要求
|
|||
|
|
- Node.js 16+
|
|||
|
|
- npm 8+
|
|||
|
|
- PM2
|
|||
|
|
- Nginx
|
|||
|
|
- MySQL 8.0+
|
|||
|
|
|
|||
|
|
## 部署步骤
|
|||
|
|
|
|||
|
|
### 1. 服务器环境准备
|
|||
|
|
```bash
|
|||
|
|
# 登录服务器
|
|||
|
|
ssh root@www.jiebanke.com
|
|||
|
|
|
|||
|
|
# 创建部署目录
|
|||
|
|
mkdir -p /data/nodejs/xlxumu/
|
|||
|
|
|
|||
|
|
# 安装Node.js(如果未安装)
|
|||
|
|
curl -fsSL https://rpm.nodesource.com/setup_16.x | bash -
|
|||
|
|
yum install -y nodejs
|
|||
|
|
|
|||
|
|
# 安装PM2
|
|||
|
|
npm install -g pm2
|
|||
|
|
|
|||
|
|
# 安装Nginx
|
|||
|
|
yum install -y nginx
|
|||
|
|
|
|||
|
|
# 安装MySQL客户端(可选)
|
|||
|
|
yum install -y mysql
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 2. 上传代码到服务器
|
|||
|
|
```bash
|
|||
|
|
# 在本地开发机器执行同步脚本
|
|||
|
|
cd e:/vue/xlxumu
|
|||
|
|
./scripts/sync-to-server.sh
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 3. 配置生产环境
|
|||
|
|
```bash
|
|||
|
|
# 在服务器上编辑环境变量
|
|||
|
|
vi /data/nodejs/xlxumu/backend/api/.env
|
|||
|
|
|
|||
|
|
# 内容示例:
|
|||
|
|
NODE_ENV=production
|
|||
|
|
PORT=3350
|
|||
|
|
DB_HOST=生产环境MySQL地址
|
|||
|
|
DB_PORT=3306
|
|||
|
|
DB_USER=生产环境MySQL用户
|
|||
|
|
DB_PASSWORD=生产环境MySQL密码
|
|||
|
|
DB_NAME=xlxumu_production
|
|||
|
|
JWT_SECRET=your-super-secret-jwt-key-here
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 4. 配置Nginx
|
|||
|
|
```bash
|
|||
|
|
# 上传Nginx配置到服务器
|
|||
|
|
scp ./scripts/xlxumu-api.conf root@www.jiebanke.com:/etc/nginx/conf.d/
|
|||
|
|
|
|||
|
|
# 检查Nginx配置
|
|||
|
|
nginx -t
|
|||
|
|
|
|||
|
|
# 重启Nginx
|
|||
|
|
systemctl restart nginx
|
|||
|
|
|
|||
|
|
# 设置Nginx开机自启
|
|||
|
|
systemctl enable nginx
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 5. 启动应用服务
|
|||
|
|
```bash
|
|||
|
|
# 在服务器上执行启动脚本
|
|||
|
|
cd /data/nodejs/xlxumu/
|
|||
|
|
./scripts/start-server.sh
|
|||
|
|
|
|||
|
|
# 或者手动启动
|
|||
|
|
cd /data/nodejs/xlxumu/backend/api
|
|||
|
|
npm install --production
|
|||
|
|
pm2 start server.js --name xlxumu-api --env production
|
|||
|
|
pm2 startup
|
|||
|
|
pm2 save
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 6. 配置SSL证书
|
|||
|
|
```bash
|
|||
|
|
# 将SSL证书文件上传到服务器
|
|||
|
|
# 证书文件应放置在:
|
|||
|
|
# - /etc/ssl/certs/xlapi.jiebanke.com.crt
|
|||
|
|
# - /etc/ssl/private/xlapi.jiebanke.com.key
|
|||
|
|
|
|||
|
|
# 设置证书文件权限
|
|||
|
|
chmod 644 /etc/ssl/certs/xlapi.jiebanke.com.crt
|
|||
|
|
chmod 600 /etc/ssl/private/xlapi.jiebanke.com.key
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 服务管理命令
|
|||
|
|
|
|||
|
|
### PM2管理
|
|||
|
|
```bash
|
|||
|
|
# 查看服务状态
|
|||
|
|
pm2 status
|
|||
|
|
|
|||
|
|
# 查看日志
|
|||
|
|
pm2 logs xlxumu-api
|
|||
|
|
|
|||
|
|
# 重启服务
|
|||
|
|
pm2 restart xlxumu-api
|
|||
|
|
|
|||
|
|
# 停止服务
|
|||
|
|
pm2 stop xlxumu-api
|
|||
|
|
|
|||
|
|
# 删除服务
|
|||
|
|
pm2 delete xlxumu-api
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Nginx管理
|
|||
|
|
```bash
|
|||
|
|
# 重启Nginx
|
|||
|
|
systemctl restart nginx
|
|||
|
|
|
|||
|
|
# 查看Nginx状态
|
|||
|
|
systemctl status nginx
|
|||
|
|
|
|||
|
|
# 查看Nginx错误日志
|
|||
|
|
tail -f /var/log/nginx/error.log
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 文件目录结构
|
|||
|
|
```
|
|||
|
|
/data/nodejs/xlxumu/
|
|||
|
|
├── backend/
|
|||
|
|
│ ├── api/ # API服务核心代码
|
|||
|
|
│ │ ├── server.js # 主服务文件
|
|||
|
|
│ │ ├── package.json # 依赖配置
|
|||
|
|
│ │ ├── .env # 环境变量
|
|||
|
|
│ │ └── modules/ # 各业务模块
|
|||
|
|
│ ├── database/ # 数据库设计文档
|
|||
|
|
│ ├── services/ # 服务层代码
|
|||
|
|
│ └── utils/ # 工具类
|
|||
|
|
└── scripts/ # 部署脚本
|
|||
|
|
├── sync-to-server.sh # 同步脚本
|
|||
|
|
├── start-server.sh # 启动脚本
|
|||
|
|
└── xlxumu-api.conf # Nginx配置
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 监控和维护
|
|||
|
|
|
|||
|
|
### 服务健康检查
|
|||
|
|
```bash
|
|||
|
|
# API健康检查
|
|||
|
|
curl https://xlapi.jiebanke.com/health
|
|||
|
|
|
|||
|
|
# 服务状态检查
|
|||
|
|
pm2 monit
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 日志查看
|
|||
|
|
```bash
|
|||
|
|
# 查看应用日志
|
|||
|
|
pm2 logs xlxumu-api
|
|||
|
|
|
|||
|
|
# 查看Nginx访问日志
|
|||
|
|
tail -f /var/log/nginx/xlxumu-api.access.log
|
|||
|
|
|
|||
|
|
# 查看Nginx错误日志
|
|||
|
|
tail -f /var/log/nginx/xlxumu-api.error.log
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 故障排除
|
|||
|
|
|
|||
|
|
### 常见问题
|
|||
|
|
1. **端口占用**: 检查3350端口是否被占用 `netstat -tlnp | grep 3350`
|
|||
|
|
2. **权限问题**: 确保部署目录有正确权限 `chown -R nginx:nginx /data/nodejs/xlxumu`
|
|||
|
|
3. **证书问题**: 检查SSL证书路径和权限
|
|||
|
|
4. **数据库连接**: 验证MySQL连接信息是否正确
|
|||
|
|
|
|||
|
|
### 紧急恢复
|
|||
|
|
```bash
|
|||
|
|
# 如果服务崩溃,手动重启
|
|||
|
|
pm2 restart xlxumu-api
|
|||
|
|
|
|||
|
|
# 如果PM2有问题,直接启动Node.js
|
|||
|
|
cd /data/nodejs/xlxumu/backend/api && node server.js
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 备份策略
|
|||
|
|
|
|||
|
|
### 代码备份
|
|||
|
|
```bash
|
|||
|
|
# 定期备份代码
|
|||
|
|
tar -czf /backup/xlxumu-api-$(date +%Y%m%d).tar.gz /data/nodejs/xlxumu/
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 数据库备份
|
|||
|
|
```bash
|
|||
|
|
# 定期备份MySQL数据库
|
|||
|
|
mysqldump -u用户名 -p密码 xlxumu_production > /backup/xlxumu-db-$(date +%Y%m%d).sql
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 安全建议
|
|||
|
|
|
|||
|
|
1. 定期更新系统和软件包
|
|||
|
|
2. 使用强密码和密钥
|
|||
|
|
3. 配置防火墙规则
|
|||
|
|
4. 定期检查日志
|
|||
|
|
5. 监控系统资源使用情况
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
*最后更新: $(date +%Y-%m-%d)*
|