General Medusa Application Deployment Guide

In this document, you'll learn the general steps to deploy your Medusa application. How you apply these steps depend on your chosen hosting provider or platform.

Find how-to guides for specific platforms in this documentation.

Deploy with Medusa Cloud

Want Medusa to manage and maintain your infrastructure? Sign up and learn more about Medusa Cloud

Medusa Cloud is our managed services offering that makes deploying and operating Medusa applications possible without having to worry about configuring, scaling, and maintaining infrastructure. Medusa Cloud hosts your server, Admin dashboard, database, and Redis instance.

With Medusa Cloud, you maintain full customization control as you deploy your own modules and customizations directly from GitHub:

  • Push to deploy.
  • Multiple testing environments.
  • Preview environments for new PRs.
  • Test on production-like data.

Hosting Provider Requirements#

When you deploy your Medusa application, make sure your chosen hosting provider supports deploying the following resources:

  1. PostgreSQL database: If your hosting provider doesn't support database hosting, you must find another hosting provider for the PostgreSQL database.
  2. Redis database: If your hosting provider doesn't support database hosting, you must find another hosting provider for the Redis database.
  3. Medusa application in server and worker mode. This means your hosting provider should support deploying two applications or instances from the same codebase.
  4. For optimal experience, the hosting provider and plan must offer at least 2GB of RAM.

1. Configure Medusa Application#

Worker Mode#

The workerMode configuration determines which mode the Medusa application runs in.

When you deploy the Medusa application, you deploy two instances: one in server mode, and one in worker mode.

Learn more about the workerMode configuration in this document.

So, add the following configuration in medusa-config.ts:

medusa-config.ts
1module.exports = defineConfig({2  projectConfig: {3    // ...4    workerMode: process.env.MEDUSA_WORKER_MODE as "shared" | "worker" | "server",5  },6})

Later, you’ll set different values of the MEDUSA_WORKER_MODE environment variable for each Medusa application deployment or instance.

Configure Medusa Admin#

You need to disable the Medusa Admin in the worker Medusa application, while keeping it enabled in the server Medusa application. So, add the following configuration in medusa-config.ts:

medusa-config.ts
1module.exports = defineConfig({2  // ...3  admin: {4    disable: process.env.DISABLE_MEDUSA_ADMIN === "true",5  },6})

Later, you’ll set different values of the DISABLE_MEDUSA_ADMIN environment variable.

Configure Redis URL#

The redisUrl configuration specifies the connection URL to Redis to store the Medusa server's session.

NoteLearn more in the Medusa Configuration documentation .

So, add the following configuration in medusa-config.ts :

medusa-config.ts
1module.exports = defineConfig({2  projectConfig: {3    // ...4    redisUrl: process.env.REDIS_URL,5  },6})

2. Add predeploy Script#

Before you start the Medusa application in production, you should always run migrations and sync links.

So, add the following script in package.json:

Code
1"scripts": {2  // ...3  "predeploy": "medusa db:migrate"4},

3. Install Production Modules and Providers#

By default, your Medusa application uses modules and providers useful for development, such as the In-Memory Cache Module or the Local File Module Provider.

It’s highly recommended to instead use modules and providers suitable for production, including:

Then, add these modules in medusa-config.ts:

medusa-config.ts
1import { Modules } from "@medusajs/framework/utils"2
3module.exports = defineConfig({4  // ...5  modules: [6    {7      resolve: "@medusajs/medusa/cache-redis",8      options: {9        redisUrl: process.env.REDIS_URL,10      },11    },12    {13      resolve: "@medusajs/medusa/event-bus-redis",14      options: {15        redisUrl: process.env.REDIS_URL,16      },17    },18    {19      resolve: "@medusajs/medusa/workflow-engine-redis",20      options: {21        redis: {22          url: process.env.REDIS_URL,23        },24      },25    },26  ],27})
TipCheck out the Integrations and Architectural Modules documentation for other modules and providers to use.

4. Create PostgreSQL and Redis Databases#

Your Medusa application must connect to PostgreSQL and Redis databases. So, before you deploy it, create production PostgreSQL and Redis databases.

If your hosting provider doesn't support databases, you can use Neon for PostgreSQL database hosting, and Redis Cloud for the Redis database hosting.

After hosting both databases, keep their connection URLs for the next steps.


5. Deploy Medusa Application in Server Mode#

As mentioned earlier, you'll deploy two instances or create two deployments of your Medusa application: one in server mode, and the other in worker mode.

The deployment steps depend on your hosting provider. This section provides the general steps to perform during the deployment.

Set Environment Variables#

When setting the environment variables of the Medusa application, set the following variables:

Terminal
COOKIE_SECRET=supersecret # TODO GENERATE SECURE SECRETJWT_SECRET=supersecret  # TODO GENERATE SECURE SECRETSTORE_CORS= # STOREFRONT URLADMIN_CORS= # ADMIN URLAUTH_CORS= # STOREFRONT AND ADMIN URLS, SEPARATED BY COMMASDISABLE_MEDUSA_ADMIN=trueMEDUSA_WORKER_MODE=serverPORT=9000DATABASE_URL # POSTGRES DATABASE URLREDIS_URL= # REDIS DATABASE URL

Where:

  • The value of COOKIE_SECRET and JWT_SECRET must be a randomly generated secret.
  • STORE_CORS's value is the URL of your storefront. If you don’t have it yet, you can skip adding it for now.
  • ADMIN_CORS's value is the URL of the admin dashboard, which is the same as the server Medusa application. You can add it later if you don't currently have it.
  • AUTH_CORS's value is the URLs of any application authenticating users, customers, or other actor types, such as the storefront and admin URLs. The URLs are separated by commas. If you don’t have the URLs yet, you can set its value later.
  • Set DISABLE_MEDUSA_ADMIN's value to false so that the admin is built with the server application.
  • Set the PostgreSQL database's connection URL as the value of DATABASE_URL
  • Set the Redis database's connection URL as the value of REDIS_URL.

Feel free to add any other relevant environment variables, such as for integrations and architectural modules.

Set Start Command#

The Medusa application's production build, which is created using the build command, outputs the Medusa application to .medusa/server.

So, you must run the start command from the .medusa/server directory.

If your hosting provider doesn't support setting a current-working directory, set the start command to the following:

Set Backend URL in Admin Configuration#

After you’ve obtained the Medusa application’s URL, add the following configuration to medusa-config.ts:

medusa-config.ts
1module.exports = defineConfig({2  // ...3  admin: {4    // ...5    backendUrl: process.env.MEDUSA_BACKEND_URL,6  },7})

Then, push the changes to the GitHub repository or deployed application.

In your hosting provider, add or modify the following environment variables for the Medusa application in server mode:

Terminal
ADMIN_CORS= # MEDUSA APPLICATION URLAUTH_CORS= # ADD MEDUSA APPLICATION URLMEDUSA_BACKEND_URL= # URL TO DEPLOYED MEDUSA APPLICATION

Where you set the value of ADMIN_CORS and MEDUSA_BACKEND_URL to the Medusa application’s URL, and you add the URL to AUTH_CORS.

TipRemember to separate URLs in AUTH_CORS by commas.

6. Deploy Medusa Application in Worker Mode#

Next, you'll deploy the Medusa application in worker mode.

As explained in the previous section, the deployment steps depend on your hosting provider. This section provides the general steps to perform during the deployment.

Set Environment Variables#

When setting the environment variables of the Medusa application, set the following variables:

Terminal
COOKIE_SECRET=supersecret # TODO GENERATE SECURE SECRETJWT_SECRET=supersecret  # TODO GENERATE SECURE SECRETDISABLE_MEDUSA_ADMIN=trueMEDUSA_WORKER_MODE=workerPORT=9000DATABASE_URL # POSTGRES DATABASE URLREDIS_URL= # REDIS DATABASE URL

Where:

  • The value of COOKIE_SECRET and JWT_SECRET must be a randomly generated secret.
  • Set DISABLE_MEDUSA_ADMIN's value to true so that the admin isn't built with the worker application.
  • Set the PostgreSQL database's connection URL as the value of DATABASE_URL
  • Set the Redis database's connection URL as the value of REDIS_URL.

Feel free to add any other relevant environment variables, such as for integrations and architectural modules.

Set Start Command#

The Medusa application's production build, which is created using the build command, outputs the Medusa application to .medusa/server.

So, you must run the start command from the .medusa/server directory.

If your hosting provider doesn't support setting a current-working directory, set the start command to the following:


7. Test Deployed Application#

Once the application is deployed and live, go to <APP_URL>/health, where <APP_URL> is the URL of the Medusa application in server mode. If the deployment was successful, you’ll see the OK response.

The Medusa Admin is also available at <APP_URL>/app.


Create Admin User#

If your hosting provider supports running commands in your Medusa application's directory, run the following command to create an admin user:

Terminal
npx medusa user -e admin-medusa@test.com -p supersecret

Replace the email admin-medusa@test.com and password supersecret with the credentials you want.

You can use these credentials to log into the Medusa Admin dashboard.

Was this chapter helpful?
Edit this page