# 1Panel 部署指南 - SalesPipeline ## 系統需求 - Python 3.10+ - Node.js 18+ (僅用於建置前端) - MySQL 5.7+ / 8.0+ --- ## 部署步驟 ### 1. 上傳專案 將專案上傳至伺服器,例如:`/opt/salespipeline` ```bash # 建立專案目錄 mkdir -p /opt/salespipeline cd /opt/salespipeline # 上傳或 clone 專案 # git clone . ``` ### 2. 設定 Python 虛擬環境 ```bash cd /opt/salespipeline/backend # 建立虛擬環境 python3 -m venv venv # 啟動虛擬環境 source venv/bin/activate # 安裝依賴 pip install -r requirements.txt ``` ### 3. 設定環境變數 ```bash # 複製範本 cp .env.example .env # 編輯環境變數 nano .env ``` 編輯 `.env` 檔案: ```env # Database Configuration DB_HOST=your_mysql_host DB_PORT=3306 DB_USER=your_user DB_PASSWORD=your_password DB_DATABASE=your_database # JWT Configuration SECRET_KEY=<更換為強密碼> ALGORITHM=HS256 ACCESS_TOKEN_EXPIRE_MINUTES=1440 # Application Settings APP_HOST=0.0.0.0 APP_PORT=8000 WORKERS=2 DEBUG=False # CORS Settings (開發時使用) CORS_ORIGINS=http://localhost:3000,http://127.0.0.1:3000 ``` ### 4. 建置前端 ```bash cd /opt/salespipeline/frontend # 安裝依賴 npm install # 建置 (輸出至 backend/static) npm run build ``` ### 5. 初始化資料庫 資料庫表會在應用程式首次啟動時自動建立。 ### 6. 設定 Systemd 服務 建立服務檔案 `/etc/systemd/system/salespipeline.service`: ```ini [Unit] Description=SalesPipeline Application After=network.target [Service] Type=simple User=www-data Group=www-data WorkingDirectory=/opt/salespipeline/backend Environment="PATH=/opt/salespipeline/backend/venv/bin" EnvironmentFile=/opt/salespipeline/backend/.env ExecStart=/opt/salespipeline/backend/venv/bin/python run.py Restart=always RestartSec=10 [Install] WantedBy=multi-user.target ``` 啟動服務: ```bash # 重新載入 systemd sudo systemctl daemon-reload # 啟動服務 sudo systemctl start salespipeline # 設定開機自啟 sudo systemctl enable salespipeline # 查看狀態 sudo systemctl status salespipeline # 查看日誌 sudo journalctl -u salespipeline -f ``` --- ## 1Panel 設定 ### 方案 A:直接使用 Python 網站 (推薦) 1. 進入 1Panel 控制台 2. 網站 → 建立網站 → Python 專案 3. 設定: - 專案路徑:`/opt/salespipeline/backend` - Python 版本:3.10 - 啟動命令:`/opt/salespipeline/backend/venv/bin/python run.py` - Port:依 `.env` 中的 `APP_PORT` 設定 ### 方案 B:使用反向代理 1. 建立 Systemd 服務 (如上述步驟 6) 2. 1Panel → 網站 → 建立網站 → 反向代理 3. 設定: - 網站域名:your-domain.com - 代理地址:`http://127.0.0.1:` (依 .env 設定) --- ## 目錄結構 ``` /opt/salespipeline/ ├── backend/ │ ├── app/ # 應用程式碼 │ ├── data/ # 資料目錄 (上傳檔案) │ ├── static/ # 前端建置檔案 │ ├── venv/ # Python 虛擬環境 │ ├── .env # 環境變數 │ ├── requirements.txt │ └── run.py # 啟動腳本 └── frontend/ ├── src/ ├── package.json └── ... ``` --- ## 環境變數說明 | 變數名稱 | 說明 | 預設值 | |---------|------|--------| | DB_HOST | 資料庫主機 | localhost | | DB_PORT | 資料庫端口 | 3306 | | DB_USER | 資料庫使用者 | root | | DB_PASSWORD | 資料庫密碼 | - | | DB_DATABASE | 資料庫名稱 | sales_pipeline | | SECRET_KEY | JWT 密鑰 | - | | ALGORITHM | JWT 演算法 | HS256 | | ACCESS_TOKEN_EXPIRE_MINUTES | Token 過期時間(分鐘) | 1440 | | APP_HOST | 應用監聽地址 | 0.0.0.0 | | APP_PORT | 應用監聽端口 | 8000 | | WORKERS | 工作進程數 | 1 | | DEBUG | 除錯模式 | False | | TABLE_PREFIX | 資料表前綴 | PJ_SOA_ | | CORS_ORIGINS | 允許的跨域來源 | - | --- ## 常用指令 ```bash # 啟動服務 sudo systemctl start salespipeline # 停止服務 sudo systemctl stop salespipeline # 重啟服務 sudo systemctl restart salespipeline # 查看日誌 sudo journalctl -u salespipeline -f # 更新程式碼後重建 cd /opt/salespipeline/frontend && npm run build sudo systemctl restart salespipeline ``` --- ## 故障排除 ### 連線資料庫失敗 1. 確認 `.env` 中的資料庫連線資訊正確 2. 確認防火牆允許連接資料庫 Port ### 無法存取網站 1. 確認服務正在運行:`systemctl status salespipeline` 2. 確認 APP_PORT 未被佔用 3. 檢查防火牆設定 ### 靜態檔案 404 確認前端已建置:`ls /opt/salespipeline/backend/static/`