MyWorkTime/PRODUCTION_DEPLOYMENT.md
2025-07-28 01:04:18 +08:00

282 lines
4.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 🚀 WorkTime 生产环境部署指南
## 📋 系统要求
- **Node.js**: 版本 14.0.0 或更高
- **npm**: 版本 6.0.0 或更高
- **内存**: 至少 512MB 可用内存
- **存储**: 至少 100MB 可用空间
## 🛠️ 快速部署
### 方法1: 使用部署脚本(推荐)
```bash
# 克隆项目
git clone <your-repository-url>
cd WorkTime
# 运行部署脚本
./deploy.sh
```
### 方法2: 手动部署
```bash
# 1. 安装依赖
npm install
# 2. 构建前端应用
npm run build
# 3. 启动服务器
node server.js
```
## 🌐 生产环境配置
### 环境变量
```bash
# 设置端口可选默认为3001
export PORT=3001
# 设置数据文件路径(可选)
export DATA_FILE=/path/to/user-data.json
```
### 使用 PM2 管理进程(推荐)
```bash
# 安装 PM2
npm install -g pm2
# 启动应用
pm2 start server.js --name "worktime"
# 查看状态
pm2 status
# 查看日志
pm2 logs worktime
# 重启应用
pm2 restart worktime
# 停止应用
pm2 stop worktime
```
### 使用 Docker 部署
```dockerfile
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build
EXPOSE 3001
CMD ["node", "server.js"]
```
```bash
# 构建镜像
docker build -t worktime-app .
# 运行容器
docker run -d -p 3001:3001 --name worktime worktime-app
```
## 🔧 服务器配置
### Nginx 反向代理配置
```nginx
server {
listen 80;
server_name your-domain.com;
location / {
proxy_pass http://localhost:3001;
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_cache_bypass $http_upgrade;
}
}
```
### Apache 反向代理配置
```apache
<VirtualHost *:80>
ServerName your-domain.com
ProxyPreserveHost On
ProxyPass / http://localhost:3001/
ProxyPassReverse / http://localhost:3001/
ErrorLog ${APACHE_LOG_DIR}/worktime_error.log
CustomLog ${APACHE_LOG_DIR}/worktime_access.log combined
</VirtualHost>
```
## 📊 监控和维护
### 健康检查
```bash
# 检查应用状态
curl http://localhost:3001/api/health
# 预期响应
{"status":"ok","timestamp":"2025-07-27T16:59:36.507Z"}
```
### 日志管理
```bash
# 查看应用日志
tail -f /var/log/worktime.log
# 查看错误日志
tail -f /var/log/worktime-error.log
```
### 数据备份
```bash
# 备份用户数据
cp user-data.json backup/user-data-$(date +%Y%m%d-%H%M%S).json
# 自动备份脚本
#!/bin/bash
BACKUP_DIR="/backup/worktime"
mkdir -p $BACKUP_DIR
cp user-data.json $BACKUP_DIR/user-data-$(date +%Y%m%d-%H%M%S).json
find $BACKUP_DIR -name "*.json" -mtime +7 -delete
```
## 🔒 安全配置
### 防火墙设置
```bash
# 只允许必要端口
sudo ufw allow 22 # SSH
sudo ufw allow 80 # HTTP
sudo ufw allow 443 # HTTPS
sudo ufw enable
```
### SSL 证书配置
```bash
# 使用 Let's Encrypt
sudo certbot --nginx -d your-domain.com
# 或使用自签名证书
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout private.key -out certificate.crt
```
## 📈 性能优化
### 启用 Gzip 压缩
在 Nginx 配置中添加:
```nginx
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;
```
### 缓存配置
```nginx
# 静态资源缓存
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
```
## 🚨 故障排除
### 常见问题
1. **端口被占用**
```bash
# 检查端口占用
netstat -tlnp | grep 3001
# 杀死占用进程
sudo kill -9 <PID>
```
2. **权限问题**
```bash
# 确保数据文件可写
chmod 644 user-data.json
chown www-data:www-data user-data.json
```
3. **内存不足**
```bash
# 增加 Node.js 内存限制
node --max-old-space-size=1024 server.js
```
### 日志分析
```bash
# 查看错误日志
grep "ERROR" /var/log/worktime.log
# 查看访问统计
awk '{print $1}' /var/log/worktime.log | sort | uniq -c | sort -nr
```
## 📞 技术支持
如果遇到问题,请检查:
1. 应用日志文件
2. 系统资源使用情况
3. 网络连接状态
4. 数据库文件权限
## 🔄 更新部署
```bash
# 1. 停止应用
pm2 stop worktime
# 2. 拉取最新代码
git pull origin main
# 3. 安装依赖
npm install
# 4. 重新构建
npm run build
# 5. 重启应用
pm2 restart worktime
```
---
**注意**: 请根据您的具体环境和需求调整配置参数。