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
Required Tools
- Git’s CLI tool. You can follow this documentation to learn how to install it for your operating system.
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.
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.
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.
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>
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
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:
- Click on the ”New Project” button.
- 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:
- Click on the PostgreSQL card.
- Choose the Connect tab.
- Copy the Postgres Connection URL.
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.
To find the Redis database URL which you’ll need later:
- Click on the Redis card.
- Choose the Connect tab.
- Copy the Redis Connection URL.
Deploy the Medusa Server Repository
In the same project view:
- Click on the New button.
- Choose the ”GitHub Repo” option.
- Choose the ”Configure GitHub App” option to give Railway permission to read and pull your code from GitHub.
- 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:
- Click on the GitHub repository’s card.
- Choose the Variables tab.
- Add the following environment variables:
PORT=9000
JWT_SECRET=something
COOKIE_SECRET=something
DATABASE_URL=<YOUR_DATABASE_URL>
REDIS_URL=<YOUR_REDIS_URL>
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:
- Click on the GitHub repository’s card.
- Click on the Settings tab and scroll down to the Service section.
- Paste the following in the Start Command field:
medusa migrations run && medusa develop
Add Domain Name
The last step is to add a domain name to your Medusa server. To do that:
- Click on the GitHub repository’s card.
- Click on the Settings tab and scroll down to the Domains section.
- 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:
- Click on the GitHub repository’s card.
- Click on the Deployments tab.
- 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.