> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-HK/llms.txt # 將 Mastra 部署至 Amazon EC2 將你的 Mastra 伺服器部署至 Amazon EC2。伺服器會在你管理的基礎設施上運行,並可支援長時間運行的 Agent 和 Workflow。 > **資訊:** 本指南涵蓋如何部署 [Mastra 伺服器](https://mastra.zisheng.pro/zh-HK/docs/server/mastra-server)。如你使用[伺服器適配器](https://mastra.zisheng.pro/zh-HK/docs/server/server-adapters)或[網頁框架](https://mastra.zisheng.pro/zh-HK/docs/deployment/web-framework),請依照該框架慣常的方式部署。 ## 開始之前 你需要: - 一個 [Mastra 應用程式](https://mastra.zisheng.pro/zh-HK/guides/getting-started/quickstart) - 一個已安裝 Git 和 Node.js 22.13.0 或以上版本的 [EC2](https://aws.amazon.com/ec2/) 執行個體(Ubuntu 或 Amazon Linux) 如要用於生產環境,你亦需要: - 一個指向執行個體的網域名稱(SSL 憑證的必要條件) - 網域的 SSL 憑證(例如配合 Let's Encrypt 使用 [Certbot](https://certbot.eff.org/)) - 反向代理(例如 [nginx](https://nginx.org/)),用來將流量轉送至你的應用程式 ## 部署 1. 連線至你的 EC2 執行個體並複製儲存庫: **公開儲存庫**: ```bash git clone https://github.com//.git ``` **私人儲存庫**: ```bash git clone git@github.com:/.git ``` 前往儲存庫目錄: ```bash cd "" ``` 2. 安裝依賴套件: **npm**: ```bash npm install ``` **pnpm**: ```bash pnpm install ``` **Yarn**: ```bash yarn install ``` **Bun**: ```bash bun install ``` 3. 建立 `.env` 檔案並加入環境變數: ```bash touch .env ``` 請記得設定運行應用程式所需的環境變數(例如模型 Provider 的 API 金鑰): ```bash OPENAI_API_KEY= # Add other required environment variables ``` 4. 建置應用程式: **npm**: ```bash npm run build ``` **pnpm**: ```bash pnpm run build ``` **Yarn**: ```bash yarn build ``` **Bun**: ```bash bun run build ``` 5. 運行應用程式: ```bash node --env-file=".env" .mastra/output/index.mjs ``` 這是一個基本範例。在生產環境中,請使用 [PM2](https://pm2.keymetrics.io/) 或 [systemd](https://systemd.io/) 等程序管理器,讓應用程式持續運行並處理重新啟動。 > **注意:** 對外公開端點前,請先設定[驗證](https://mastra.zisheng.pro/zh-HK/docs/server/auth)。 6. 你的 Mastra 伺服器現已在連接埠 4111 上運行,但只能從本機存取。 你可以在 EC2 安全群組中開啟連接埠 4111 以直接存取,或設定反向代理(例如 nginx)監聽連接埠 80 和 443,並將請求轉送至 `http://localhost:4111`。 在生產環境中,你應使用反向代理,以便設定 HTTPS。HTTPS 會加密網絡流量,而且大多數 webhook 整合和 Agent 互動的外部服務均要求使用 HTTPS。 7. 在 `https:///api/agents` 驗證部署;該網址應會傳回 Agent 的 JSON 清單。 8. 你現在可以透過 HTTP 呼叫 Mastra 端點。 ## 後續步驟 - [從 Mastra Client 連線](https://mastra.zisheng.pro/zh-HK/docs/server/mastra-client) - [部署概覽](https://mastra.zisheng.pro/zh-HK/docs/deployment/overview)