docs/src/content/en/guides/deployment/amazon-ec2.mdx
import Steps from "@site/src/components/Steps"; import StepItem from "@site/src/components/StepItem"; import Tabs from "@theme/Tabs"; import TabItem from "@theme/TabItem";
Deploy your Mastra server to Amazon EC2. This gives you full control over your server environment and supports long-running agents and workflows.
:::info This guide covers deploying the Mastra server. If you're using a server adapter or web framework, deploy the way you normally would for that framework. :::
You'll need:
For production, you'll also need:
<Tabs>
<TabItem value="public" label="Public Repository">
```bash
git clone https://github.com/<your-username>/<your-repository>.git
```
</TabItem>
<TabItem value="private" label="Private Repository">
```bash
git clone [email protected]:<your-username>/<your-repository>.git
```
</TabItem>
</Tabs>
Navigate to the repository directory:
```bash
cd "<your-repository>"
```
```bash npm2yarn
npm install
```
```bash
touch .env
```
Remember to set your environment variables needed to run your application (e.g. your model provider API key):
```bash
OPENAI_API_KEY=<your-openai-api-key>
# Add other required environment variables
```
```bash npm2yarn
npm run build
```
```bash
node --env-file=".env" .mastra/output/index.mjs
```
This is a basic example. In production, use a process manager like [PM2](https://pm2.keymetrics.io/) or [systemd](https://systemd.io/) to keep your application running and handle restarts.
:::warning
Set up [authentication](/docs/server/auth) before exposing your endpoints publicly.
:::
You can open port 4111 in your EC2 security group for direct access, or configure a reverse proxy (such as nginx) to listen on ports 80 and 443 and forward requests to `http://localhost:4111`.
In production, you should use a reverse proxy so you can configure HTTPS. HTTPS encrypts traffic and is required for most webhook integrations and external services your agents interact with.