Files
aiapply/服务器稳定性优化方案.txt
2025-10-17 23:38:10 +08:00

350 lines
7.5 KiB
Plaintext
Raw Permalink 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.
# PromptForge 服务器稳定性优化方案
## 📊 当前服务器资源分析
### 硬件配置
- **CPU**: 2 核心
- **内存**: 3.6GB 总内存1.9GB 可用
- **磁盘**: 89GB 总容量62GB 可用 (28% 使用率)
- **负载**: 0.24 (低负载,运行良好)
### 当前应用资源使用
- **Next.js 服务**: 323MB 内存 (8.5%)
- **总内存使用**: 1.4GB (38.9%)
- **可用内存**: 1.9GB (充足)
## 🎯 稳定性优化方案
### 方案一PM2 进程管理 (推荐)
#### 1. 安装和配置 PM2
```bash
# 安装 PM2
npm install -g pm2
# 创建 PM2 配置文件
cat > ecosystem.config.js << 'EOF'
module.exports = {
apps: [{
name: 'promptforge',
script: 'npx',
args: 'next@14.0.4 dev -H 0.0.0.0 -p 3000',
cwd: '/home/renjianbo/aiapply',
instances: 1, // 单实例,适合小服务器
autorestart: true,
watch: false,
max_memory_restart: '800M', // 内存超过800MB自动重启
min_uptime: '10s',
max_restarts: 10,
restart_delay: 4000,
env: {
NODE_ENV: 'production',
PORT: 3000
},
error_file: '/home/renjianbo/aiapply/logs/err.log',
out_file: '/home/renjianbo/aiapply/logs/out.log',
log_file: '/home/renjianbo/aiapply/logs/combined.log',
time: true
}]
}
EOF
# 创建日志目录
mkdir -p logs
# 启动服务
pm2 start ecosystem.config.js
# 设置开机自启动
pm2 startup
pm2 save
```
#### 2. PM2 监控和管理
```bash
# 查看服务状态
pm2 status
# 查看实时日志
pm2 logs promptforge
# 重启服务
pm2 restart promptforge
# 停止服务
pm2 stop promptforge
# 监控资源使用
pm2 monit
```
### 方案二:系统级优化
#### 1. 系统资源限制
```bash
# 创建系统限制配置
sudo tee /etc/security/limits.d/promptforge.conf << 'EOF'
# PromptForge 用户资源限制
renjianbo soft nofile 65536
renjianbo hard nofile 65536
renjianbo soft nproc 32768
renjianbo hard nproc 32768
EOF
# 应用限制
ulimit -n 65536
ulimit -u 32768
```
#### 2. 内存优化
```bash
# 调整系统内存参数
sudo tee -a /etc/sysctl.conf << 'EOF'
# 内存优化
vm.swappiness = 10
vm.dirty_ratio = 15
vm.dirty_background_ratio = 5
vm.overcommit_memory = 1
EOF
# 应用配置
sudo sysctl -p
```
### 方案三Nginx 反向代理优化
#### 1. 创建优化的 Nginx 配置
```bash
# 创建 Nginx 配置文件
sudo tee /etc/nginx/conf.d/promptforge.conf << 'EOF'
upstream promptforge_backend {
server 127.0.0.1:3000;
keepalive 32;
}
server {
listen 80;
server_name 101.43.95.130;
# 重定向到 HTTPS
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name 101.43.95.130;
# SSL 配置
ssl_certificate /path/to/your/cert.pem;
ssl_certificate_key /path/to/your/key.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512;
# 性能优化
client_max_body_size 10M;
client_body_timeout 60s;
client_header_timeout 60s;
keepalive_timeout 65s;
send_timeout 60s;
# 压缩配置
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)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
# 反向代理
location / {
proxy_pass http://promptforge_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_cache_bypass $http_upgrade;
# 超时设置
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
# 缓冲设置
proxy_buffering on;
proxy_buffer_size 4k;
proxy_buffers 8 4k;
}
}
EOF
```
### 方案四:监控和告警
#### 1. 创建监控脚本
```bash
# 创建监控脚本
cat > monitor.sh << 'EOF'
#!/bin/bash
# 监控脚本
LOG_FILE="/home/renjianbo/aiapply/logs/monitor.log"
DATE=$(date '+%Y-%m-%d %H:%M:%S')
# 检查服务状态
if ! netstat -tlnp | grep -q :3000; then
echo "[$DATE] 服务未运行,尝试重启..." >> $LOG_FILE
cd /home/renjianbo/aiapply
pm2 restart promptforge
fi
# 检查内存使用
MEMORY_USAGE=$(free | grep Mem | awk '{printf "%.1f", $3/$2 * 100.0}')
if (( $(echo "$MEMORY_USAGE > 80" | bc -l) )); then
echo "[$DATE] 内存使用率过高: ${MEMORY_USAGE}%" >> $LOG_FILE
fi
# 检查磁盘空间
DISK_USAGE=$(df / | tail -1 | awk '{print $5}' | sed 's/%//')
if [ $DISK_USAGE -gt 80 ]; then
echo "[$DATE] 磁盘使用率过高: ${DISK_USAGE}%" >> $LOG_FILE
fi
echo "[$DATE] 监控检查完成" >> $LOG_FILE
EOF
chmod +x monitor.sh
# 添加到 crontab (每5分钟检查一次)
(crontab -l 2>/dev/null; echo "*/5 * * * * /home/renjianbo/aiapply/monitor.sh") | crontab -
```
### 方案五:并发访问优化
#### 1. 连接池配置
```bash
# 创建环境变量配置
cat >> .env.local << 'EOF'
# 数据库连接池
DATABASE_POOL_SIZE=5
DATABASE_POOL_TIMEOUT=20000
# 应用配置
NEXT_PUBLIC_MAX_CONCURRENT_REQUESTS=10
NEXT_PUBLIC_REQUEST_TIMEOUT=30000
EOF
```
#### 2. 系统连接优化
```bash
# 调整系统连接参数
sudo tee -a /etc/sysctl.conf << 'EOF'
# 网络连接优化
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 5000
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_intvl = 60
net.ipv4.tcp_keepalive_probes = 3
EOF
sudo sysctl -p
```
## 📈 预期性能提升
### 并发访问能力
- **当前**: 5-10 并发用户
- **优化后**: 15-25 并发用户
- **响应时间**: 减少 30-50%
### 稳定性提升
- **自动重启**: 进程异常时自动恢复
- **内存监控**: 防止内存泄漏
- **负载均衡**: Nginx 反向代理
- **监控告警**: 实时状态监控
## 🚀 实施步骤
### 第一步PM2 部署 (立即实施)
```bash
# 停止当前服务
pkill -f "next dev"
# 安装 PM2
npm install -g pm2
# 启动 PM2 服务
pm2 start ecosystem.config.js
pm2 startup
pm2 save
```
### 第二步:系统优化 (可选)
```bash
# 应用系统限制
sudo tee /etc/security/limits.d/promptforge.conf << 'EOF'
renjianbo soft nofile 65536
renjianbo hard nofile 65536
EOF
# 调整内存参数
sudo tee -a /etc/sysctl.conf << 'EOF'
vm.swappiness = 10
net.core.somaxconn = 65535
EOF
sudo sysctl -p
```
### 第三步:监控部署 (推荐)
```bash
# 创建监控脚本
./monitor.sh
# 设置定时监控
(crontab -l 2>/dev/null; echo "*/5 * * * * /home/renjianbo/aiapply/monitor.sh") | crontab -
```
## ⚠️ 注意事项
1. **内存限制**: 当前服务器内存有限,避免过度优化
2. **CPU 核心**: 2核心服务器建议单实例运行
3. **磁盘空间**: 定期清理日志文件
4. **网络带宽**: 监控带宽使用情况
5. **备份策略**: 定期备份重要数据
## 📊 监控指标
### 关键指标
- **内存使用率**: < 80%
- **CPU 使用率**: < 70%
- **响应时间**: < 2秒
- **并发连接**: < 25
- **错误率**: < 1%
### 监控命令
```bash
# 实时监控
pm2 monit
# 查看日志
pm2 logs promptforge --lines 100
# 系统资源
htop
iostat -x 1
```
---
**创建时间**: $(date)
**服务器配置**: 2核3.6GB内存
**预期并发**: 15-25用户
**优化目标**: 稳定性 + 性能