feat(deploy): 添加一键部署和更新脚本
添加交互式安装脚本 install.js 和一键更新脚本 update.js 更新部署文档和 README 以支持新功能 优化数据库初始化流程和系统设置默认值
This commit is contained in:
+311
-88
@@ -1,6 +1,39 @@
|
|||||||
# IDC设备管理系统 - 安装部署指南
|
# IDC设备管理系统 - 安装部署指南
|
||||||
|
|
||||||
## 快速开始
|
## 快速开始(推荐)
|
||||||
|
|
||||||
|
### 一键部署脚本(交互式)
|
||||||
|
|
||||||
|
我们提供了交互式安装脚本,自动完成环境检测、依赖安装、数据库初始化和服务启动。
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 方式一:使用 npm
|
||||||
|
npm run deploy
|
||||||
|
|
||||||
|
# 方式二:直接运行
|
||||||
|
node install.js
|
||||||
|
```
|
||||||
|
|
||||||
|
**脚本功能:**
|
||||||
|
- ✅ 自动检测 Node.js、npm、PM2、Nginx
|
||||||
|
- ✅ Linux 系统支持自动安装 Node.js(交互式)
|
||||||
|
- ✅ 交互式配置数据库(SQLite/MySQL)
|
||||||
|
- ✅ 交互式选择前端部署方式(Nginx/PM2 serve)
|
||||||
|
- ✅ 自动安装项目依赖
|
||||||
|
- ✅ 自动初始化数据库
|
||||||
|
- ✅ 自动构建前端项目
|
||||||
|
- ✅ 使用 PM2 启动和管理服务
|
||||||
|
|
||||||
|
**Linux 自动安装 Node.js:**
|
||||||
|
- 支持 Ubuntu、Debian、CentOS、RHEL、Fedora、Arch
|
||||||
|
- 自动检测发行版并使用对应安装方式
|
||||||
|
- 无需手动下载,一键完成
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 传统手动部署
|
||||||
|
|
||||||
|
如需手动部署,请参考以下步骤:
|
||||||
|
|
||||||
### 1. 克隆项目
|
### 1. 克隆项目
|
||||||
|
|
||||||
@@ -43,11 +76,83 @@ cd frontend && npm run dev
|
|||||||
|
|
||||||
| 项目 | 要求 |
|
| 项目 | 要求 |
|
||||||
|------|------|
|
|------|------|
|
||||||
| Node.js | ≥14.0.0 |
|
| Node.js | ≥14.0.0(推荐 20.x LTS) |
|
||||||
| npm | ≥6.0.0 |
|
| npm | ≥6.0.0(随 Node.js 安装) |
|
||||||
| 操作系统 | Windows 10/11、macOS、Linux |
|
| 操作系统 | Windows 10/11、macOS、Linux |
|
||||||
| 内存 | 开发:4GB+ / 生产:8GB+ |
|
| 内存 | 开发:4GB+ / 生产:8GB+ |
|
||||||
|
|
||||||
|
### Node.js 安装
|
||||||
|
|
||||||
|
#### Linux(自动安装)
|
||||||
|
运行 `node install.js`,选择自动安装即可。
|
||||||
|
|
||||||
|
#### Linux(手动安装)
|
||||||
|
|
||||||
|
**Ubuntu/Debian:**
|
||||||
|
```bash
|
||||||
|
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
|
||||||
|
sudo apt-get install -y nodejs
|
||||||
|
```
|
||||||
|
|
||||||
|
**CentOS/RHEL/Fedora:**
|
||||||
|
```bash
|
||||||
|
curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash -
|
||||||
|
sudo yum install -y nodejs
|
||||||
|
```
|
||||||
|
|
||||||
|
**Arch Linux:**
|
||||||
|
```bash
|
||||||
|
sudo pacman -S nodejs npm
|
||||||
|
```
|
||||||
|
|
||||||
|
**使用 nvm(推荐开发者):**
|
||||||
|
```bash
|
||||||
|
# 安装 nvm
|
||||||
|
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
|
||||||
|
|
||||||
|
# 安装 Node.js 20
|
||||||
|
nvm install 20
|
||||||
|
nvm use 20
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Windows
|
||||||
|
|
||||||
|
**方式一:官方安装包(推荐)**
|
||||||
|
1. 访问: https://nodejs.org/
|
||||||
|
2. 下载 LTS 版本(推荐 v20.x)
|
||||||
|
3. 运行安装包,按向导完成安装
|
||||||
|
|
||||||
|
**方式二:nvm-windows(推荐开发者)**
|
||||||
|
1. 下载安装 nvm-windows: https://github.com/coreybutler/nvm-windows/releases
|
||||||
|
2. 执行: `nvm install 20`
|
||||||
|
3. 执行: `nvm use 20`
|
||||||
|
|
||||||
|
**方式三:Winget(Windows 10/11)**
|
||||||
|
```powershell
|
||||||
|
winget install OpenJS.NodeJS.LTS
|
||||||
|
```
|
||||||
|
|
||||||
|
#### macOS
|
||||||
|
|
||||||
|
**方式一:Homebrew(推荐)**
|
||||||
|
```bash
|
||||||
|
brew install node@
|
||||||
|
```
|
||||||
|
|
||||||
|
**方式二:官方安装包**
|
||||||
|
1. 访问: https://nodejs.org/
|
||||||
|
2. 下载 macOS 安装包并安装
|
||||||
|
|
||||||
|
**方式三:nvm(推荐开发者)**
|
||||||
|
```bash
|
||||||
|
# 安装 nvm
|
||||||
|
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
|
||||||
|
|
||||||
|
# 安装 Node.js 20
|
||||||
|
nvm install 20
|
||||||
|
nvm use 20
|
||||||
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 开发环境部署
|
## 开发环境部署
|
||||||
@@ -111,9 +216,22 @@ proxy: {
|
|||||||
|
|
||||||
## 生产环境部署
|
## 生产环境部署
|
||||||
|
|
||||||
### 后端部署
|
### 方式一:一键部署脚本(推荐)
|
||||||
|
|
||||||
#### 1. 配置环境变量
|
```bash
|
||||||
|
npm run deploy
|
||||||
|
```
|
||||||
|
|
||||||
|
按提示选择:
|
||||||
|
- 数据库类型:SQLite 或 MySQL
|
||||||
|
- 前端部署方式:Nginx(推荐)或 PM2 serve
|
||||||
|
- 是否自动安装 Nginx(Windows)
|
||||||
|
|
||||||
|
### 方式二:手动部署
|
||||||
|
|
||||||
|
#### 后端部署
|
||||||
|
|
||||||
|
##### 1. 配置环境变量
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cd backend
|
cd backend
|
||||||
@@ -132,13 +250,13 @@ MYSQL_PASSWORD=secure_password
|
|||||||
MYSQL_DATABASE=idc_management
|
MYSQL_DATABASE=idc_management
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 2. 安装生产依赖
|
##### 2. 安装生产依赖
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm install
|
npm install
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 3. 配置数据库
|
##### 3. 配置数据库
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
CREATE DATABASE idc_management CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
CREATE DATABASE idc_management CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||||
@@ -147,7 +265,13 @@ GRANT ALL PRIVILEGES ON idc_management.* TO 'idc_user'@'localhost';
|
|||||||
FLUSH PRIVILEGES;
|
FLUSH PRIVILEGES;
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 4. 使用PM2管理进程
|
##### 4. 初始化数据库
|
||||||
|
|
||||||
|
```bash
|
||||||
|
node scripts/init-database.js
|
||||||
|
```
|
||||||
|
|
||||||
|
##### 5. 使用PM2管理进程
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm install -g pm2
|
npm install -g pm2
|
||||||
@@ -156,9 +280,9 @@ pm2 startup
|
|||||||
pm2 save
|
pm2 save
|
||||||
```
|
```
|
||||||
|
|
||||||
### 前端部署
|
#### 前端部署
|
||||||
|
|
||||||
#### 1. 构建生产版本
|
##### 1. 构建生产版本
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cd frontend
|
cd frontend
|
||||||
@@ -166,14 +290,21 @@ npm install
|
|||||||
npm run build
|
npm run build
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 2. 部署静态文件
|
##### 2. 部署静态文件
|
||||||
|
|
||||||
|
**Nginx 方式:**
|
||||||
```bash
|
```bash
|
||||||
sudo cp -r dist/* /var/www/idc-frontend/
|
sudo cp -r dist/* /var/www/idc-frontend/
|
||||||
sudo chown -R www-data:www-data /var/www/idc-frontend
|
sudo chown -R www-data:www-data /var/www/idc-frontend
|
||||||
sudo chmod -R 755 /var/www/idc-frontend
|
sudo chmod -R 755 /var/www/idc-frontend
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**PM2 serve 方式:**
|
||||||
|
```bash
|
||||||
|
npm install -g serve
|
||||||
|
pm2 start serve --name "idc-frontend" -- -s dist -l 3000
|
||||||
|
```
|
||||||
|
|
||||||
### Nginx配置
|
### Nginx配置
|
||||||
|
|
||||||
#### 1. 创建配置文件
|
#### 1. 创建配置文件
|
||||||
@@ -192,6 +323,18 @@ server {
|
|||||||
root /var/www/idc-frontend;
|
root /var/www/idc-frontend;
|
||||||
index index.html;
|
index index.html;
|
||||||
|
|
||||||
|
# Gzip压缩
|
||||||
|
gzip on;
|
||||||
|
gzip_vary on;
|
||||||
|
gzip_min_length 1024;
|
||||||
|
gzip_types text/plain text/css text/xml text/javascript application/javascript application/xml+rss application/json;
|
||||||
|
|
||||||
|
# 静态资源缓存
|
||||||
|
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
|
||||||
|
expires 1y;
|
||||||
|
add_header Cache-Control "public, immutable";
|
||||||
|
}
|
||||||
|
|
||||||
location / {
|
location / {
|
||||||
try_files $uri $uri/ /index.html;
|
try_files $uri $uri/ /index.html;
|
||||||
}
|
}
|
||||||
@@ -202,6 +345,17 @@ server {
|
|||||||
proxy_set_header Host $host;
|
proxy_set_header Host $host;
|
||||||
proxy_set_header X-Real-IP $remote_addr;
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
proxy_connect_timeout 60s;
|
||||||
|
proxy_send_timeout 60s;
|
||||||
|
proxy_read_timeout 60s;
|
||||||
|
}
|
||||||
|
|
||||||
|
location /uploads {
|
||||||
|
proxy_pass http://127.0.0.1:8000;
|
||||||
|
proxy_http_version 1.1;
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
|
client_max_body_size 50M;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -223,6 +377,39 @@ sudo certbot --nginx -d your-domain.com
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## 服务管理
|
||||||
|
|
||||||
|
### PM2 管理命令
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pm2 status # 查看服务状态
|
||||||
|
pm2 logs idc-backend # 查看后端日志
|
||||||
|
pm2 logs idc-frontend # 查看前端日志(PM2方式)
|
||||||
|
pm2 restart idc-backend # 重启后端服务
|
||||||
|
pm2 stop idc-backend # 停止后端服务
|
||||||
|
pm2 delete idc-backend # 删除后端服务
|
||||||
|
pm2 save # 保存当前进程列表
|
||||||
|
pm2 startup # 配置开机自启
|
||||||
|
```
|
||||||
|
|
||||||
|
### Nginx 管理命令
|
||||||
|
|
||||||
|
**Linux:**
|
||||||
|
```bash
|
||||||
|
sudo nginx -t # 测试配置
|
||||||
|
sudo systemctl restart nginx # 重启服务
|
||||||
|
sudo systemctl status nginx # 查看状态
|
||||||
|
```
|
||||||
|
|
||||||
|
**Windows:**
|
||||||
|
```cmd
|
||||||
|
nginx -t # 测试配置
|
||||||
|
nginx -s reload # 重载配置
|
||||||
|
nginx -s stop # 停止Nginx
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## 数据库配置
|
## 数据库配置
|
||||||
|
|
||||||
### SQLite(开发环境)
|
### SQLite(开发环境)
|
||||||
@@ -285,6 +472,86 @@ crontab -e
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## 更新升级
|
||||||
|
|
||||||
|
### 方式一:一键更新脚本(推荐)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run update
|
||||||
|
```
|
||||||
|
|
||||||
|
功能:自动备份 → 拉取代码 → 更新依赖 → 重建前端 → 重启服务
|
||||||
|
|
||||||
|
### 方式二:手动更新
|
||||||
|
|
||||||
|
#### 1. 备份数据
|
||||||
|
|
||||||
|
```bash
|
||||||
|
mysqldump -u idc_user -p idc_management > backup.sql
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 2. 拉取最新代码
|
||||||
|
|
||||||
|
**从GitHub拉取**:
|
||||||
|
```bash
|
||||||
|
git pull origin master
|
||||||
|
```
|
||||||
|
|
||||||
|
**从Gitee拉取**:
|
||||||
|
```bash
|
||||||
|
git pull origin master
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 3. 更新依赖
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd backend && npm install
|
||||||
|
cd ../frontend && npm install && npm run build
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 4. 重启服务
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pm2 restart idc-backend
|
||||||
|
sudo systemctl restart nginx
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 监控维护
|
||||||
|
|
||||||
|
### 查看日志
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pm2 logs idc-backend
|
||||||
|
tail -f /var/log/nginx/idc_assest-error.log
|
||||||
|
```
|
||||||
|
|
||||||
|
### 健康检查
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl http://localhost:8000/health
|
||||||
|
```
|
||||||
|
|
||||||
|
### 性能优化
|
||||||
|
|
||||||
|
**PM2配置**(`deploy/ecosystem.config.js`):
|
||||||
|
```javascript
|
||||||
|
module.exports = {
|
||||||
|
apps: [{
|
||||||
|
name: 'idc-backend',
|
||||||
|
script: './server.js',
|
||||||
|
cwd: './backend',
|
||||||
|
instances: 1,
|
||||||
|
exec_mode: 'fork',
|
||||||
|
max_memory_restart: '1G',
|
||||||
|
autorestart: true
|
||||||
|
}]
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## 常见问题
|
## 常见问题
|
||||||
|
|
||||||
### 端口冲突
|
### 端口冲突
|
||||||
@@ -316,90 +583,46 @@ npm install
|
|||||||
- 验证连接参数
|
- 验证连接参数
|
||||||
- 确认数据库已创建
|
- 确认数据库已创建
|
||||||
|
|
||||||
### PM2命令
|
### 部署脚本问题
|
||||||
|
|
||||||
```bash
|
**Linux 自动安装 Node.js 失败:**
|
||||||
pm2 status # 查看状态
|
- 检查 sudo 权限
|
||||||
pm2 logs idc-backend # 查看日志
|
- 检查网络连接
|
||||||
pm2 restart idc-backend # 重启服务
|
- 尝试手动安装后重新运行脚本
|
||||||
pm2 stop idc-backend # 停止服务
|
|
||||||
pm2 delete idc-backend # 删除服务
|
|
||||||
```
|
|
||||||
|
|
||||||
### Nginx命令
|
**Nginx 未安装(选择 Nginx 部署时):**
|
||||||
|
- Windows:脚本会询问是否自动下载安装
|
||||||
```bash
|
- Linux/macOS:脚本会显示安装命令,需手动安装
|
||||||
sudo nginx -t # 测试配置
|
|
||||||
sudo systemctl restart nginx # 重启服务
|
|
||||||
sudo systemctl status nginx # 查看状态
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 更新升级
|
## 项目结构
|
||||||
|
|
||||||
### 1. 备份数据
|
|
||||||
|
|
||||||
```bash
|
|
||||||
mysqldump -u idc_user -p idc_management > backup.sql
|
|
||||||
```
|
```
|
||||||
|
idc_assest/
|
||||||
### 2. 拉取最新代码
|
├── backend/ # 后端服务
|
||||||
|
│ ├── middleware/ # 中间件
|
||||||
**从GitHub拉取**:
|
│ ├── models/ # 数据模型
|
||||||
```bash
|
│ ├── routes/ # API路由
|
||||||
git pull origin master
|
│ ├── scripts/ # 数据库脚本
|
||||||
```
|
│ ├── uploads/ # 文件上传目录
|
||||||
|
│ ├── server.js # 服务入口
|
||||||
**从Gitee拉取**:
|
│ └── package.json
|
||||||
```bash
|
├── frontend/ # 前端应用
|
||||||
git pull origin master
|
│ ├── src/
|
||||||
```
|
│ │ ├── api/ # API接口
|
||||||
|
│ │ ├── components/ # 组件
|
||||||
### 3. 更新依赖
|
│ │ ├── pages/ # 页面
|
||||||
|
│ │ └── utils/ # 工具函数
|
||||||
```bash
|
│ ├── dist/ # 构建输出
|
||||||
cd backend && npm install
|
│ └── package.json
|
||||||
cd ../frontend && npm install && npm run build
|
├── deploy/ # 部署配置
|
||||||
```
|
│ ├── ecosystem.config.js # PM2配置
|
||||||
|
│ └── nginx-idc.conf # Nginx配置
|
||||||
### 4. 重启服务
|
├── install.js # 交互式安装脚本 ⭐
|
||||||
|
├── update.js # 一键更新脚本 ⭐
|
||||||
```bash
|
├── package.json
|
||||||
pm2 restart idc-backend
|
└── DEPLOYMENT.md # 本文件
|
||||||
sudo systemctl restart nginx
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 监控维护
|
|
||||||
|
|
||||||
### 查看日志
|
|
||||||
|
|
||||||
```bash
|
|
||||||
pm2 logs idc-backend
|
|
||||||
tail -f /var/log/nginx/idc_assest-error.log
|
|
||||||
```
|
|
||||||
|
|
||||||
### 健康检查
|
|
||||||
|
|
||||||
```bash
|
|
||||||
curl http://localhost:8000/health
|
|
||||||
```
|
|
||||||
|
|
||||||
### 性能优化
|
|
||||||
|
|
||||||
**PM2配置**(`ecosystem.config.js`):
|
|
||||||
```javascript
|
|
||||||
module.exports = {
|
|
||||||
apps: [{
|
|
||||||
name: 'idc-backend',
|
|
||||||
script: 'server.js',
|
|
||||||
instances: 'max',
|
|
||||||
exec_mode: 'cluster',
|
|
||||||
max_memory_restart: '1G'
|
|
||||||
}]
|
|
||||||
};
|
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|||||||
@@ -62,8 +62,13 @@ jigui/
|
|||||||
│ ├── routes/ # API路由
|
│ ├── routes/ # API路由
|
||||||
│ ├── middleware/ # 中间件
|
│ ├── middleware/ # 中间件
|
||||||
│ └── server.js # 服务入口
|
│ └── server.js # 服务入口
|
||||||
|
├── deploy/ # 部署配置 ⭐ NEW
|
||||||
|
│ ├── ecosystem.config.js # PM2配置
|
||||||
|
│ └── nginx-idc.conf # Nginx配置
|
||||||
├── docs/ # 项目文档
|
├── docs/ # 项目文档
|
||||||
│ └── api/ # 接口文档
|
│ └── api/ # 接口文档
|
||||||
|
├── install.js # 交互式安装脚本 ⭐ NEW
|
||||||
|
├── update.js # 一键更新脚本 ⭐ NEW
|
||||||
├── README.md # 项目说明
|
├── README.md # 项目说明
|
||||||
├── CHANGELOG.md # 版本记录
|
├── CHANGELOG.md # 版本记录
|
||||||
└── DEPLOYMENT.md # 部署指南
|
└── DEPLOYMENT.md # 部署指南
|
||||||
@@ -71,28 +76,99 @@ jigui/
|
|||||||
|
|
||||||
## 快速开始
|
## 快速开始
|
||||||
|
|
||||||
### 环境要求
|
### 方式一:一键部署脚本(推荐)⭐ NEW
|
||||||
|
|
||||||
- Node.js ≥14.0.0
|
我们提供了交互式安装脚本,自动完成所有部署步骤:
|
||||||
- npm ≥6.0.0 或 pnpm ≥8.0.0
|
|
||||||
- 操作系统:Windows 10/11、macOS、Linux
|
|
||||||
|
|
||||||
### 安装步骤
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# 1. 安装后端依赖
|
# 克隆项目
|
||||||
|
git clone https://github.com/gituib/idc_assest.git
|
||||||
|
cd idc_assest
|
||||||
|
|
||||||
|
# 运行交互式安装脚本
|
||||||
|
npm run deploy
|
||||||
|
# 或
|
||||||
|
node install.js
|
||||||
|
```
|
||||||
|
|
||||||
|
**脚本功能:**
|
||||||
|
- ✅ 自动检测 Node.js、npm、PM2、Nginx
|
||||||
|
- ✅ **Linux 支持自动安装 Node.js**(交互式)
|
||||||
|
- ✅ 交互式配置数据库(SQLite/MySQL)
|
||||||
|
- ✅ 交互式选择运行环境(development/production)
|
||||||
|
- ✅ 交互式选择前端部署方式(Nginx/PM2 serve)
|
||||||
|
- ✅ 自动安装项目依赖
|
||||||
|
- ✅ 自动初始化数据库
|
||||||
|
- ✅ 自动构建前端项目
|
||||||
|
- ✅ 使用 PM2 启动和管理服务
|
||||||
|
|
||||||
|
**交互配置示例:**
|
||||||
|
```
|
||||||
|
▶ 环境检测
|
||||||
|
✓ Node.js v20.11.0
|
||||||
|
✓ npm 10.2.4
|
||||||
|
✓ PM2 已安装
|
||||||
|
|
||||||
|
▶ 数据库配置
|
||||||
|
选择数据库类型:
|
||||||
|
1. SQLite(零配置,适合开发/小规模)
|
||||||
|
2. MySQL(生产环境推荐)
|
||||||
|
请选择 (1): 1
|
||||||
|
|
||||||
|
▶ 服务配置
|
||||||
|
后端服务端口 (8000):
|
||||||
|
选择运行环境:
|
||||||
|
1. production(生产模式,性能优化,推荐正式部署)
|
||||||
|
2. development(开发模式,详细日志,便于调试)
|
||||||
|
请选择 (1): 1
|
||||||
|
前端部署方式:
|
||||||
|
1. Nginx(性能最优,推荐生产环境)
|
||||||
|
2. PM2 serve(简单快捷,无需额外安装)
|
||||||
|
请选择 (1): 1
|
||||||
|
|
||||||
|
▶ 配置确认
|
||||||
|
部署配置摘要:
|
||||||
|
数据库类型: sqlite
|
||||||
|
后端端口: 8000
|
||||||
|
运行环境: production
|
||||||
|
前端部署: nginx
|
||||||
|
前端端口: 80
|
||||||
|
确认以上配置并开始部署? (Y/n): Y
|
||||||
|
|
||||||
|
✓ 后端环境变量文件已生成 (.env)
|
||||||
|
✓ PM2 配置文件已生成 (deploy/ecosystem.config.js)
|
||||||
|
✓ Nginx 配置文件已生成 (deploy/nginx-idc.conf)
|
||||||
|
✓ 安装部署完成!
|
||||||
|
```
|
||||||
|
|
||||||
|
### 方式二:手动安装
|
||||||
|
|
||||||
|
#### 环境要求
|
||||||
|
|
||||||
|
- Node.js ≥14.0.0(推荐 20.x LTS)
|
||||||
|
- npm ≥6.0.0
|
||||||
|
- 操作系统:Windows 10/11、macOS、Linux
|
||||||
|
|
||||||
|
#### 安装步骤
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 1. 克隆项目
|
||||||
|
git clone https://github.com/gituib/idc_assest.git
|
||||||
|
cd idc_assest
|
||||||
|
|
||||||
|
# 2. 安装后端依赖
|
||||||
cd backend
|
cd backend
|
||||||
npm install
|
npm install
|
||||||
|
|
||||||
# 2. 安装前端依赖
|
# 3. 安装前端依赖
|
||||||
cd ../frontend
|
cd ../frontend
|
||||||
npm install
|
npm install
|
||||||
|
|
||||||
# 3. 配置环境变量
|
# 4. 配置环境变量
|
||||||
cd ../backend
|
cd ../backend
|
||||||
cp .env.example .env
|
cp .env.example .env
|
||||||
|
|
||||||
# 4. 启动服务
|
# 5. 启动服务
|
||||||
# 启动后端(端口8000)
|
# 启动后端(端口8000)
|
||||||
npm run dev
|
npm run dev
|
||||||
|
|
||||||
@@ -105,6 +181,32 @@ npm run dev
|
|||||||
- 前端应用:http://localhost:3000
|
- 前端应用:http://localhost:3000
|
||||||
- 后端API:http://localhost:8000/api
|
- 后端API:http://localhost:8000/api
|
||||||
|
|
||||||
|
## 更新升级
|
||||||
|
|
||||||
|
### 一键更新(推荐)⭐ NEW
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run update
|
||||||
|
```
|
||||||
|
|
||||||
|
功能:自动备份 → 拉取代码 → 更新依赖 → 重建前端 → 重启服务
|
||||||
|
|
||||||
|
### 手动更新
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 拉取最新代码
|
||||||
|
git pull
|
||||||
|
|
||||||
|
# 更新后端
|
||||||
|
cd backend && npm install
|
||||||
|
|
||||||
|
# 更新前端并构建
|
||||||
|
cd ../frontend && npm install && npm run build
|
||||||
|
|
||||||
|
# 重启服务
|
||||||
|
pm2 restart idc-backend
|
||||||
|
```
|
||||||
|
|
||||||
## 主要功能
|
## 主要功能
|
||||||
|
|
||||||
### 机房管理
|
### 机房管理
|
||||||
@@ -179,6 +281,22 @@ npm run dev
|
|||||||
- 配置SSL证书
|
- 配置SSL证书
|
||||||
- 建议使用PM2管理进程
|
- 建议使用PM2管理进程
|
||||||
|
|
||||||
|
### 服务管理命令
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 查看服务状态
|
||||||
|
pm2 status
|
||||||
|
|
||||||
|
# 查看日志
|
||||||
|
pm2 logs idc-backend
|
||||||
|
|
||||||
|
# 重启服务
|
||||||
|
pm2 restart idc-backend
|
||||||
|
|
||||||
|
# 停止服务
|
||||||
|
pm2 stop idc-backend
|
||||||
|
```
|
||||||
|
|
||||||
## 版本历史
|
## 版本历史
|
||||||
|
|
||||||
完整版本记录请参考 [CHANGELOG.md](CHANGELOG.md)。
|
完整版本记录请参考 [CHANGELOG.md](CHANGELOG.md)。
|
||||||
|
|||||||
@@ -185,14 +185,7 @@ const defaultDeviceFields = [
|
|||||||
async function initDeviceFields() {
|
async function initDeviceFields() {
|
||||||
try {
|
try {
|
||||||
console.log('开始初始化设备字段配置...');
|
console.log('开始初始化设备字段配置...');
|
||||||
|
// 注意:数据库连接和表结构同步已在 server.js 中完成,这里直接初始化数据
|
||||||
// 连接数据库
|
|
||||||
await sequelize.authenticate();
|
|
||||||
console.log('数据库连接成功');
|
|
||||||
|
|
||||||
// 同步表结构
|
|
||||||
await sequelize.sync();
|
|
||||||
console.log('数据库表结构同步完成');
|
|
||||||
|
|
||||||
// 批量创建默认字段
|
// 批量创建默认字段
|
||||||
for (const field of defaultDeviceFields) {
|
for (const field of defaultDeviceFields) {
|
||||||
|
|||||||
@@ -37,20 +37,33 @@ const initDefaultSettings = async () => {
|
|||||||
{ settingKey: 'terms_of_service', settingValue: JSON.stringify(''), settingType: 'string', category: 'about', description: '服务条款URL', isEditable: true },
|
{ settingKey: 'terms_of_service', settingValue: JSON.stringify(''), settingType: 'string', category: 'about', description: '服务条款URL', isEditable: true },
|
||||||
];
|
];
|
||||||
|
|
||||||
|
let createdCount = 0;
|
||||||
|
let updatedCount = 0;
|
||||||
|
let errorCount = 0;
|
||||||
|
|
||||||
for (const setting of defaultSettings) {
|
for (const setting of defaultSettings) {
|
||||||
try {
|
try {
|
||||||
const existing = await SystemSetting.findByPk(setting.settingKey);
|
const existing = await SystemSetting.findByPk(setting.settingKey);
|
||||||
if (!existing) {
|
if (!existing) {
|
||||||
await SystemSetting.create(setting);
|
await SystemSetting.create(setting);
|
||||||
|
createdCount++;
|
||||||
|
} else if (existing.description !== setting.description) {
|
||||||
|
// 更新描述(用于修复英文描述问题)
|
||||||
|
await existing.update({ description: setting.description });
|
||||||
|
updatedCount++;
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`初始化设置 ${setting.settingKey} 失败:`, error);
|
console.error(`初始化设置 ${setting.settingKey} 失败:`, error.message);
|
||||||
|
errorCount++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(`系统设置初始化结果: 创建 ${createdCount} 个, 更新 ${updatedCount} 个, 失败 ${errorCount} 个`);
|
||||||
|
return { createdCount, updatedCount, errorCount };
|
||||||
};
|
};
|
||||||
|
|
||||||
// 初始化默认设置
|
// 注意:初始化现在由 server.js 统一调用,避免重复初始化
|
||||||
initDefaultSettings();
|
// initDefaultSettings();
|
||||||
|
|
||||||
// 获取所有设置
|
// 获取所有设置
|
||||||
router.get('/', async (req, res) => {
|
router.get('/', async (req, res) => {
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
/**
|
/**
|
||||||
* 数据库初始化脚本
|
* 数据库初始化脚本
|
||||||
* 用于生产环境首次部署时创建表结构
|
* 用于生产环境首次部署时创建表结构
|
||||||
|
*
|
||||||
|
* 注意:此脚本为独立运行脚本,与 server.js 的初始化逻辑重复
|
||||||
|
* 如果 server.js 已能正常完成初始化,则无需单独运行此脚本
|
||||||
|
* 如需单独运行:node backend/scripts/init-database.js
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const { sequelize } = require('../db');
|
const { sequelize } = require('../db');
|
||||||
|
|||||||
@@ -44,6 +44,15 @@ sequelize.authenticate()
|
|||||||
const SystemSetting = require('./models/SystemSetting');
|
const SystemSetting = require('./models/SystemSetting');
|
||||||
return SystemSetting.sync();
|
return SystemSetting.sync();
|
||||||
})
|
})
|
||||||
|
.then(() => {
|
||||||
|
// 初始化系统设置默认值(关键:确保部署时数据正确初始化)
|
||||||
|
console.log('开始初始化系统设置默认值...');
|
||||||
|
const { initDefaultSettings } = require('./routes/systemSettings');
|
||||||
|
return initDefaultSettings();
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
console.log('系统设置初始化完成');
|
||||||
|
})
|
||||||
.catch(err => console.error('数据库操作失败:', err));
|
.catch(err => console.error('数据库操作失败:', err));
|
||||||
|
|
||||||
// 导入路由
|
// 导入路由
|
||||||
|
|||||||
+1385
File diff suppressed because it is too large
Load Diff
+3
-1
@@ -6,7 +6,9 @@
|
|||||||
"start": "concurrently \"npm run start:backend\" \"npm run start:frontend\"",
|
"start": "concurrently \"npm run start:backend\" \"npm run start:frontend\"",
|
||||||
"start:backend": "cd backend && npm run dev",
|
"start:backend": "cd backend && npm run dev",
|
||||||
"start:frontend": "cd frontend && npm run dev",
|
"start:frontend": "cd frontend && npm run dev",
|
||||||
"install:all": "npm install && cd backend && npm install && cd ../frontend && npm install"
|
"install:all": "npm install && cd backend && npm install && cd ../frontend && npm install",
|
||||||
|
"deploy": "node install.js",
|
||||||
|
"update": "node update.js"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"idc",
|
"idc",
|
||||||
|
|||||||
@@ -0,0 +1,143 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
/**
|
||||||
|
* IDC设备管理系统 - 一键更新脚本
|
||||||
|
* 功能:拉取最新代码 → 安装依赖 → 重建前端 → 重启服务
|
||||||
|
*/
|
||||||
|
|
||||||
|
const { execSync } = require('child_process');
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
// 颜色输出
|
||||||
|
const colors = {
|
||||||
|
reset: '\x1b[0m',
|
||||||
|
bright: '\x1b[1m',
|
||||||
|
green: '\x1b[32m',
|
||||||
|
yellow: '\x1b[33m',
|
||||||
|
red: '\x1b[31m',
|
||||||
|
cyan: '\x1b[36m',
|
||||||
|
gray: '\x1b[90m'
|
||||||
|
};
|
||||||
|
|
||||||
|
const log = {
|
||||||
|
info: (msg) => console.log(`${colors.cyan}ℹ${colors.reset} ${msg}`),
|
||||||
|
success: (msg) => console.log(`${colors.green}✓${colors.reset} ${msg}`),
|
||||||
|
warning: (msg) => console.log(`${colors.yellow}⚠${colors.reset} ${msg}`),
|
||||||
|
error: (msg) => console.log(`${colors.red}✗${colors.reset} ${msg}`),
|
||||||
|
step: (msg) => console.log(`\n${colors.bright}${colors.cyan}▶ ${msg}${colors.reset}`),
|
||||||
|
divider: () => console.log(`${colors.gray}${'─'.repeat(60)}${colors.reset}`)
|
||||||
|
};
|
||||||
|
|
||||||
|
function runCommand(command, options = {}) {
|
||||||
|
try {
|
||||||
|
const result = execSync(command, {
|
||||||
|
encoding: 'utf8',
|
||||||
|
stdio: options.silent ? 'pipe' : 'inherit',
|
||||||
|
cwd: options.cwd || process.cwd(),
|
||||||
|
shell: true
|
||||||
|
});
|
||||||
|
return { success: true, output: result };
|
||||||
|
} catch (error) {
|
||||||
|
return { success: false, error: error.message };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
console.log(`
|
||||||
|
${colors.bright}${colors.cyan}
|
||||||
|
╔══════════════════════════════════════════════════════════╗
|
||||||
|
║ IDC设备管理系统 - 一键更新脚本 ║
|
||||||
|
║ One-Click Update Script ║
|
||||||
|
╚══════════════════════════════════════════════════════════╝
|
||||||
|
${colors.reset}`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 1. 备份数据库
|
||||||
|
log.step('1. 备份数据');
|
||||||
|
const envPath = path.join(__dirname, 'backend', '.env');
|
||||||
|
if (fs.existsSync(envPath)) {
|
||||||
|
const envContent = fs.readFileSync(envPath, 'utf8');
|
||||||
|
const dbTypeMatch = envContent.match(/DB_TYPE=(\w+)/);
|
||||||
|
const dbType = dbTypeMatch ? dbTypeMatch[1] : 'sqlite';
|
||||||
|
|
||||||
|
if (dbType === 'sqlite') {
|
||||||
|
const dbPath = path.join(__dirname, 'backend', 'idc_management.db');
|
||||||
|
const backupDir = path.join(__dirname, 'backup');
|
||||||
|
if (!fs.existsSync(backupDir)) {
|
||||||
|
fs.mkdirSync(backupDir, { recursive: true });
|
||||||
|
}
|
||||||
|
const backupPath = path.join(backupDir, `database_${Date.now()}.db`);
|
||||||
|
if (fs.existsSync(dbPath)) {
|
||||||
|
fs.copyFileSync(dbPath, backupPath);
|
||||||
|
log.success(`数据库已备份: ${backupPath}`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.warning('MySQL数据库请手动备份');
|
||||||
|
console.log(` ${colors.gray}mysqldump -u username -p idc_management > backup_${Date.now()}.sql${colors.reset}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 拉取最新代码
|
||||||
|
log.step('2. 拉取最新代码');
|
||||||
|
const gitResult = runCommand('git pull');
|
||||||
|
if (gitResult.success) {
|
||||||
|
log.success('代码更新完成');
|
||||||
|
} else {
|
||||||
|
log.warning('代码拉取失败或不是git仓库,跳过');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 更新后端依赖
|
||||||
|
log.step('3. 更新后端依赖');
|
||||||
|
const backendResult = runCommand('npm install', { cwd: path.join(__dirname, 'backend') });
|
||||||
|
if (backendResult.success) {
|
||||||
|
log.success('后端依赖更新完成');
|
||||||
|
} else {
|
||||||
|
log.error('后端依赖更新失败');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 更新前端依赖并构建
|
||||||
|
log.step('4. 更新前端依赖并构建');
|
||||||
|
const frontendInstall = runCommand('npm install', { cwd: path.join(__dirname, 'frontend') });
|
||||||
|
if (frontendInstall.success) {
|
||||||
|
log.success('前端依赖更新完成');
|
||||||
|
|
||||||
|
const frontendBuild = runCommand('npm run build', { cwd: path.join(__dirname, 'frontend') });
|
||||||
|
if (frontendBuild.success) {
|
||||||
|
log.success('前端构建完成');
|
||||||
|
} else {
|
||||||
|
log.error('前端构建失败');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.error('前端依赖更新失败');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. 重启服务
|
||||||
|
log.step('5. 重启服务');
|
||||||
|
const restartResult = runCommand('pm2 restart idc-backend');
|
||||||
|
if (restartResult.success) {
|
||||||
|
log.success('后端服务已重启');
|
||||||
|
} else {
|
||||||
|
log.error('后端服务重启失败,请手动执行: pm2 restart idc-backend');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否有前端PM2服务
|
||||||
|
const frontendRestart = runCommand('pm2 restart idc-frontend 2>nul || echo "frontend not found"', { silent: true });
|
||||||
|
if (frontendRestart.success && !frontendRestart.output.includes('frontend not found')) {
|
||||||
|
log.success('前端服务已重启');
|
||||||
|
}
|
||||||
|
|
||||||
|
log.divider();
|
||||||
|
log.success('更新完成!');
|
||||||
|
console.log(`\n${colors.cyan}访问地址:${colors.reset}`);
|
||||||
|
console.log(` 后端API: http://localhost:8000/api`);
|
||||||
|
console.log(` 前端页面: http://localhost`);
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
log.error(`更新失败: ${error.message}`);
|
||||||
|
console.error(error);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
main();
|
||||||
Reference in New Issue
Block a user