> Discover all available pages from the documentation index: https://mastra.zisheng.pro/llms.txt # 将 Mastra 部署到 Amazon EC2 将 Mastra Server 部署到 Amazon EC2。Server 在你管理的基础设施上运行,可支持长时运行的 Agent 和 Workflow。 > **信息:** 本指南介绍如何部署 [Mastra Server](https://mastra.zisheng.pro/docs/server/mastra-server)。如果你使用的是 [Server 适配器](https://mastra.zisheng.pro/docs/server/server-adapters)或 [Web 框架](https://mastra.zisheng.pro/docs/deployment/web-framework),请按该框架的常规方式进行部署。 ## 开始之前 你需要: - 一个 [Mastra 应用](https://mastra.zisheng.pro/guides/getting-started/quickstart) - 一个已安装 Git 和 Node.js 22.13.0+ 的 [EC2](https://aws.amazon.com/ec2/) 实例(Ubuntu 或 Amazon Linux) 用于生产环境时,你还需要: - 一个指向实例的域名(SSL 证书所必需) - 域名对应的 SSL 证书(例如使用 [Certbot](https://certbot.eff.org/) 和 Let's Encrypt) - 一个将流量转发到应用的反向代理(例如 [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 Key): ```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/docs/server/auth)。 6. Mastra Server 现在已在 4111 端口上运行,但只能从本地访问。 你可以在 EC2 安全组中开放 4111 端口以直接访问,也可以配置反向代理(例如 nginx)监听 80 和 443 端口,并将请求转发到 `http://localhost:4111`。 在生产环境中,应使用反向代理以便配置 HTTPS。HTTPS 会加密流量,也是大多数 Webhook 集成以及 Agent 所交互外部服务的必要条件。 7. 访问 `https:///api/agents` 验证部署;该端点应返回 Agent 的 JSON 列表。 8. 现在可以通过 HTTP 调用 Mastra 端点。 ## 后续步骤 - [从 Mastra Client 连接](https://mastra.zisheng.pro/docs/server/mastra-client) - [部署概览](https://mastra.zisheng.pro/docs/deployment/overview)