Files
medusa-store/www/docs/content/deployments/server/deploying-on-railway.md
Shahed Nasser 914d773d3a api-ref: custom API reference (#4770)
* initialized next.js project

* finished markdown sections

* added operation schema component

* change page metadata

* eslint fixes

* fixes related to deployment

* added response schema

* resolve max stack issue

* support for different property types

* added support for property types

* added loading for components

* added more loading

* type fixes

* added oneOf type

* removed console

* fix replace with push

* refactored everything

* use static content for description

* fixes and improvements

* added code examples section

* fix path name

* optimizations

* fixed tag navigation

* add support for admin and store references

* general enhancements

* optimizations and fixes

* fixes and enhancements

* added search bar

* loading enhancements

* added loading

* added code blocks

* added margin top

* add empty response text

* fixed oneOf parameters

* added path and query parameters

* general fixes

* added base path env variable

* small fix for arrays

* enhancements

* design enhancements

* general enhancements

* fix isRequired

* added enum values

* enhancements

* general fixes

* general fixes

* changed oas generation script

* additions to the introduction section

* added copy button for code + other enhancements

* fix response code block

* fix metadata

* formatted store introduction

* move sidebar logic to Tags component

* added test env variables

* fix code block bug

* added loading animation

* added expand param + loading

* enhance operation loading

* made responsive + improvements

* added loading provider

* fixed loading

* adjustments for small devices

* added sidebar label for endpoints

* added feedback component

* fixed analytics

* general fixes

* listen to scroll for other headings

* added sample env file

* update api ref files + support new fields

* fix for external docs link

* added new sections

* fix last item in sidebar not showing

* move docs content to www/docs

* change redirect url

* revert change

* resolve build errors

* configure rewrites

* changed to environment variable url

* revert changing environment variable name

* add environment variable for API path

* fix links

* fix tailwind settings

* remove vercel file

* reconfigured api route

* move api page under api

* fix page metadata

* fix external link in navigation bar

* update api spec

* updated api specs

* fixed google lint error

* add max-height on request samples

* add padding before loading

* fix for one of name

* fix undefined types

* general fixes

* remove response schema example

* redesigned navigation bar

* redesigned sidebar

* fixed up paddings

* added feedback component + report issue

* fixed up typography, padding, and general styling

* redesigned code blocks

* optimization

* added error timeout

* fixes

* added indexing with algolia + fixes

* fix errors with algolia script

* redesign operation sections

* fix heading scroll

* design fixes

* fix padding

* fix padding + scroll issues

* fix scroll issues

* improve scroll performance

* fixes for safari

* optimization and fixes

* fixes to docs + details animation

* padding fixes for code block

* added tab animation

* fixed incorrect link

* added selection styling

* fix lint errors

* redesigned details component

* added detailed feedback form

* api reference fixes

* fix tabs

* upgrade + fixes

* updated documentation links

* optimizations to sidebar items

* fix spacing in sidebar item

* optimizations and fixes

* fix endpoint path styling

* remove margin

* final fixes

* change margin on small devices

* generated OAS

* fixes for mobile

* added feedback modal

* optimize dark mode button

* fixed color mode useeffect

* minimize dom size

* use new style system

* radius and spacing design system

* design fixes

* fix eslint errors

* added meta files

* change cron schedule

* fix docusaurus configurations

* added operating system to feedback data

* change content directory name

* fixes to contribution guidelines

* revert renaming content

* added api-reference to documentation workflow

* fixes for search

* added dark mode + fixes

* oas fixes

* handle bugs

* added code examples for clients

* changed tooltip text

* change authentication to card

* change page title based on selected section

* redesigned mobile navbar

* fix icon colors

* fix key colors

* fix medusa-js installation command

* change external regex in algolia

* change changeset

* fix padding on mobile

* fix hydration error

* update depedencies
2023-08-15 18:07:54 +03:00

9.1 KiB
Raw Blame History

description, addHowToData
description addHowToData
Learn step-by-step. true

Deploy Your Medusa Backend to Railway

In this document, youll 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 plan 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:

Deploy with Railway


Prerequisites

Medusa Backend

It is assumed that you already have a Medusa backend installed locally. If you dont, please follow the quickstart guide.

Furthermore, your Medusa backend should be configured to work with PostgreSQL and Redis. You can follow the Configure your Backend documentation 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:

Needed Accounts

  • Railway account.
  • GitHub account to create a repository to host your backend's codebase.

Required Tools


(Optional) Step 1: Add Nixpacks Configurations

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.

In the root of your Medusa project, add the nixpacks.toml file with the following content:

[phases.setup]
nixPkgs = ['nodejs', 'yarn']

[phases.install]
cmds=['yarn install']

Step 2: Create GitHub Repository

Before you can 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:

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:

git add .
git commit -m "initial commit"
git push

After pushing the changes, you can find the files in your GitHub repository.


Step 3: Deploy to Railway

In this section, youll create the PostgreSQL and Redis databases first, then deploy the backend from the GitHub repository.

Create the PostgreSQL Database

On the Railway dashboard:

  1. Click on the ”New Project**”** button.
  2. Choose from the dropdown 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.

Create the Redis Database

In the same project view:

  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.

Deploy the Medusa Backend Repository

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.
  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 Environment Variables

To configure the environment variables of your Medusa backend:

  1. Click on the GitHub repositorys card.
  2. Choose the Variables tab.
  3. Add the following environment variables:
PORT=9000
JWT_SECRET=something
COOKIE_SECRET=something
DATABASE_URL=${{Postgres.DATABASE_URL}}
REDIS_URL=${{Redis.REDIS_URL}}
DATABASE_TYPE=postgres

Notice that the values of DATABASE_URL and REDIS_URL reference the values from the PostgreSQL and Redis databases you created.

:::warning

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 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.

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:
medusa migrations run && medusa start

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.
  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 4: Test the Backend

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.

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.

:::tip

If you generated a random domain, you can find it by clicking on the GitHub repositorys card → Deployment tab.

:::

Health Route

You can access /health to get health status of your deployed backend.

Testing the Admin

:::note

Make sure to either set the autoRebuild option of the admin plugin to true or add its build command as part of the start command of your backend.

:::

If you deployed the admin dashboard alongside 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:

  1. Click on the GitHub repositorys card.
  2. Click on the Deployments tab.
  3. 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.


Run Commands on the Backend

To run commands on your backend, you can use Railways CLI tool to run a local shell and execute commands.

For example, you can run commands on the backend to seed the database or create a new user using Medusas CLI tool.

Create Admin User

You can create an admin user by running the following command in the root of your Medusa project:

railway run npx medusa user --email admin@medusa-test.com --password supersecret

See Also