Skip to main content

亚马逊 EC2

🌐 Amazon EC2

将你的 Mastra 应用部署到 Amazon EC2(弹性云计算)。

🌐 Deploy your Mastra applications to Amazon EC2 (Elastic Cloud Compute).

note

本指南假设你的 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

  1. 连接到你的 EC2 实例并克隆你的仓库:

    🌐 Connect to your EC2 instance and clone your repository:

    git clone https://github.com/<your-username>/<your-repository>.git

    导航到仓库目录:

    🌐 Navigate to the repository directory:

    cd "<your-repository>"
  2. 安装依赖:

    🌐 Install dependencies:

    npm install
  3. 创建一个 .env 文件并添加你的环境变量:

    🌐 Create a .env file and add your environment variables:

    touch .env

    编辑 .env 文件并添加你的环境变量:

    🌐 Edit the .env file and add your environment variables:

    OPENAI_API_KEY=<your-openai-api-key>
    # Add other required environment variables
  4. 构建应用:

    🌐 Build the application:

    npm run build
  5. 运行应用:

    🌐 Run the application:

    node --env-file=".env" .mastra/output/index.mjs
    note

    你的 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