1 import { Prerequisites } from "docs-ui"
2
3 export const metadata = {
4 title: `General Medusa Application Deployment Guide`,
5 }
6
7 # {metadata.title}
8
9 In this guide, you'll learn the general steps to deploy your Medusa application. How you apply these steps depend on your chosen hosting provider or platform.
10
11 <Note title="Deploy with Cloud" type="success">
12
13 Before deciding to self-host your Medusa application, consider reading the [Cloud vs. Self-Hosting comparison guide](!cloud!/comparison). By using Cloud, you can avoid the complexities and challenges of self-hosting, while benefiting from automated deployment, scaling, and maintenance.
14
15 With Cloud, you also maintain full customization control as you deploy your own modules and customizations directly from GitHub:
16
17 - Push to deploy.
18 - Multiple testing environments.
19 - Preview environments for new PRs.
20 - Test on production-like data.
21 - Storefront deployment.
22 - Emails setup.
23 - [And more](!cloud!#cloud-features).
24
25 </Note>
26
27 <Prerequisites items={[
28 {
29 text: "Medusa application’s codebase hosted on GitHub repository.",
30 link: "/learn"
31 },
32 ]} />
33
34 ## What You'll Deploy
35
36 When you deploy the Medusa application, you need to deploy the following resources:
37
38 1. PostgreSQL database: This is the database that will hold your Medusa application's details.
39 2. Redis database: This is the database that will store the Medusa server's session.
40 3. Medusa application in [server and worker mode](../../production/worker-mode/page.mdx), where:
41 - The server mode handles incoming API requests and serving the Medusa Admin dashboard.
42 - The worker mode handles background tasks, such as scheduled jobs and subscribers.
43
44 So, when choosing a hosting provider, make sure it supports deploying these resources. Also, for optimal experience, the hosting provider and plan must offer at least 2GB of RAM.
45
46 ---
47
48 ## 1. Configure Medusa Application
49
50 ### Worker Mode
51
52 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.
53
54 Learn more about worker mode in the [Worker Module chapter](../../production/worker-mode/page.mdx).
55
56 So, add the following configuration in `medusa-config.ts`:
57
58 ```ts title="medusa-config.ts"
59 module.exports = defineConfig({
60 projectConfig: {
61 // ...
62 workerMode: process.env.MEDUSA_WORKER_MODE as "shared" | "worker" | "server",
63 },
64 })
65 ```
66
67 Later, you’ll set different values of the `MEDUSA_WORKER_MODE` environment variable for each Medusa application deployment or instance.
68
69 ### Configure Medusa Admin
70
71 The Medusa Admin is served by the Medusa server application. So, you need to disable it in the worker Medusa application only.
72
73 To disable the Medusa Admin in the worker Medusa application while keeping it enabled in the server Medusa application, add the following configuration in `medusa-config.ts`:
74
75 ```ts title="medusa-config.ts"
76 module.exports = defineConfig({
77 // ...
78 admin: {
79 disable: process.env.DISABLE_MEDUSA_ADMIN === "true",
80 },
81 })
82 ```
83
84 Later, you’ll set different values of the `DISABLE_MEDUSA_ADMIN` environment variable for each Medusa application instance.
85
86 ### Configure Redis URL
87
88 The `redisUrl` configuration specifies the connection URL to Redis to store the Medusa server's session.
89
90 <Note>
91
92 Learn more in the [Medusa Configuration documentation](../../configurations/medusa-config/page.mdx#redisurl).
93
94 </Note>
95
96 So, add the following configuration in `medusa-config.ts` :
97
98 ```ts title="medusa-config.ts"
99 module.exports = defineConfig({
100 projectConfig: {
101 // ...
102 redisUrl: process.env.REDIS_URL,
103 },
104 })
105 ```
106
107 ---
108
109 ## 2. Add predeploy Script
110
111 Before you start the Medusa application in production, you should always run migrations and sync links.
112
113 So, add the following script in `package.json`:
114
115 ```json
116 "scripts": {
117 // ...
118 "predeploy": "medusa db:migrate"
119 },
120 ```
121
122 ---
123
124 ## 3. Install Production Modules and Providers
125
126 By default, your Medusa application uses modules and providers useful for development, such as the Local File Module Provider.
127
128 It’s highly recommended to instead use modules and providers suitable for production, including:
129
130 - [Redis Caching Module](!resources!/infrastructure-modules/caching/providers/redis)
131 - [Redis Event Bus Module](!resources!/infrastructure-modules/event/redis)
132 - [Workflow Engine Redis Module](!resources!/infrastructure-modules/workflow-engine/redis)
133 - [Redis Locking Module Provider](!resources!/infrastructure-modules/locking/redis)
134 - [PostHog Analytics Module Provider](!resources!/infrastructure-modules/analytics/posthog) (If you're using analytics in your application. You can also use other analytics module providers that are production-ready).
135 - [S3 File Module Provider](!resources!/infrastructure-modules/file/s3) (or other file module providers that are production-ready).
136 - [SendGrid Notification Module Provider](!resources!/infrastructure-modules/notification/sendgrid) (or other notification module providers that are production-ready).
137
138 <Note>
139
140 The Caching Module was introduced in [Medusa v2.11.0](https://github.com/medusajs/medusa/releases/tag/v2.11.0) to replace the deprecated Cache Module.
141
142 </Note>
143
144 Then, add these modules in `medusa-config.ts`:
145
146 ```ts title="medusa-config.ts"
147 import { Modules } from "@medusajs/framework/utils"
148
149 module.exports = defineConfig({
150 // ...
151 modules: [
152 {
153 resolve: "@medusajs/medusa/caching",
154 options: {
155 providers: [
156 {
157 resolve: "@medusajs/caching-redis",
158 id: "caching-redis",
159 is_default: true,
160 options: {
161 redisUrl: process.env.CACHE_REDIS_URL,
162 },
163 },
164 ],
165 },
166 },
167 {
168 resolve: "@medusajs/medusa/event-bus-redis",
169 options: {
170 redisUrl: process.env.REDIS_URL,
171 },
172 },
173 {
174 resolve: "@medusajs/medusa/workflow-engine-redis",
175 options: {
176 redis: {
177 // Note: This was `url` before v2.12.2
178 // It's now deprecated in favor of `redisUrl`
179 redisUrl: process.env.REDIS_URL,
180 },
181 },
182 },
183 {
184 resolve: "@medusajs/medusa/locking",
185 options: {
186 providers: [
187 {
188 resolve: "@medusajs/medusa/locking-redis",
189 id: "locking-redis",
190 is_default: true,
191 options: {
192 redisUrl: process.env.LOCKING_REDIS_URL,
193 },
194 },
195 ],
196 },
197 },
198
199 ],
200 })
201 ```
202
203 <Note title="Tip">
204
205 Check out the [Integrations](!resources!/integrations) and [Infrastructure Modules](!resources!/infrastructure-modules) documentation for other modules and providers to use.
206
207 </Note>
208
209 ---
210
211 ## 4. Create PostgreSQL and Redis Databases
212
213 Your Medusa application must connect to PostgreSQL and Redis databases. So, before you deploy it, create production PostgreSQL and Redis databases.
214
215 If your hosting provider doesn't support databases, you can use [Neon for PostgreSQL database hosting](https://neon.tech/), and [Redis Cloud for the Redis database hosting](https://redis.io/cloud/).
216
217 After hosting both databases, keep their connection URLs for the next steps.
218
219 ---
220
221 ## 5. Deploy Medusa Application in Server Mode
222
223 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.
224
225 The deployment steps depend on your hosting provider. This section provides the general steps to perform during the deployment.
226
227 ### Set Environment Variables
228
229 When setting the environment variables of the Medusa application, set the following variables:
230
231 ```bash
232 COOKIE_SECRET=supersecret # TODO GENERATE SECURE SECRET
233 JWT_SECRET=supersecret # TODO GENERATE SECURE SECRET
234 STORE_CORS= # STOREFRONT URL
235 ADMIN_CORS= # ADMIN URL
236 AUTH_CORS= # STOREFRONT AND ADMIN URLS, SEPARATED BY COMMAS
237 DISABLE_MEDUSA_ADMIN=false
238 MEDUSA_WORKER_MODE=server
239 PORT=9000
240 DATABASE_URL= # POSTGRES DATABASE URL
241 REDIS_URL= # REDIS DATABASE URL
242 ```
243
244 Where:
245
246 - The value of `COOKIE_SECRET` and `JWT_SECRET` must be a randomly generated secret. These are required in production. If not set, an error is thrown when starting the Medusa application.
247 - `STORE_CORS`'s value is the URL of your storefront. If you don’t have it yet, you can skip adding it for now.
248 - `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.
249 - `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.
250 - Set `DISABLE_MEDUSA_ADMIN`'s value to `false` so that the admin is built with the server application.
251 - Set the PostgreSQL database's connection URL as the value of `DATABASE_URL`
252 - Set the Redis database's connection URL as the value of `REDIS_URL`.
253
254 Feel free to add any other relevant environment variables, such as for integrations and Infrastructure Modules. If you're using environment variables in your admin customizations, make sure to set them as well, as they're inlined during the build process.
255
256 ### Set Start Command
257
258 The Medusa application's production build, which is created using the `build` command, outputs the Medusa application to `.medusa/server`. So, you must install the dependencies in the `.medusa/server` directory, then run the `start` command in it.
259
260 If your hosting provider doesn't support setting a current-working directory, set the start command to the following:
261
262 ```bash npm2yarn
263 cd .medusa/server && npm install && npm run predeploy && npm run start
264 ```
265
266 Notice that you run the `predeploy` command before starting the Medusa application to run migrations and sync links whenever there's an update.
267
268 ### Set Backend URL in Admin Configuration
269
270 The Medusa Admin is built and hosted statically. To send requests to the Medusa server application, you must set the backend URL in the Medusa Admin's configuration.
271
272 After you’ve obtained the Medusa application’s URL, add the following configuration to `medusa-config.ts`:
273
274 ```ts title="medusa-config.ts"
275 module.exports = defineConfig({
276 // ...
277 admin: {
278 // ...
279 backendUrl: process.env.MEDUSA_BACKEND_URL,
280 },
281 })
282 ```
283
284 Then, push the changes to the GitHub repository or deployed application.
285
286 In your hosting provider, add or modify the following environment variables for the Medusa application in server mode:
287
288 ```bash
289 ADMIN_CORS= # MEDUSA APPLICATION URL
290 AUTH_CORS= # ADD MEDUSA APPLICATION URL
291 MEDUSA_BACKEND_URL= # URL TO DEPLOYED MEDUSA APPLICATION
292 ```
293
294 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`.
295
296 After setting the environment variables, make sure to restart the deployment for the changes to take effect.
297
298 <Note title="Tip">
299
300 Remember to separate URLs in `AUTH_CORS` by commas.
301
302 </Note>
303
304 ---
305
306 ## 6. Deploy Medusa Application in Worker Mode
307
308 Next, you'll deploy the Medusa application in worker mode.
309
310 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.
311
312 ### Set Environment Variables
313
314 When setting the environment variables of the Medusa application, set the following variables:
315
316 ```bash
317 COOKIE_SECRET=supersecret # TODO GENERATE SECURE SECRET
318 JWT_SECRET=supersecret # TODO GENERATE SECURE SECRET
319 DISABLE_MEDUSA_ADMIN=true
320 MEDUSA_WORKER_MODE=worker
321 PORT=9000
322 DATABASE_URL= # POSTGRES DATABASE URL
323 REDIS_URL= # REDIS DATABASE URL
324 ```
325
326 Where:
327
328 - The value of `COOKIE_SECRET` and `JWT_SECRET` must be a randomly generated secret. These are required in production. If not set, an error is thrown when starting the Medusa application.
329 - Set `DISABLE_MEDUSA_ADMIN`'s value to `true` so that the admin isn't built with the worker application.
330 - Set the PostgreSQL database's connection URL as the value of `DATABASE_URL`
331 - Set the Redis database's connection URL as the value of `REDIS_URL`.
332
333 Feel free to add any other relevant environment variables, such as for integrations and Infrastructure Modules.
334
335 ### Set Start Command
336
337 The Medusa application's production build, which is created using the `build` command, outputs the Medusa application to `.medusa/server`. So, you must install the dependencies in the `.medusa/server` directory, then run the `start` command in it.
338
339 If your hosting provider doesn't support setting a current-working directory, set the start command to the following:
340
341 ```bash npm2yarn
342 cd .medusa/server && npm install && npm run start
343 ```
344
345 ---
346
347 ## 7. Test Deployed Application
348
349 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.
350
351 The Medusa Admin is also available at `<APP_URL>/app`.
352
353 ---
354
355 ## Create Admin User
356
357 If your hosting provider supports running commands in your Medusa application's directory, run the following command to create an admin user:
358
359 ```bash npx2yarn
360 npx medusa user -e admin-medusa@test.com -p supersecret
361 ```
362
363 Replace the email `admin-medusa@test.com` and password `supersecret` with the credentials you want.
364
365 You can use these credentials to log into the Medusa Admin dashboard.