亚马逊 EC2
🌐 Amazon EC2
将你的 Mastra 应用部署到 Amazon EC2(弹性云计算)。
🌐 Deploy your Mastra applications to Amazon EC2 (Elastic Cloud Compute).
本指南假设你的 Mastra 应用是使用默认的 npx create-mastra@latest 命令创建的。有关如何创建新的 Mastra 应用的更多信息,请参阅我们的入门指南
先决条件Direct link to 先决条件
🌐 Prerequisites
- 具有 EC2 访问权限的 AWS 账户
- 运行 Ubuntu 24+ 或 Amazon Linux 的 EC2 实例
- 一个 A 记录指向你的实例的域名
- 配置的反向代理(例如,使用 nginx)
- 已配置 SSL 证书(例如,使用 Let's Encrypt)
- 在你的实例上安装 Node.js 22.13.0 或更高版本
部署步骤Direct link to 部署步骤
🌐 Deployment Steps
连接到你的 EC2 实例并克隆你的仓库:
🌐 Connect to your EC2 instance and clone your repository:
- Public Repository
- Private Repository
git clone https://github.com/<your-username>/<your-repository>.gitgit clone https://<your-username>:<your-personal-access-token>@github.com/<your-username>/<your-repository>.git导航到仓库目录:
🌐 Navigate to the repository directory:
cd "<your-repository>"安装依赖:
🌐 Install dependencies:
- npm
- pnpm
- Yarn
- Bun
npm installpnpm installyarn installbun install创建一个
.env文件并添加你的环境变量:🌐 Create a
.envfile and add your environment variables:touch .env编辑
.env文件并添加你的环境变量:🌐 Edit the
.envfile and add your environment variables:OPENAI_API_KEY=<your-openai-api-key>
# Add other required environment variables构建应用:
🌐 Build the application:
npm run build运行应用:
🌐 Run the application:
node --env-file=".env" .mastra/output/index.mjsnote你的 Mastra 应用默认将在端口 4111 上运行。请确保你的反向代理已配置为将请求转发到该端口。
连接到你的Mastra服务器Direct link to 连接到你的Mastra服务器
🌐 Connect to your Mastra server
现在,你可以使用 @mastra/client-js 包中的 MastraClient 从客户端应用连接到 Mastra 服务器。
🌐 You can now connect to your Mastra server from your client application using a MastraClient from the @mastra/client-js package.
有关更多信息,请参阅 MastraClient 文档。
🌐 Refer to the MastraClient documentation for more information.
import { MastraClient } from "@mastra/client-js";
const mastraClient = new MastraClient({
baseUrl: "https://<your-domain-name>",
});
下一步Direct link to 下一步
🌐 Next steps