docs: improvements to railway deployment guide + new troubleshooting guide (#7012)

This commit is contained in:
Shahed Nasser
2024-04-15 09:30:31 +02:00
committed by GitHub
parent 47a175ce94
commit 793184bf42
6 changed files with 145 additions and 136 deletions
@@ -3,6 +3,10 @@ description: 'Learn step-by-step.'
addHowToData: true
---
import Tabs from "@theme/Tabs"
import TabItem from "@theme/TabItem"
import MimeErrorTroubleshooting from "../../troubleshooting/mime-error.md"
# Deploy Your Medusa Backend to Railway
In this document, youll learn how to deploy your Medusa backend to Railway.
@@ -13,50 +17,23 @@ In this document, youll learn how to deploy your Medusa backend to Railway.
Railway provides a free trial (no credit card required) that allows you to deploy your Medusa backend along with PostgreSQL and Redis databases. This is useful mainly for development and demo purposes.
:::note
If you're deploying the admin plugin along with the backend, you'll need at least the Developer plan. The resources provided by the starter plan will cause memory errors.
:::
If you also don't have a Medusa project, you can deploy to Railway instantly with this button:
<a
href="https://railway.app/template/zC7eOq?referralCode=TW4Qi0" className="img-url no-zoom-img">
href="https://railway.app/template/zC7eOq?referralCode=TW4Qi0" className="img-url no-zoom-img mb-2 inline-block">
<img src="https://railway.app/button.svg" alt="Deploy with Railway" className="no-zoom-img"/>
</a>
---
<Note type="check">
## Prerequisites
- [Medusa backend installed locally](../../create-medusa-app.mdx)
- Production modules installed on the Medusa backend, such as [Redis Event Module](../../development/events/modules/redis.md) and [Redis Cache Module](../../development/cache/modules/redis.md).
- A [Railway](https://railway.app/) account. If you're deploying the admin plugin along with the backend, you'll need at least the Developer plan. The resources provided by the starter plan lead to memory errors.
- [A GitHub repository for your Medusa backend](https://docs.github.com/en/repositories/creating-and-managing-repositories/creating-a-new-repository).
### Medusa Backend
</Note>
It is assumed that you already have a Medusa backend installed locally. If you dont, please follow the [quickstart guide](../../development/backend/install.mdx).
## Configure the Admin
Furthermore, your Medusa backend should be configured to work with PostgreSQL and Redis. You can follow the [Configure your Backend documentation](./../../references/medusa_config/interfaces/medusa_config.ConfigModule.mdx) to learn how to do that.
### Production Modules
If you're using development modules for events and caching, it's highly recommended to install production modules instead. These can be:
- [Redis Event Module](../../development/events/modules/redis.md)
- [Redis Cache Module](../../development/cache/modules/redis.md)
### Needed Accounts
- A [Railway](https://railway.app/) account.
- A [GitHub](https://github.com/) account to create a repository to host your backend's codebase.
### Required Tools
- Gits CLI tool. You can follow [this documentation to learn how to install it for your operating system](./../../development/backend/prepare-environment.mdx#git).
---
## (Optional) Step 0: Configure the Admin
If you're using the Medusa Admin plugin, you have two options to deploy it: either with the backend or separately.
You have two options to deploy the Medusa Admin: either with the backend or separately.
### Deploying with the Backend
@@ -104,7 +81,14 @@ const plugins = [
This ensures that the admin isn't built or served in production. You can also change `@medusajs/admin` dependency to be a dev dependency in `package.json`.
You can alternatively remove the admin plugin for the plugins array.
Also, change the `build` command to remove the command that builds the admin:
```json
"scripts": {
// ...
"build": "cross-env npm run clean && npm run build:server",
}
```
:::tip
@@ -114,23 +98,46 @@ Refer to the [admin deployment guides on how to deploy the admin separately](../
---
## (Optional) Step 1: Add Nixpacks Configurations
## Add Railway Configuration File
If you've created your project using `create-medusa-app`, you might receive errors during the deployment process as Railway uses NPM by default. To avoid that, you need to configure Nixpacks to either use `yarn` or add the `--legacy-peer-deps` option to `npm install`.
To avoid errors during the installation process, it's recommended to use `yarn` for installing the dependencies. Alternatively, pass the `--legacy-peer-deps` option to the npm command.
In the root of your Medusa project, add the `nixpacks.toml` file with the following content:
Add in the root of your Medusa project the file `railway.toml` with the content based on the package manager of your choice:
```toml
[phases.setup]
nixPkgs = ['nodejs', 'yarn']
<Tabs groupId="railway-config" isCodeTabs={true} codeTitle="railway.toml">
<TabItem label="yarn" value="yarn">
[phases.install]
cmds=['yarn install']
```
```toml
[build]
builder = "NIXPACKS"
[build.nixpacksPlan.phases.setup]
nixPkgs = ["nodejs", "yarn"]
[build.nixpacksPlan.phases.install]
cmds=["yarn install"]
```
</TabItem>
<TabItem label="npm" value="npm">
```toml
[build]
builder = "NIXPACKS"
[build.nixpacksPlan.phases.setup]
nixPkgs = ["nodejs", "npm"]
[build.nixpacksPlan.phases.install]
cmds=["npm install --legacy-peer-deps"]
```
</TabItem>
</Tabs>
---
## Step 2: Add Worker Mode Configuration
## Add Worker Mode Configuration
:::note
@@ -152,88 +159,53 @@ This allows you to switch between modes for different deployed Medusa instances
---
## Step 3: Create GitHub Repository
## Push Changes to GitHub Repo
Before you deploy your Medusa backend you need to create a GitHub repository and push the code base to it.
On GitHub, click the plus icon at the top right, then click New Repository.
Youll then be redirected to a new page with a form. In the form, enter the Repository Name then scroll down and click Create repository.
### Push Code to GitHub Repository
The next step is to push the code to the GitHub repository you just created.
After creating the repository, youll be redirected to the repositorys page. On that page, you should see a URL that you can copy to connect your repository to a local directory.
Copy the link. Then, open your terminal in the directory that holds your Medusa backend codebase and run the following commands:
```bash
git init
git remote add origin <GITHUB_URL>
```
Where `<GITHUB_URL>` is the URL you just copied.
Then, add, commit, and push the changes into the repository:
```bash
git add .
git commit -m "initial commit"
git push
```
After pushing the changes, you can find the files in your GitHub repository.
Before proceeding further, push all changes you've made to your GitHub repository so that it's included in the deployment process.
---
## Step 4: Deploy to Railway
In this section, youll create the PostgreSQL and Redis databases first, then deploy two instances of the Medusa backend: one having a `server` runtime mode, and another having a `worker` runtime mode.
### Create the PostgreSQL Database
## Create Project + PostgreSQL Database
On the Railway dashboard:
1. Click on the ”New Project**”** button.
2. Choose from the dropdown the ”Provision PostgreSQL” option.
1. Click on the ”New Projectbutton.
2. Choose from the list the ”Provision PostgreSQL” option.
A new database will be created and, after a few seconds, you will be redirected to the project page where you will see the newly-created database.
A new database will be created and, after a few seconds, you'll be redirected to the project page where you'll see the newly-created database.
### Create the Redis Database
---
## Create the Redis Database
In the same project view:
1. Click on the New button.
2. Choose the Database option.
3. Choose Add Redis.
1. Click on the New button.
2. Choose the Database option.
3. Choose Add Redis.
A new Redis database will be added to the project view in a few seconds. Click on it to open the database sidebar.
### Note about Modules
---
If you use modules that require setting up other resources, make sure to add them at this point. This guide does not cover configurations specific to a module.
## Module Services and Variables
### Deploy Medusa in Server Mode
If you use modules that require setting up other services or environment variables, make sure to add them at this point. This guide doesn't cover configurations specific to a module.
---
## Deploy Medusa in Server Mode
In this section, you'll create a Medusa backend instance running in `server` runtime mode.
In the same project view:
1. Click on the New button.
2. Choose the ”GitHub Repo” option.
3. If you still haven't given GitHub permissions to Railway, choose the ”Configure GitHub App” option to do that.
1. Click on the New button.
2. Choose the ”GitHub Repo” option.
3. If you still haven't given GitHub permissions to Railway, choose the ”Configure GitHub App” option to do that.
4. Choose the repository from the GitHub Repo dropdown.
:::tip
If the GitHub repositories in the dropdown are stuck on loading and aren't showing, you might need to delete the project and start over. This only happens before you configure your account with GitHub.
:::
It will take the backend a few minutes for the deployment to finish. It may fail since you haven't added the environment variables yet.
#### Configure Backend Environment Variables
### Configure Backend Environment Variables
To configure the environment variables of your Medusa backend:
@@ -247,7 +219,6 @@ JWT_SECRET=something
COOKIE_SECRET=something
DATABASE_URL=${{Postgres.DATABASE_URL}}
REDIS_URL=${{Redis.REDIS_URL}}
DATABASE_TYPE=postgres
MEDUSA_WORKER_MODE=server
```
@@ -255,39 +226,40 @@ Notice that the values of `DATABASE_URL` and `REDIS_URL` reference the values fr
:::warning
Its highly recommended to use strong, randomly generated secrets for `JWT_SECRET` and `COOKIE_SECRET`.
Its highly recommended to use strong, randomly generated secrets for `JWT_SECRET` and `COOKIE_SECRET`.
:::
Make sure to add any other environment variables that are relevant for you here. For example, you can add environment variables related to Medusa Admin or your modules.
#### Change Backend's Start Command
### Change Backend's Start Command
The start command is the command used to run the backend. Youll change it to run any available migrations, then run the Medusa backend. This way if you create your own migrations or update the Medusa backend, it's guaranteed that these migrations are run first before the backend starts.
The start command is the command used to run the backend. Youll change it to run any available migrations, then run the Medusa backend. This way if you create your own migrations or update the Medusa backend, it's guaranteed that these migrations run first before the backend starts.
To change the start command of your Medusa backend:
1. Click on the GitHub repositorys card.
2. Click on the Settings tab and scroll down to the Deploy section.
3. Paste the following in the Start Command field:
3. Click on the Start Command button.
4. Paste the following in the shown input:
```bash
medusa migrations run && medusa start
```
### Deploy Medusa in Worker Mode
---
## Deploy Medusa in Worker Mode
You'll now create another Medusa instance that'll be in `worker` runtime mode.
In the same project view:
1. Click on the New button.
2. Choose the ”GitHub Repo” option.
1. Click on the New button.
2. Choose the ”GitHub Repo” option.
3. Choose the same repository from the GitHub Repo dropdown.
It will take the worker backend a few minutes for the deployment to finish. It may fail since you haven't added the environment variables yet.
#### Configure Environment Variables for Worker Mode
### Configure Environment Variables for Worker Mode
To configure the environment variables of the Medusa instance running in worker mode:
@@ -301,7 +273,6 @@ JWT_SECRET=something
COOKIE_SECRET=something
DATABASE_URL=${{Postgres.DATABASE_URL}}
REDIS_URL=${{Redis.REDIS_URL}}
DATABASE_TYPE=postgres
MEDUSA_WORKER_MODE=worker
```
@@ -309,49 +280,60 @@ Notice that the values of `DATABASE_URL` and `REDIS_URL` reference the values fr
:::warning
Its highly recommended to use strong, randomly generated secrets for `JWT_SECRET` and `COOKIE_SECRET`.
Its highly recommended to use strong, randomly generated secrets for `JWT_SECRET` and `COOKIE_SECRET`.
:::
Make sure to add any other environment variables that are relevant for you here.
#### Change Worker's Start Command
### Change Worker's Start Command
The start command is the command used to run the Medusa instance in worker mode. To set it:
1. Click on the workers card.
2. Click on the Settings tab and scroll down to the Deploy section.
3. Paste the following in the Start Command field:
3. Click on the Start Command button.
4. Paste the following in the shown input:
```bash
medusa start
```
### Add Domain Name
---
## Add Domain Name
The last step is to add a domain name to your Medusa backend. To do that:
1. Click on the GitHub repositorys card.
1. Click on the Medusa server runtime's card.
2. Click on the Settings tab and scroll down to the Environment section.
3. Either click on the Custom Domain button to enter your own domain or the Generate Domain button to generate a random domain.
---
## Step 5: Test the Backend
## Deploy Changes
Every change you make to the settings redeploys the backend. You can check the Deployments of the backend by clicking on the GitHub repositorys card and choosing the Deployments tab.
At the top center of your project's dashboard page, there's a Deploy button that deploys all the changes you've made so far.
After the backend is redeployed successfully, open the app in your browser using the domain name. For example, you can open the URL `<YOUR_APP_URL>/store/products` which will return the products available on your backend.
Click on the button to trigger the deployment. The deployment will take a few minutes before it's ready.
:::tip
---
If you generated a random domain, you can find it by clicking on the GitHub repositorys card → Deployment tab.
## Test the Backend
:::
Once the deployment is finished, you can access the Medusa backend on the custom domain/domain you've generated.
For example, you can open the URL `<YOUR_APP_URL>/store/products` which returns the products available on your backend.
<Note>
Even if the deployment is successful, you might see a "Railway Not Found" error when accessing your URL. Give it a couple of minutes for the changes to actually take effect and try again.
</Note>
### Health Route
You can access `/health` to get health status of your deployed backend.
Access `<YOUR_APP_URL>/health` to get health status of your deployed backend.
### Testing the Admin
@@ -371,17 +353,21 @@ If you run into any issues or a problem with your deployed backend, you can chec
This error may be thrown by a module that uses Redis. If you see it in your build or deploy logs, make sure that your Redis configurations are correct.
### Mime Error
<MimeErrorTroubleshooting />
---
## Run Commands on the Backend
## Run Commands on the Deployed Services
To run commands on your backend, you can use [Railways CLI tool to run a local shell and execute commands](https://docs.railway.app/develop/cli#local-shell).
[Railways CLI tool](https://docs.railway.app/develop/cli#local-shell) allows you to run commands locally, but using environment variables from your projects.
For example, you can run commands on the backend to seed the database or create a new user using [Medusas CLI tool](../../cli/reference.mdx).
For example, you can use it to run Medusa commands such as running migrations or creating a user, and it'll use the database environment variables from Railway to perform the action on the production database.
### Create Admin User
You can create an admin user by running the following command in the root of your Medusa project:
To create an admin user in your deployed backend, run the following command in the root of your Medusa project:
```bash
railway run npx medusa user --email admin@medusa-test.com --password supersecret
@@ -47,6 +47,6 @@ In this case, it's best to set the `worker_mode` configuration's value to an env
:::tip
Check out the [Railway Deployment Guide](../../deployments/server/deploying-on-railway.md) as an example.
Check out the [Railway Deployment Guide](../../deployments/server/deploying-on-railway.mdx) as an example.
:::
@@ -0,0 +1,17 @@
---
title: "Mime Error"
---
The following error can occur when running the `build` command either locally or in an deployed Medusa backend:
```
Cannot find type definition file for 'mime'
```
This error typically occurs when using yarn v1 in an older Medusa setup.
To resolve the error, install v1.3.5 of `@types/mime` as a development dependency in your Medusa backend:
```bash npm2yarn
npm install @types/mime@1.3.5 --save-dev
```
+5
View File
@@ -2181,6 +2181,11 @@ module.exports = {
type: "category",
label: "Medusa Backend",
items: [
{
type: "doc",
id: "troubleshooting/mime-error",
label: "Mime Error",
},
{
type: "doc",
id: "troubleshooting/eaddrinuse",
@@ -2,7 +2,7 @@
import MDXComponents from "@theme-original/MDXComponents"
import CloudinaryImage from "@site/src/components/CloudinaryImage"
import MDXA from "./A"
import { Kbd, DetailsSummary } from "docs-ui"
import { Kbd, DetailsSummary, Note } from "docs-ui"
import H1 from "./H1"
import MDXCode from "./Code"
import MDXDetails from "./Details"
@@ -22,6 +22,7 @@ const components = {
h1: H1,
DocCard,
DocCardList,
Note,
}
export default components
@@ -43,7 +43,7 @@ export const NoteLayout = ({
{title && (
<span
className={clsx(
"txt-compact-medium-plus block mb-docs_0.125",
"text-compact-medium-plus block mb-docs_0.125",
isDefaultStyle && "text-medusa-fg-base",
isWarningStyle && "text-medusa-tag-red-text"
)}