Skip to main content

Deploy Your Medusa Server to Railway

In this document, you’ll learn how to deploy your Medusa server 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 server along with PostgreSQL and Redis databases. This is useful mainly for development and demo purposes.


Prerequisites

Medusa Server

It is assumed that you already have a Medusa server installed locally. If you don’t, please follow the quickstart guide.

Furthermore, your Medusa server should be configured to work with PostgreSQL and Redis. You can follow the Configure your Server documentation to learn how to do that.

Needed Accounts

  • Railway account.
  • GitHub account to create a repository to host your server’s codebase.

Required Tools


Changes to Medusa Server Codebase

By default, Railway looks for a Dockerfile in the root of the codebase. If there is one, it will try to deploy your server using it.

As this guide doesn't use Docker, make sure to delete Dockerfile from the root of your Medusa server.


Create GitHub Repository

Before you can deploy your Medusa server 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.

Click plus then choose new repository from dropdown

You’ll then be redirected to a new page with a form. In the form, enter the Repository Name then scroll down and click Create repository.

New repository form

Push Code to GitHub Repository

The next step is to push the code to the GitHub repository you just created.

After creating the repository, you’ll be redirected to the repository’s page. On that page, you should see a URL that you can copy to connect your repository to a local directory.

GitHub repository's URL

Copy the link. Then, open your terminal in the directory that holds your Medusa server codebase and run the following commands:

git init
git remote add origin <GITHUB_URL>
Report Incorrect CodeReport Incorrect CodeCopy to ClipboardCopy to Clipboard

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 origin master
Report Incorrect CodeReport Incorrect CodeCopy to ClipboardCopy to Clipboard

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


Deploy to Railway

In this section, you’ll create the PostgreSQL and Redis databases first, then deploy the server 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.

To find the PostgreSQL database URL which you’ll need later:

  1. Click on the PostgreSQL card.
  2. Choose the Connect tab.
  3. Copy the Postgres Connection URL.

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.

To find the Redis database URL which you’ll need later:

  1. Click on the Redis card.
  2. Choose the Connect tab.
  3. Copy the Redis Connection URL.

Deploy the Medusa Server Repository

In the same project view:

  1. Click on the New button.
  2. Choose the ”GitHub Repo” option.
  3. Choose the ”Configure GitHub App” option to give Railway permission to read and pull your code from GitHub.
  4. Choose the repository from the GitHub Repo dropdown.

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 server a few minutes to deploy successfully.

Configure Environment Variables

To configure the environment variables of your Medusa server:

  1. Click on the GitHub repository’s card.
  2. Choose the Variables tab.
  3. Add the following environment variables:
PORT=9000
JWT_SECRET=something
COOKIE_SECRET=something
DATABASE_URL=<YOUR_DATABASE_URL>
REDIS_URL=<YOUR_REDIS_URL>
Report Incorrect CodeReport Incorrect CodeCopy to ClipboardCopy to Clipboard

Where <YOUR_DATABASE_URL> and <YOUR_REDIS_URL> are the URLs you copied earlier when you created the PostgreSQL and Redis databases respectively.

It’s highly recommended to use strong, randomly generated secrets for JWT_SECRET and ****COOKIE_SECRET.

Change Start Command

The start command is the command used to run the server. You’ll change it to run any available migrations, then run the Medusa server. This way if you create your own migrations or update the Medusa server, it's guaranteed that these migrations are run first before the server starts.

To change the start command of your Medusa server:

  1. Click on the GitHub repository’s card.
  2. Click on the Settings tab and scroll down to the Service section.
  3. Paste the following in the Start Command field:
medusa migrations run && medusa develop
Report Incorrect CodeReport Incorrect CodeCopy to ClipboardCopy to Clipboard

Add Domain Name

The last step is to add a domain name to your Medusa server. To do that:

  1. Click on the GitHub repository’s card.
  2. Click on the Settings tab and scroll down to the Domains section.
  3. Either click on the Custom Domain button to enter your own domain or the Generate Domain button to generate a random domain.

Test your Server

Every change you make to the settings redeploys the server. You can check the Deployments of the server by clicking on the GitHub repository’s card and choosing the Deployments tab.

After the server 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 server.

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


Troubleshooting

If you run into any issues or a problem with your deployed server, you can check the logs in your Railway container instance by:

  1. Click on the GitHub repository’s card.
  2. Click on the Deployments tab.
  3. Click on the View Logs button.

Run Commands on Server

To run commands on your server, you can use Railway’s CLI tool to run a local shell and execute commands.

For example, you can run commands on the server to seed the database or create a new user using Medusa’s CLI tool.


See Also