# 生产部署指南 ## 目录结构 ``` /www/chat-app/ ├── public/ # 前端 dist + PHP 入口 │ ├── index.html # 前端入口 │ ├── assets/ # 前端 JS/CSS │ └── index.php # PHP 后端入口 ├── src/ # PHP 业务代码 ├── vendor/ # composer 依赖 ├── chat.db # SQLite 数据库 └── composer.json ``` ## 部署步骤 ### 1. 安装 PHP 依赖 ```bash cd /www/chat-app composer install --no-dev --optimize-autoloader ``` ### 2. 前端构建并放入 public 本地执行: ```bash npm run build cp -r dist/* server/public/ ``` ### 3. Nginx 配置 ```nginx server { listen 80; server_name your-domain.com; root /www/chat-app/public; index index.html index.php; # 前端静态资源(JS/CSS/图片等) location /assets/ { try_files $uri =404; expires 1y; add_header Cache-Control "public, immutable"; } # API 请求 → PHP location /api/ { try_files $uri /index.php$is_args$args; } # 前端路由(SPA)→ index.html location / { try_files $uri $uri/ /index.html; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; # 或 unix:/run/php/php8.3-fpm.sock fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } ``` ### 4. PHP-FPM 配置要点 确保 `php.ini` 中: ```ini ; SQLite 需要 extension=pdo_sqlite ; SSE 流式输出需要 output_buffering = Off zlib.output_compression = Off ``` ### 5. 数据库权限 ```bash chmod 666 /www/chat-app/chat.db ``` ## Docker 部署(推荐) ### docker-compose.yml 示例 ```yaml version: '3.8' services: nginx: image: nginx:alpine ports: - "80:80" volumes: - ./dist:/var/www/dist:ro - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro depends_on: - php php: image: php:8.3-fpm volumes: - ./server:/var/www/html - /data:/data # 数据库挂载到独立目录,避免权限问题 environment: - DB_PATH=/data/chat.db # 通过环境变量覆盖数据库路径 - JWT_SECRET=your-secret # 生产环境务必修改 - CHAT_PASSWORD=your-pass # 生产环境务必修改 ``` ### 环境变量说明 | 变量 | 默认值 | 说明 | |------|--------|------| | `DB_PATH` | `server/chat.db` | SQLite 数据库路径,建议挂载到独立目录 | | `JWT_SECRET` | `chat-terminal-secret-key-change-in-production` | JWT 签名密钥,**生产环境必须修改** | | `CHAT_PASSWORD` | `admin` | 登录密码,**生产环境必须修改** | ## 简化版:PHP 内置服务器(仅测试) ```bash cd /www/chat-app/public php -S 0.0.0.0:8000 ``` > 注意:PHP 内置服务器性能差,不适合生产。