Skip to content

Commit

Permalink
docs: added deploy to railway docs (#2697)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahednasser authored Nov 29, 2022
1 parent 657ada6 commit 663e262
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 3 deletions.
204 changes: 204 additions & 0 deletions docs/content/deployments/server/deploying-on-railway.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
---
description: 'Learn step-by-step.'
---

# Deploy Your Medusa Server to Railway

In this document, you’ll learn how to deploy your Medusa server to Railway.

## What is Railway

[Railway](https://railway.app/) 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](../../quickstart/quick-start.md).

Furthermore, your Medusa server should be configured to work with PostgreSQL and Redis. You can follow the [Configure your Server documentation](./../../usage/configurations.md) to learn how to do that.

### Needed Accounts

-[Railway](https://railway.app/) account.
-[GitHub](https:/) account to create a repository to host your server’s codebase.

### Required Tools

- Git’s CLI tool. You can follow [this documentation to learn how to install it for your operating system](./../../tutorial/0-set-up-your-development-environment.mdx#git).

## 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](https://res.cloudinary.com/dza7lstvk/image/upload/v1668001782/Medusa%20Docs/Netlify/0YlxBRi_aiywpo.png)

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](https://res.cloudinary.com/dza7lstvk/image/upload/v1668001800/Medusa%20Docs/Netlify/YPYXAF2_lypjne.png)

### 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](https://res.cloudinary.com/dza7lstvk/image/upload/v1668001818/Medusa%20Docs/Netlify/pHfSTuT_w544lr.png)

Copy the link. Then, open your terminal in the directory that holds your Medusa server 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 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:

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.

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

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

:::warning

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:

```bash
medusa migrations run && medusa develop
```

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

:::tip

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](https://docs.railway.app/develop/cli#local-shell).

For example, you can run commands on the server to seed the database or create a new user using [Medusa’s CLI tool](../../cli/reference.md).

## What’s Next

- Learn [how to deploy the Medusa Admin to Netlify](../admin/deploying-on-netlify.md).
- Learn [how to deploy the Gatsby Storefront to Netlify](../storefront/deploying-gatsby-on-netlify.md).
17 changes: 14 additions & 3 deletions www/docs/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,34 @@ module.exports = {
id: "deployments/server/deploying-on-heroku",
label: "Deploy on Heroku",
customProps: {
image: 'https://i.imgur.com/xNvxSkf.png'
image: 'https://res.cloudinary.com/dza7lstvk/image/upload/v1669739927/Medusa%20Docs/Other/xNvxSkf_l230wq.png'
}
},
{
type: "doc",
id: "deployments/server/deploying-on-digital-ocean",
label: "Deploy on DigitalOcean",
customProps: {
image: 'https://i.imgur.com/aahqJp4.png'
image: 'https://res.cloudinary.com/dza7lstvk/image/upload/v1669739945/Medusa%20Docs/Other/aahqJp4_lbtfdz.png'
}
},
{
type: "doc",
id: "deployments/server/deploying-on-qovery",
label: "Deploy on Qovery",
customProps: {
image: 'https://i.imgur.com/qOvY2dN.png'
image: 'https://res.cloudinary.com/dza7lstvk/image/upload/v1669739955/Medusa%20Docs/Other/qOvY2dN_vogsxy.png'
}
},
{
type: "doc",
id: "deployments/server/deploying-on-railway",
label: "Deploy on Railway",
customProps: {
themedImage: {
light: 'https://res.cloudinary.com/dza7lstvk/image/upload/v1669741520/Medusa%20Docs/Other/railway-light_fzuyeo.png',
dark: 'https://res.cloudinary.com/dza7lstvk/image/upload/v1669741520/Medusa%20Docs/Other/railway-dark_kkzuwh.png'
}
}
}
]
Expand Down

0 comments on commit 663e262

Please sign in to comment.