Deploy Your Medusa Backend to Railway
In this document, you’ll learn how to deploy your Medusa backend to Railway.
What is Railway
Railway is a hosting provider that you can use to deploy web applications and databases without having to worry about managing the full infrastructure.
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.
- Medusa backend installed locally
- Production modules installed on the Medusa backend, such as Redis Event Module and Redis Cache Module.
- A Railway 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.
Configure the Admin
You have two options to deploy the Medusa Admin: either with the backend or separately.
Deploying with the Backend
To deploy the admin with the backend:
- Your chosen plan must offer at least 2GB of RAM.
- Enable the autoRebuild option of the admin plugin:
Alternatively, you can use a GitHub action to build the admin as explained here.
Deploying Separately
If you choose to deploy the admin separately, disable the admin plugin's serve option:
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
.
Also, change the build
command to remove the command that builds the admin:
Refer to the admin deployment guides on how to deploy the admin separately.
Add Railway Configuration File
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.
Add in the root of your Medusa project the file railway.toml
with the content based on the package manager of your choice:
Add Worker Mode Configuration
Learn more about the Worker Mode in this guide.
Set the worker_mode
configuration in your medusa-config.js
, if you haven't already:
This allows you to switch between modes for different deployed Medusa instances based on the MEDUSA_WORKER_MODE
environment variable.
Push Changes to GitHub Repo
Before proceeding further, push all changes you've made to your GitHub repository so that it's included in the deployment process.
Create Project + PostgreSQL Database
On the Railway dashboard:
- Click on the ”New Project” button.
- Choose from the list the ”Provision PostgreSQL” option.
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
In the same project view:
- Click on the New button.
- Choose the Database option.
- 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.
Module Services and Variables
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:
- Click on the New button.
- Choose the ”GitHub Repo” option.
- If you still haven't given GitHub permissions to Railway, choose the ”Configure GitHub App” option to do that.
- Choose the repository from the GitHub Repo dropdown.
Configure Backend Environment Variables
To configure the environment variables of your Medusa backend:
- Click on the GitHub repository’s card.
- Choose the Variables tab.
- Add the following environment variables:
Notice that the values of DATABASE_URL
and REDIS_URL
reference the values from the PostgreSQL and Redis databases you created.
It’s 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
The start command is the command used to run the backend. You’ll 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:
- Click on the GitHub repository’s card.
- Click on the Settings tab and scroll down to the Deploy section.
- Click on the Start Command button.
- Paste the following in the shown input:
Deploy Medusa in Worker Mode
You'll now create another Medusa instance that'll be in worker
runtime mode.
In the same project view:
- Click on the New button.
- Choose the ”GitHub Repo” option.
- Choose the same repository from the GitHub Repo dropdown.
Configure Environment Variables for Worker Mode
To configure the environment variables of the Medusa instance running in worker mode:
- Click on the card of the Medusa instance you just created..
- Choose the Variables tab.
- Add the following environment variables:
Notice that the values of DATABASE_URL
and REDIS_URL
reference the values from the PostgreSQL and Redis databases you created.
It’s 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
The start command is the command used to run the Medusa instance in worker mode. To set it:
- Click on the worker’s card.
- Click on the Settings tab and scroll down to the Deploy section.
- Click on the Start Command button.
- Paste the following in the shown input:
Add Domain Name
The last step is to add a domain name to your Medusa backend. To do that:
- Click on the Medusa server runtime's card.
- Click on the Settings tab and scroll down to the Environment section.
- Either click on the Custom Domain button to enter your own domain or the Generate Domain button to generate a random domain.
Deploy Changes
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.
Click on the button to trigger the deployment. The deployment will take a few minutes before it's ready.
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.
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.
Health Route
Access <YOUR_APP_URL>/health
to get health status of your deployed backend.
Testing the Admin
If you deployed the admin dashboard with the backend, you can test it by going to <YOUR_APP_URL>/app
. If you changed the admin path, make sure to change /app
to the path you've set.
Troubleshooting
If you run into any issues or a problem with your deployed backend, you can check the logs in your Railway container instance by:
- Click on the GitHub repository’s card.
- Click on the Deployments tab.
- Click on the View Logs button.
Error: connect ENOENT
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
The following error can occur when running the build
command either locally or in an deployed Medusa backend:
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:
Run Commands on the Deployed Services
Railway’s CLI tool allows you to run commands locally, but using environment variables from your projects.
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
To create an admin user in your deployed backend, run the following command in the root of your Medusa project: