# 基金投资管理系统 - Windows 服务器部署指南

## 一、系统要求

- Windows Server 2016/2019/2022
- Node.js 18.x 或 20.x LTS
- 4GB+ RAM
- 50GB+ 可用磁盘空间
- 域名已备案并解析到服务器 IP

## 二、部署架构

```
[用户浏览器]
     ↓ HTTPS (443)
[Nginx 反向代理]
  ↙           ↘
[:80 HTTP]  [:3001 API]
     ↓            ↓
[静态文件]  [Node.js 后端]
               ↓
          [SQLite 数据库]
```

## 三、详细部署步骤

### 步骤 1：服务器环境准备

#### 1.1 安装 Node.js

1. 下载 Node.js LTS 版本：
   https://nodejs.org/dist/v20.11.0/node-v20.11.0-x64.msi

2. 运行安装程序，勾选"Automatically install necessary tools"

3. 验证安装：
   ```
   node -v    # 应显示 v20.11.0
   npm -v     # 应显示 10.2.4
   ```

#### 1.2 安装 Nginx for Windows

1. 下载 Nginx Windows 版本：
   https://nginx.org/en/download.html

2. 解压到 `C:\nginx`

3. 测试运行：
   ```
   cd C:\nginx
   nginx.exe
   ```

4. 访问 http://localhost 确认 nginx 正常运行

#### 1.3 安装 PM2（Node.js 进程管理器）

```bash
npm install -g pm2
pm2 --version
```

### 步骤 2：部署后端服务

#### 2.1 创建部署目录

```bash
mkdir C:\deploy\pe-investment
cd C:\deploy\pe-investment
```

#### 2.2 上传后端代码

将以下文件/文件夹上传到服务器：
- `server/` 文件夹
- `data/` 文件夹（包含数据库）
- `package.json`
- `package-lock.json`

#### 2.3 安装后端依赖

```bash
cd C:\deploy\pe-investment
npm install --production
```

#### 2.4 配置环境变量

创建 `C:\deploy\pe-investment\.env` 文件：

```env
NODE_ENV=production
PORT=3001
JWT_SECRET=your-super-secret-jwt-key-change-this-in-production
CORS_ORIGIN=https://your-domain.com
```

#### 2.5 启动后端服务

```bash
cd C:\deploy\pe-investment
pm2 start server/index.js --name pe-api
pm2 save
pm2 startup
```

#### 2.6 验证后端服务

```bash
curl http://localhost:3001/api/health
```

应返回：`{"status":"ok"}`

### 步骤 3：部署前端静态文件

#### 3.1 复制构建文件

将 `client/build/` 文件夹的内容复制到 `C:\deploy\pe-investment\public`

#### 3.2 配置 Nginx

编辑 `C:\nginx\conf\nginx.conf`：

```nginx
worker_processes 1;

events {
    worker_connections 1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    # Gzip 压缩
    gzip on;
    gzip_vary on;
    gzip_min_length 1024;
    gzip_types text/plain text/css text/xml text/javascript application/javascript application/json;

    server {
        listen 80;
        server_name your-domain.com www.your-domain.com;

        # 重定向到 HTTPS
        return 301 https://$server_name$request_uri;
    }

    server {
        listen 443 ssl http2;
        server_name your-domain.com www.your-domain.com;

        # SSL 证书配置
        ssl_certificate C:/ssl/your-domain.com.pem;
        ssl_certificate_key C:/ssl/your-domain.com.key;
        ssl_session_timeout 1d;
        ssl_session_cache shared:SSL:50m;
        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
        ssl_prefer_server_ciphers off;

        root C:/deploy/pe-investment/public;
        index index.html;

        # 前端路由
        location / {
            try_files $uri $uri/ /index.html;
        }

        # API 代理
        location /api/ {
            proxy_pass http://127.0.0.1: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;

            # 文件上传大小限制
            client_max_body_size 50M;
        }

        # 静态资源缓存
        location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2)$ {
            expires 1y;
            add_header Cache-Control "public, immutable";
        }

        # 安全头
        add_header X-Frame-Options "SAMEORIGIN" always;
        add_header X-Content-Type-Options "nosniff" always;
        add_header X-XSS-Protection "1; mode=block" always;
    }
}
```

#### 3.3 测试 Nginx 配置

```bash
cd C:\nginx
nginx.exe -t
```

### 步骤 4：SSL 证书配置

#### 4.1 申请 SSL 证书（使用 Let's Encrypt 免费证书）

下载 Certbot：https://certbot.eff.org/downloads

以管理员身份运行命令提示符：

```bash
cd C:\certbot
certbot-auto certonly --nginx -d your-domain.com -d www.your-domain.com
```

#### 4.2 复制证书到 Nginx 配置目录

```bash
mkdir C:\ssl
copy "C:\Certbot\live\your-domain.com\fullchain.pem" C:\ssl\your-domain.com.pem
copy "C:\Certbot\live\your-domain.com\privkey.pem" C:\ssl\your-domain.com.key
```

### 步骤 5：启动所有服务

#### 5.1 启动 Nginx

```bash
cd C:\nginx
nginx.exe
```

#### 5.2 验证部署

访问：`https://your-domain.com`

默认管理员账号：
- 用户名：`admin`
- 密码：`admin123`

## 四、运维管理

### 查看服务状态

```bash
pm2 status
pm2 logs pe-api
```

### 重启服务

```bash
pm2 restart pe-api
nginx.exe -s reload
```

### 日志管理

```bash
# 查看后端日志
pm2 logs pe-api --lines 100

# Nginx 日志
C:\nginx\logs\access.log
C:\nginx\logs\error.log
```

### 数据库备份

```bash
copy C:\deploy\pe-investment\data\pe_management.db C:\backup\pe_management_20240101.db
```

## 五、故障排查

### 后端无法启动

1. 检查端口占用：
   ```bash
   netstat -ano | findstr :3001
   ```

2. 查看错误日志：
   ```bash
   pm2 logs pe-api
   ```

### 前端无法访问

1. 检查 Nginx 状态：
   ```bash
   tasklist | findstr nginx
   ```

2. 检查 Nginx 错误日志

### 数据库连接失败

1. 确认数据库文件存在
2. 检查文件权限（Node.js 进程需要有读写权限）

## 六、安全建议

1. 修改默认管理员密码
2. 设置强 JWT_SECRET
3. 启用防火墙，仅开放 80 和 443 端口
4. 定期备份数据库
5. 启用 HTTPS 并强制跳转
6. 考虑使用云数据库（如阿里云 RDS）替代 SQLite

## 七、紧急回滚

如果部署失败，停止所有服务并恢复原配置：

```bash
# 停止服务
pm2 stop pe-api
nginx.exe -s stop

# 恢复备份文件
xcopy C:\backup\pe-investment\* C:\deploy\pe-investment\ /E /Y

# 重新启动
pm2 start pe-api
nginx.exe
```
