"refactor(backend): 移除未使用的redis、socket.io和sqlite3依赖"包括:
- `backend/.env.production`:生产环境变量配置 - `backend/ecosystem.config.js`:PM2生态系统配置文件 - `scripts/nginx-aijianhua.conf`:Nginx配置文件 - `scripts/server-deploy.sh`:服务器部署脚本 - `scripts/sync-to-server.sh`:代码同步脚本 - `docs/生产环境部署指南.md`:生产环境部署指南文档 同时移除了`package.json`中不再使用的依赖项:- `redis`- `socket.io` - `sqlite3`这些更改将有助于简化生产环境的部署流程,并确保服务能够稳定运行。```
This commit is contained in:
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"java.compile.nullAnalysis.mode": "automatic"
|
||||
}
|
||||
36
backend/.env.production
Normal file
36
backend/.env.production
Normal file
@@ -0,0 +1,36 @@
|
||||
# 爱鉴花生产环境配置
|
||||
# 部署到CentOS服务器时使用
|
||||
|
||||
NODE_ENV=production
|
||||
PORT=3330
|
||||
|
||||
# 数据库配置 - 生产环境MySQL
|
||||
DB_HOST=129.211.213.226
|
||||
DB_PORT=9527
|
||||
DB_USER=root
|
||||
DB_PASSWORD=aiotAiot123!
|
||||
DB_NAME=xlxumudata
|
||||
|
||||
# JWT配置
|
||||
JWT_SECRET=your-production-jwt-secret-key-change-this-in-production
|
||||
JWT_EXPIRE=7d
|
||||
|
||||
# 文件上传配置
|
||||
UPLOAD_MAX_SIZE=50MB
|
||||
UPLOAD_PATH=./uploads
|
||||
|
||||
# CORS配置 - 生产环境域名
|
||||
CORS_ORIGIN=https://your-domain.com,https://wapi.aijianhua.com
|
||||
|
||||
# 日志配置
|
||||
LOG_LEVEL=info
|
||||
LOG_FILE=/data/nodejs/aijianhua/logs/app.log
|
||||
|
||||
# 性能配置
|
||||
NODE_OPTIONS=--max-old-space-size=4096
|
||||
|
||||
# Swagger配置(生产环境启用)
|
||||
SWAGGER_ENABLED=true
|
||||
SWAGGER_TITLE=爱鉴花API文档
|
||||
SWAGGER_DESCRIPTION=爱鉴花小程序后端API接口文档
|
||||
SWAGGER_VERSION=1.0.0
|
||||
35
backend/ecosystem.config.js
Normal file
35
backend/ecosystem.config.js
Normal file
@@ -0,0 +1,35 @@
|
||||
// PM2生态系统配置文件
|
||||
// 用于生产环境部署
|
||||
|
||||
module.exports = {
|
||||
apps: [{
|
||||
name: 'aijianhua-backend',
|
||||
script: './app.js',
|
||||
instances: 'max',
|
||||
exec_mode: 'cluster',
|
||||
env: {
|
||||
NODE_ENV: 'production',
|
||||
PORT: 3330,
|
||||
NODE_OPTIONS: '--max-old-space-size=4096'
|
||||
},
|
||||
env_production: {
|
||||
NODE_ENV: 'production',
|
||||
PORT: 3330
|
||||
},
|
||||
// 日志配置
|
||||
log_file: '/data/nodejs/aijianhua/logs/combined.log',
|
||||
out_file: '/data/nodejs/aijianhua/logs/out.log',
|
||||
error_file: '/data/nodejs/aijianhua/logs/error.log',
|
||||
// 进程管理
|
||||
max_memory_restart: '1G',
|
||||
watch: false,
|
||||
ignore_watch: ['node_modules', 'logs', 'uploads'],
|
||||
// 重启策略
|
||||
autorestart: true,
|
||||
restart_delay: 3000,
|
||||
// 高级配置
|
||||
node_args: '--harmony',
|
||||
merge_logs: true,
|
||||
time: true
|
||||
}]
|
||||
};
|
||||
@@ -22,9 +22,6 @@
|
||||
"morgan": "^1.10.0",
|
||||
"multer": "^1.4.5-lts.1",
|
||||
"mysql2": "^3.6.0",
|
||||
"redis": "^4.6.8",
|
||||
"socket.io": "^4.7.2",
|
||||
"sqlite3": "^5.1.7",
|
||||
"swagger-jsdoc": "^6.2.8",
|
||||
"swagger-ui-express": "^4.6.3",
|
||||
"validator": "^13.11.0",
|
||||
|
||||
180
docs/生产环境部署指南.md
Normal file
180
docs/生产环境部署指南.md
Normal file
@@ -0,0 +1,180 @@
|
||||
# 爱鉴花小程序 - 生产环境部署指南
|
||||
|
||||
## 服务器信息
|
||||
- **服务器地址**: www.jiebanke.com
|
||||
- **服务器用户**: root
|
||||
- **部署目录**: /data/nodejs/aijianhua/
|
||||
- **后端域名**: wapi.aijianhua.com
|
||||
- **数据库**: 生产环境MySQL (129.211.213.226:9527)
|
||||
|
||||
## 部署前准备
|
||||
|
||||
### 1. 服务器环境准备
|
||||
```bash
|
||||
# 登录服务器
|
||||
ssh root@www.jiebanke.com
|
||||
|
||||
# 安装Node.js (如果未安装)
|
||||
curl -fsSL https://rpm.nodesource.com/setup_18.x | bash -
|
||||
yum install -y nodejs
|
||||
|
||||
# 安装PM2
|
||||
npm install -g pm2
|
||||
|
||||
# 创建项目目录
|
||||
mkdir -p /data/nodejs/aijianhua
|
||||
mkdir -p /data/nodejs/aijianhua/logs
|
||||
mkdir -p /data/nodejs/aijianhua/uploads
|
||||
|
||||
# 设置目录权限
|
||||
chmod 755 /data/nodejs/aijianhua
|
||||
```
|
||||
|
||||
### 2. SSL证书准备
|
||||
将SSL证书文件上传到服务器:
|
||||
- 证书文件: `/etc/nginx/ssl/wapi.aijianhua.com.crt`
|
||||
- 私钥文件: `/etc/nginx/ssl/wapi.aijianhua.com.key`
|
||||
|
||||
## 部署步骤
|
||||
|
||||
### 步骤1: 同步代码到服务器
|
||||
在本地开发机器上执行:
|
||||
```bash
|
||||
# 进入项目目录
|
||||
cd e:/vue/aijianhua/
|
||||
|
||||
# 运行同步脚本
|
||||
bash scripts/sync-to-server.sh
|
||||
```
|
||||
|
||||
### 步骤2: 在服务器上部署
|
||||
登录服务器后执行:
|
||||
```bash
|
||||
# 进入项目目录
|
||||
cd /data/nodejs/aijianhua
|
||||
|
||||
# 运行部署脚本
|
||||
bash ../scripts/server-deploy.sh
|
||||
```
|
||||
|
||||
### 步骤3: 配置Nginx
|
||||
将Nginx配置文件复制到正确位置:
|
||||
```bash
|
||||
# 备份原有配置(如果有)
|
||||
cp /etc/nginx/conf.d/aijianhua.conf /etc/nginx/conf.d/aijianhua.conf.bak
|
||||
|
||||
# 使用新的配置文件
|
||||
cp /data/nodejs/aijianhua/scripts/nginx-aijianhua.conf /etc/nginx/conf.d/aijianhua.conf
|
||||
|
||||
# 测试Nginx配置
|
||||
nginx -t
|
||||
|
||||
# 重启Nginx
|
||||
systemctl restart nginx
|
||||
```
|
||||
|
||||
### 步骤4: 验证部署
|
||||
```bash
|
||||
# 检查服务状态
|
||||
pm2 status
|
||||
|
||||
# 检查服务日志
|
||||
tail -f /data/nodejs/aijianhua/logs/out.log
|
||||
|
||||
# 测试健康检查接口
|
||||
curl https://wapi.aijianhua.com/health
|
||||
|
||||
# 测试API接口
|
||||
curl https://wapi.aijianhua.com/api/v1/products
|
||||
```
|
||||
|
||||
## 环境配置
|
||||
|
||||
### 生产环境变量 (.env.production)
|
||||
```bash
|
||||
NODE_ENV=production
|
||||
PORT=3330
|
||||
DB_HOST=129.211.213.226
|
||||
DB_PORT=9527
|
||||
DB_USER=root
|
||||
DB_PASSWORD=aiotAiot123!
|
||||
DB_NAME=xlxumudata
|
||||
JWT_SECRET=your-production-jwt-secret-key
|
||||
JWT_EXPIRE=7d
|
||||
UPLOAD_MAX_SIZE=50MB
|
||||
UPLOAD_PATH=./uploads
|
||||
CORS_ORIGIN=https://wapi.aijianhua.com
|
||||
LOG_LEVEL=info
|
||||
LOG_FILE=/data/nodejs/aijianhua/logs/app.log
|
||||
SWAGGER_ENABLED=true
|
||||
```
|
||||
|
||||
## 日常维护
|
||||
|
||||
### 服务管理
|
||||
```bash
|
||||
# 查看服务状态
|
||||
pm2 status
|
||||
|
||||
# 查看服务日志
|
||||
pm2 logs aijianhua-backend
|
||||
|
||||
# 重启服务
|
||||
pm2 restart aijianhua-backend
|
||||
|
||||
# 停止服务
|
||||
pm2 stop aijianhua-backend
|
||||
|
||||
# 删除服务
|
||||
pm2 delete aijianhua-backend
|
||||
```
|
||||
|
||||
### 日志管理
|
||||
```bash
|
||||
# 查看实时日志
|
||||
tail -f /data/nodejs/aijianhua/logs/out.log
|
||||
|
||||
# 查看错误日志
|
||||
tail -f /data/nodejs/aijianhua/logs/error.log
|
||||
|
||||
# 日志轮转(建议配置logrotate)
|
||||
```
|
||||
|
||||
### 备份策略
|
||||
```bash
|
||||
# 数据库备份
|
||||
mysqldump -h 129.211.213.226 -P 9527 -u root -p xlxumudata > backup_$(date +%Y%m%d).sql
|
||||
|
||||
# 上传文件备份
|
||||
tar -czf uploads_backup_$(date +%Y%m%d).tar.gz /data/nodejs/aijianhua/uploads/
|
||||
```
|
||||
|
||||
## 故障排除
|
||||
|
||||
### 常见问题
|
||||
1. **端口占用**: 检查3330端口是否被占用 `netstat -tlnp | grep 3330`
|
||||
2. **数据库连接失败**: 检查MySQL服务状态和网络连接
|
||||
3. **文件权限问题**: 检查上传目录权限 `chmod 755 /data/nodejs/aijianhua/uploads`
|
||||
4. **SSL证书问题**: 检查证书路径和权限
|
||||
|
||||
### 监控建议
|
||||
- 使用PM2监控应用状态
|
||||
- 配置日志监控
|
||||
- 设置健康检查告警
|
||||
- 监控服务器资源使用情况
|
||||
|
||||
## 安全建议
|
||||
|
||||
1. **定期更新依赖**: `npm audit fix`
|
||||
2. **修改默认JWT密钥**: 修改.env.production中的JWT_SECRET
|
||||
3. **配置防火墙**: 只开放必要的端口
|
||||
4. **定期备份**: 数据库和上传文件
|
||||
5. **监控日志**: 关注错误日志和安全日志
|
||||
|
||||
## 更新流程
|
||||
|
||||
1. 本地开发测试完成
|
||||
2. 运行同步脚本上传代码
|
||||
3. 在服务器上运行部署脚本
|
||||
4. 验证服务正常运行
|
||||
5. 更新文档记录变更
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
119
scripts/nginx-aijianhua.conf
Normal file
119
scripts/nginx-aijianhua.conf
Normal file
@@ -0,0 +1,119 @@
|
||||
# 爱鉴花小程序后端API服务Nginx配置
|
||||
# 域名: wapi.aijianhua.com
|
||||
# SSL证书配置
|
||||
|
||||
upstream aijianhua_backend {
|
||||
server 127.0.0.1:3330;
|
||||
keepalive 64;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 80;
|
||||
server_name wapi.aijianhua.com;
|
||||
|
||||
# HTTP重定向到HTTPS
|
||||
return 301 https://$server_name$request_uri;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 443 ssl http2;
|
||||
server_name wapi.aijianhua.com;
|
||||
|
||||
# SSL证书配置 - 需要替换为实际证书路径
|
||||
ssl_certificate /etc/nginx/ssl/wapi.aijianhua.com.crt;
|
||||
ssl_certificate_key /etc/nginx/ssl/wapi.aijianhua.com.key;
|
||||
|
||||
# SSL优化配置
|
||||
ssl_protocols TLSv1.2 TLSv1.3;
|
||||
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384;
|
||||
ssl_prefer_server_ciphers off;
|
||||
ssl_session_cache shared:SSL:10m;
|
||||
ssl_session_timeout 10m;
|
||||
ssl_session_tickets off;
|
||||
|
||||
# 安全头
|
||||
add_header X-Frame-Options DENY;
|
||||
add_header X-Content-Type-Options nosniff;
|
||||
add_header X-XSS-Protection "1; mode=block";
|
||||
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
|
||||
|
||||
# 静态文件服务配置
|
||||
location /uploads/ {
|
||||
alias /data/nodejs/aijianhua/uploads/;
|
||||
expires 30d;
|
||||
add_header Cache-Control "public, immutable";
|
||||
|
||||
# 安全限制
|
||||
client_max_body_size 50M;
|
||||
|
||||
# 防盗链
|
||||
valid_referers none blocked server_names ~(\.aijianhua\.com$);
|
||||
if ($invalid_referer) {
|
||||
return 403;
|
||||
}
|
||||
}
|
||||
|
||||
# API代理配置
|
||||
location /api/ {
|
||||
proxy_pass http://aijianhua_backend;
|
||||
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_connect_timeout 60s;
|
||||
proxy_send_timeout 60s;
|
||||
proxy_read_timeout 60s;
|
||||
|
||||
# 缓冲区配置
|
||||
proxy_buffering on;
|
||||
proxy_buffer_size 16k;
|
||||
proxy_buffers 4 32k;
|
||||
|
||||
# 禁用缓存
|
||||
proxy_cache off;
|
||||
}
|
||||
|
||||
# Swagger文档
|
||||
location /api-docs/ {
|
||||
proxy_pass http://aijianhua_backend/api-docs/;
|
||||
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;
|
||||
}
|
||||
|
||||
# 健康检查
|
||||
location /health {
|
||||
proxy_pass http://aijianhua_backend/health;
|
||||
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;
|
||||
access_log off;
|
||||
}
|
||||
|
||||
# 根路径重定向到API文档
|
||||
location = / {
|
||||
return 302 /api-docs;
|
||||
}
|
||||
|
||||
# 日志配置
|
||||
access_log /var/log/nginx/aijianhua_access.log main;
|
||||
error_log /var/log/nginx/aijianhua_error.log warn;
|
||||
}
|
||||
|
||||
# 安全配置
|
||||
# 禁止访问隐藏文件
|
||||
location ~ /\.(?!well-known) {
|
||||
deny all;
|
||||
}
|
||||
|
||||
# 禁止访问敏感文件
|
||||
location ~* \.(env|git|sql|bak|old|orig|save|swp)$ {
|
||||
deny all;
|
||||
}
|
||||
85
scripts/server-deploy.sh
Normal file
85
scripts/server-deploy.sh
Normal file
@@ -0,0 +1,85 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 爱鉴花后端服务器部署脚本
|
||||
# 在CentOS服务器上执行
|
||||
|
||||
SERVER_DIR="/data/nodejs/aijianhua"
|
||||
LOG_DIR="$SERVER_DIR/logs"
|
||||
UPLOAD_DIR="$SERVER_DIR/uploads"
|
||||
|
||||
# 颜色输出
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
echo -e "${GREEN}🚀 开始部署爱鉴花后端服务...${NC}"
|
||||
|
||||
# 检查Node.js是否安装
|
||||
if ! command -v node &> /dev/null; then
|
||||
echo -e "${RED}❌ Node.js 未安装,请先安装 Node.js${NC}"
|
||||
echo "安装命令: curl -fsSL https://rpm.nodesource.com/setup_18.x | bash - && yum install -y nodejs"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 检查PM2是否安装
|
||||
if ! command -v pm2 &> /dev/null; then
|
||||
echo -e "${YELLOW}⚠️ PM2 未安装,正在安装...${NC}"
|
||||
npm install -g pm2
|
||||
if [ $? -ne 0 ]; then
|
||||
echo -e "${RED}❌ PM2 安装失败${NC}"
|
||||
exit 1
|
||||
fi
|
||||
echo -e "${GREEN}✅ PM2 安装成功${NC}"
|
||||
fi
|
||||
|
||||
# 创建必要的目录
|
||||
mkdir -p "$LOG_DIR" "$UPLOAD_DIR"
|
||||
echo -e "${GREEN}✅ 创建日志和上传目录${NC}"
|
||||
|
||||
# 进入项目目录
|
||||
cd "$SERVER_DIR"
|
||||
|
||||
# 安装生产依赖
|
||||
echo -e "${YELLOW}📦 安装生产依赖...${NC}"
|
||||
npm install --production
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
echo -e "${RED}❌ 依赖安装失败${NC}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo -e "${GREEN}✅ 依赖安装完成${NC}"
|
||||
|
||||
# 停止现有服务(如果存在)
|
||||
echo -e "${YELLOW}🛑 停止现有服务...${NC}"
|
||||
pm2 delete aijianhua-backend 2>/dev/null || true
|
||||
|
||||
# 启动服务
|
||||
echo -e "${YELLOW}🚀 启动服务...${NC}"
|
||||
pm2 start ecosystem.config.js
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
echo -e "${GREEN}✅ 服务启动成功${NC}"
|
||||
|
||||
# 设置开机自启
|
||||
pm2 startup
|
||||
pm2 save
|
||||
|
||||
echo -e "${GREEN}📊 服务状态:${NC}"
|
||||
pm2 status
|
||||
|
||||
echo -e "${GREEN}🌐 服务信息:${NC}"
|
||||
echo " 服务名称: aijianhua-backend"
|
||||
echo " 运行端口: 3330"
|
||||
echo " 环境: production"
|
||||
echo " API文档: https://wapi.aijianhua.com/api-docs"
|
||||
echo " 健康检查: https://wapi.aijianhua.com/health"
|
||||
|
||||
else
|
||||
echo -e "${RED}❌ 服务启动失败${NC}"
|
||||
echo "查看日志: tail -f $LOG_DIR/error.log"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo -e "${GREEN}🎉 部署完成!${NC}"
|
||||
62
scripts/sync-to-server.sh
Normal file
62
scripts/sync-to-server.sh
Normal file
@@ -0,0 +1,62 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 爱鉴花后端服务同步脚本
|
||||
# 将本地代码同步到CentOS服务器
|
||||
|
||||
SERVER="www.jiebanke.com"
|
||||
SERVER_USER="root"
|
||||
SERVER_DIR="/data/nodejs/aijianhua"
|
||||
LOCAL_DIR="e:/vue/aijianhua/backend"
|
||||
|
||||
# 需要排除的文件和目录
|
||||
exclude_list=(
|
||||
"--exclude=.env"
|
||||
"--exclude=.env.development"
|
||||
"--exclude=.env.example"
|
||||
"--exclude=node_modules/"
|
||||
"--exclude=uploads/"
|
||||
"--exclude=database.sqlite"
|
||||
"--exclude=*.log"
|
||||
"--exclude=.git/"
|
||||
"--exclude=.idea/"
|
||||
"--exclude=*.bat"
|
||||
"--exclude=*.sh"
|
||||
)
|
||||
|
||||
echo "🚀 开始同步爱鉴花后端代码到服务器..."
|
||||
echo "📁 服务器: $SERVER"
|
||||
echo "📂 目标目录: $SERVER_DIR"
|
||||
echo "📦 本地目录: $LOCAL_DIR"
|
||||
echo ""
|
||||
|
||||
# 检查rsync是否安装
|
||||
if ! command -v rsync &> /dev/null; then
|
||||
echo "❌ rsync 未安装,请先安装 rsync"
|
||||
echo "Ubuntu/Debian: sudo apt-get install rsync"
|
||||
echo "CentOS/RHEL: sudo yum install rsync"
|
||||
echo "macOS: brew install rsync"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 执行同步
|
||||
rsync -avz --delete \
|
||||
"${exclude_list[@]}" \
|
||||
-e "ssh -p 22" \
|
||||
"$LOCAL_DIR/" \
|
||||
"$SERVER_USER@$SERVER:$SERVER_DIR/"
|
||||
|
||||
# 检查同步结果
|
||||
if [ $? -eq 0 ]; then
|
||||
echo ""
|
||||
echo "✅ 同步完成!"
|
||||
echo "📋 下一步操作:"
|
||||
echo " 1. 登录服务器: ssh $SERVER_USER@$SERVER"
|
||||
echo " 2. 进入目录: cd $SERVER_DIR"
|
||||
echo " 3. 安装依赖: npm install --production"
|
||||
echo " 4. 启动服务: pm2 start ecosystem.config.js"
|
||||
echo " 5. 保存配置: pm2 save"
|
||||
else
|
||||
echo ""
|
||||
echo "❌ 同步失败,请检查网络连接和服务器配置"
|
||||
exit 1
|
||||
fi
|
||||
Reference in New Issue
Block a user