docs: create docs workspace (#5174)

* docs: migrate ui docs to docs universe

* created yarn workspace

* added eslint and tsconfig configurations

* fix eslint configurations

* fixed eslint configurations

* shared tailwind configurations

* added shared ui package

* added more shared components

* migrating more components

* made details components shared

* move InlineCode component

* moved InputText

* moved Loading component

* Moved Modal component

* moved Select components

* Moved Tooltip component

* moved Search components

* moved ColorMode provider

* Moved Notification components and providers

* used icons package

* use UI colors in api-reference

* moved Navbar component

* used Navbar and Search in UI docs

* added Feedback to UI docs

* general enhancements

* fix color mode

* added copy colors file from ui-preset

* added features and enhancements to UI docs

* move Sidebar component and provider

* general fixes and preparations for deployment

* update docusaurus version

* adjusted versions

* fix output directory

* remove rootDirectory property

* fix yarn.lock

* moved code component

* added vale for all docs MD and MDX

* fix tests

* fix vale error

* fix deployment errors

* change ignore commands

* add output directory

* fix docs test

* general fixes

* content fixes

* fix announcement script

* added changeset

* fix vale checks

* added nofilter option

* fix vale error
This commit is contained in:
Shahed Nasser
2023-09-21 20:57:15 +03:00
committed by GitHub
parent 19c5d5ba36
commit fa7c94b4cc
3209 changed files with 32188 additions and 31018 deletions
@@ -0,0 +1,332 @@
---
description: 'Learn step-by-step.'
addHowToData: true
---
# Deploy Your Medusa Backend to DigitalOcean Apps
In this document, you'll learn how to deploy your Medusa backend to a DigitalOcean App.
DigitalOcean is a reliable hosting provider that provides different ways to host websites and servers. One way to host a backend is using their DigitalOcean App Platform.
## Prerequisites
### Medusa Backend
It is assumed that you already have a Medusa backend installed locally. If you dont, please follow the [quickstart guide](../../create-medusa-app.mdx).
Furthermore, your Medusa backend should be configured to work with PostgreSQL and Redis. You can follow the [Configure your Backend documentation](../../development/backend/configurations.md) to learn how to do that.
### Needed Accounts
- A [DigitalOcean](https://cloud.digitalocean.com/registrations/new) account. You need to provide a payment method on sign up.
- A [GitHub](https://github.com/) account to create a repository to host your Medusa backends codebase.
:::tip
If you want to use another Git Provider supported by DigitalOcean, its possible to follow along with this guide but youll have to perform the equivalent steps in your Git Provider.
:::
### Required Tools
- Gits CLI tool. You can follow [this documentation to learn how to install it for your operating system](../../development/backend/prepare-environment.mdx#git).
---
## Changes to package.json
Change the `start` script in `package.json` to the following:
```json
"start": "medusa migrations run && medusa start"
```
This ensures that Migrations are run every time the Medusa backend is restarted.
---
## Changes to medusa-config.js
In `medusa-config.js`, the `DATABASE_URL` variable is set to the environment variable `DATABASE_URL`. This needs to be changed as DigitalOcean provides the different details of the database connection separately.
Replace the previous declaration of `DATABASE_URL` in `medusa-config.js` with the following:
```js title="medusa-config.js"
const DB_USERNAME = process.env.DB_USERNAME
const DB_PASSWORD = process.env.DB_PASSWORD
const DB_HOST = process.env.DB_HOST
const DB_PORT = process.env.DB_PORT
const DB_DATABASE = process.env.DB_DATABASE
const DATABASE_URL =
`postgres://${DB_USERNAME}:${DB_PASSWORD}` +
`@${DB_HOST}:${DB_PORT}/${DB_DATABASE}`
```
In addition, you must add to `projectConfig` in the exported object a new property `database_extra`:
```js title=medusa-config.js
module.exports = {
projectConfig: {
// ...
database_extra: { ssl: { rejectUnauthorized: false } },
},
}
```
Also, if you're planning on using scheduled jobs, you need to set the `redis_url` configurations:
```js title=medusa-config.js
module.exports = {
projectConfig: {
// ...
redis_url: process.env.REDIS_URL,
},
}
```
---
## Changes to .gitignore
Your `.gitignore` may have `yarn.lock` and `package-lock.json` in it. If so, remove both or one of them to ensure they're committed with your code. Otherwise, you may face build errors after deployment.
---
## (Optional) Use Production Modules
If you're using development modules, such as the Local Event Bus or the In-Memory Cache Modules, it's highly recommended to use a production module instead.
Medusa provides a [Redis Event Bus](../../development/events/modules/redis.md) and a [Redis Cache Module](../../development/cache/modules/redis.md) that you can install and configure. Alternatively, you can use custom modules that integrate other services for these functionalities.
---
## 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:
```bash
git init
git remote add origin <GITHUB_URL>
git branch -M <BRANCH>
```
Where:
- `<GITHUB_URL>` is the URL you just copied.
- `<BRANCH>` is the name of your default branch. Typically, it's either `main` or `master`. If you're unsure, you can find it on the repository's main page.
Then, add, commit, and push the changes into the repository:
```bash
git add .
git commit -m "initial commit"
git push origin <BRANCH>
```
Where, `<BRANCH>` is the default repository branch.
After pushing the changes, you can find the files in your GitHub repository.
---
## Deploy to DigitalOcean App
After logging into your account, click on the Create button at the top right, then choose Apps.
### Choose Repository
In the Create App page, choose GitHub from the Service Provider list.
If you havent given DigitalOcean access before, click on Manage Access under Source Code. Youll then be redirected to GitHub to give DigitalOcean access.
Once DigitalOcean has access to your GitHub account, you should see a Repository input. Click on it and search for the repository you created earlier.
Additional inputs will show up to choose the Branch, Source Directory, and Autodeploy options.
If you host your Medusa backend in a monorepo, you should change the Source Directory to the directory the backend is available in the repository. Otherwise, it can be left as is.
Once youre done, click Next to move on to the next step.
### Specify Web Service Resources
In the next step, youll see the resources to create.
:::note
If you have a Dockerfile available in the backends codebase, youll have two resources showing. You can remove it by clicking on the trash icon at the right of the resource.
:::
Click on the Edit button next to the Web Service, and ensure the following information is set correctly:
- Resource Type is set to Node.js
- HTTP Port is set to 9000
- HTTP Request Routes is set to `/`
Once you've set up everything correctly, click the Back button at the end of the form.
### Specify Database Resources
On the same page, expand the Add Resources section, choose Database, then click Add.
In the new page, youll be shown a PostgreSQL database to be created. Notice that its important to choose a name that youll remember as youll need the name in next steps. You can leave the name as is if its not necessary to change it.
Once youre done, click Create and Attach. Youll be redirected back to the previous page with the database added to the resources.
Once youre done, click Next to move on to the next step.
### Set Environment Variables
In this section, youll add environment variables that are essential to your Medusa backend.
You should see two ways to add environment variables: Global or specific to the Web Service.
Click Edit on the second row to add environment variables specific to the Web Service. Add the following environment variables:
```bash
DB_USERNAME=${db.USERNAME}
DB_PASSWORD=${db.PASSWORD}
DB_HOST=${db.HOSTNAME}
DB_PORT=${db.PORT}
DB_DATABASE=${db.DATABASE}
REDIS_URL=${redis.DATABASE_URL}
JWT_SECRET=secret
COOKIE_SECRET=secret
NPM_CONFIG_PRODUCTION=false
YARN_PRODUCTION=false
NODE_ENV=production
```
:::caution
Its highly recommended to use strong, randomly generated secrets for `JWT_SECRET` and `COOKIE_SECRET`.
:::
Notice how for database environment variables you access the values from the database you created earlier `db`. If you changed the name of the database, you must change `db` here to the name you supplied to the PostgreSQL database.
Another thing to note here is that you added a `REDIS_URL` environment variable that uses a `redis` resource to retrieve the URL. Redis is necessary if you're using Scheduled Jobs and if you're using Redis modules, such as the Redis Event Bus module. Youll be creating the Redis resource in a later section.
If you're using modules that require setting other environment variables, make sure to set them here. You can also add them later.
Once youre done click Save, then click Next to proceed to the next section.
### Finalize App
In the next section, youll be shown the app info and the region it will be deployed to. You can leave it all as is or make changes if you find it necessary.
Once youre done, click Next to go to the next step.
In the final step, you can see a review of everything you created. If everything looks good, scroll down and click Create Resource.
### Create Redis Resource
While the backend is being deployed, you can create the Redis resource. To do that:
1. Click the Create button at the top right and choose Database from the dropdown.
2. In the new page under Choose a database engine, choose Redis.
3. Scroll down to the “Choose a unique database cluster name” input. Since you used the name `redis` in the `REDIS_URL` environment variables, change the value to `redis` here.
4. Once youre done, click on Create Database Cluster.
### Attach Redis Database
Once the Redis database is created, you need to attach it to your app. To do that:
1. Go back to the App you created earlier by choose Apps in the sidebar then clicking on the App name.
2. Click at the white Create button at the top right and choose Create/Attach Database.
3. In the new page, click on the Previously Created DigitalOcean Database radio button. Then, under Database Cluster select the Redis database you just created.
4. Once youre done click Attach Database. This will add the Redis database to the list of resources of your App and will trigger a redeploy of the App.
### Change Health Check Settings
By default, DigitalOcean will perform a health check immediately as the app runs. However, the Medusa backend requires some initial load-time before it's ready for a health check.
So, you must delay the app's health check to enure that the deployment doesn't fail. To do that:
1. On your app's page, click on the Settings page.
2. From the tabs before the settings section, choose the name of your web service resource.
3. Scroll down and find the "Health Checks" section, then click on "Edit" next to it.
4. In the section that opens, expand the "Show Advanced Parameters" section.
5. Find the "Initial Delay (s)" input and set its value to `300`.
6. Once done, click on the Save button.
This will redeploy your app, which should finish successfully.
---
## Test your Backend
Once the redeployment is complete, copy the URL of the App which can be found under the Apps name.
Then, go to `<YOUR_APP_URL>/store/products`. If the deployment was successful, you should receive a JSON response.
### 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](../../admin/configuration.md#build-command-options) 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 errors occur in your deployment, you can find the logs by going to the Activity tab in your DigitalOcean App.
### ERROR: Failed to build / Project does not contain a package manager
If you find this error in your logs, make sure to remove `yarn.lock` or `package-lock.json` from the `.gitignore` file in your project, then commit the changes.
---
## Run Commands on Your Backend
To run commands on your backend, you can access the console on the Apps page by choosing the Console tab. This opens a console in your browser where you can run commands on your backend.
For example, you can run the following commands to create a new admin user:
```bash
npx @medusajs/medusa-cli user --email <EMAIL> --password <PASSWORD>
```
Make sure to replace `<EMAIL>` and `<PASSWORD>` with the credentials you want to give the user.
---
## Add Environment Variables
Youll likely need to add environment variables later such as Admin Cross-Origin Resource Sharing (CORS) and Store CORS variables.
To add environment variables, on the Apps page click on Settings and choose the Web Service component.
Then, scroll down and find Environment Variables. You can expand the environment variables by clicking Edit on the right. Here, you can edit, add, and remove environment variables.
Once you click Save, the environment variables will be saved and a redeployment will be triggered.
---
## See Also
- [Deploy the Medusa Admin](../admin/index.mdx).
- [Deploy the Storefront](../storefront/index.mdx).
@@ -0,0 +1,334 @@
---
description: 'Learn step-by-step.'
addHowToData: true
---
# Deploy Your Medusa Backend on Heroku
In this document, you'll learn how to deploy your Medusa backend on Heroku. Heroku is a PaaS (Platform as a Service) that allows you to easily deploy your applications in the cloud.
Alternatively, you can use this button to deploy the Medusa backend to Heroku directly:
<a href="https://heroku.com/deploy?template=https://github.com/medusajs/medusa-starter-default/tree/feat/deploy-heroku" className="img-url">
<img src="https://www.herokucdn.com/deploy/button.svg" alt="Deploy to Heroku" className="no-zoom-img" />
</a>
## Prerequisites
### Medusa Backend
It is assumed that you already have a Medusa backend installed locally. If you dont, please follow the [quickstart guide](../../development/backend/install.mdx).
Furthermore, your Medusa backend should be configured to work with PostgreSQL and Redis. You can follow the [Configure your Backend documentation](../../development/backend/configurations.md) to learn how to do that.
### Needed Accounts
- A [Heroku](https://heroku.com/) account.
### Required Tools
- Gits CLI tool. You can follow [this documentation to learn how to install it for your operating system](../../development/backend/prepare-environment.mdx#git).
- Heroku's CLI tool. You can follow [Heroku's documentation to learn how to install it for your operating system](https://devcenter.heroku.com/articles/heroku-cli).
---
## Deploy to Heroku
### 1. Login to Heroku from your CLI
Before you can create an app with Heroku, you must login with the CLI tool:
```bash
heroku login
```
Depending on your operating system, you must follow either the instructions in your terminal or a page in your browser will open.
### 2. Create an App with Heroku
In the root directory of your Medusa backend, run the following commands to create an app on Heroku and add it as a remote origin:
```bash
heroku create <APP_NAME>
heroku git:remote -a <APP_NAME>
```
Where `<APP_NAME>` is the name of the app you'll create. You can use any name you want.
### 3. Install Postgresql and Redis on Heroku
Medusa requires a Postgres database and a Redis instance to work. You can add those to your Heroku app using Add-ons.
:::note
If you don't have a payment method set up in your Heroku account, you'll be asked to enter your payment details when you try to install these addons.
:::
#### PostgreSQL
Add a Postgres add-on to your Heroku app with the following command:
```bash
heroku addons:create heroku-postgresql:mini
```
This uses Heroku Postgres's smallest plan. You can check out [the available plans and pricing of Heroku Postgres on Heroku's website.](https://elements.heroku.com/addons/heroku-postgresql#pricing)
#### Redis
Add a Redis instance to your Heroku app with the following command:
```bash
heroku addons:create stackhero-redis:ist-m4euc0
```
This uses the lowest plan in Stackhero Redis. You can check out [the plans and pricing of Stackhero Redis on Heroku's website.](https://elements.heroku.com/addons/stackhero-redis#pricing)
### 4. Configure Environment Variables on Heroku
Medusa requires a set of environment variables to be configured. You can learn more about Medusa's configurations in the [Configure your Medusa backend](../../development/backend/configurations.md) document.
Run the following commands in the root directory of your Medusa backend to set some environment variables:
```bash
heroku config:set NODE_ENV=production
heroku config:set JWT_SECRET=your-super-secret
heroku config:set COOKIE_SECRET=your-super-secret-pt2
heroku config:set NPM_CONFIG_PRODUCTION=false
```
:::tip
Make sure to replace `your-super-secret` and `your-super-secret-pt2` with actual secrets in a production environment.
:::
#### Set Buildpack
Additionally, you need to set the buildpack to Node.js using the following command:
```bash
heroku buildpacks:set heroku/nodejs
```
#### Configure the Redis URL
Stackhero Redis adds the Redis URL under the environment variable `STACKHERO_REDIS_URL_TLS`. However, Medusa looks for the `REDIS_URL` environment variable when initializing the connection with Redis.
Retrieve the value of `STACKHERO_REDIS_URL_TLS` with the following command:
```bash
heroku config:get STACKHERO_REDIS_URL_TLS
```
This prints the value of the environment variable which is a Redis connection string.
:::note
If you see nothing, you have to wait until the creation process of the stackhero redis instance is done. Try waiting a few minutes then trying again.
:::
Copy that value and use it to set the environment variable `REDIS_URL` with the following command:
```bash
heroku config:set REDIS_URL=<YOUR_REDIS_URL>
```
Where `<YOUR_REDIS_URL>` is the value you received from the previous command.
#### Configure the PostgreSQL Database URL
If you're using the Heroku PostgreSQL Add-on, it should configure the environment variable `DATABASE_URL`. In that case, you don't need to perform any additional actions.
However, if you use another add-on, make sure to set the environment variable `DATABASE_URL` to the PostgreSQL Database URL.
#### (Optional) Configure Modules Environment Variables
If you use in your Medusa backend modules that require setting environment variables, then you should set them at this point.
For example, if you use the Redis Event Bus module:
```bash
heroku config:set EVENT_REDIS_URL=<YOUR_REDIS_URL>
```
Make sure to change `EVENT_REDIS_URL` to the environment variable name you use for your module's configurations.
If your module requires setting up other services, do that then add the environment variables.
#### (Optional) Configure CORS Variables
Optionally, if you've deployed the admin dashboard and you want to ensure it can use the backend's REST APIs, you must set the following environment variable:
```bash
heroku config:set ADMIN_CORS=<YOUR_ADMIN_URL>
```
Where `<YOUR_ADMIN_URL>` is the URL of your admin dashboard.
Similarly, if you've deployed the storefront and you want to ensure it can use the backend's REST APIs, you must set the following environment variable:
```bash
heroku config:set STORE_CORS=<YOUR_STOREFRONT_URL>
```
Where `<YOUR_STOREFRONT_URL>` is the URL of your storefront.
### 5. Configure Medusa Backend
Before jumping into the deployment, you need to configure your Medusa backend to ensure it uses the previous environment variables and the recommended production configurations.
#### medusa-config.js
Update `module.exports` to include the following configurations:
```js
module.exports = {
projectConfig: {
redis_url: REDIS_URL,
database_url: DATABASE_URL,
database_type: "postgres",
store_cors: STORE_CORS,
admin_cors: ADMIN_CORS,
database_extra:
process.env.NODE_ENV !== "development"
? { ssl: { rejectUnauthorized: false } }
: {},
},
plugins,
modules,
}
```
#### package.json
Update `scripts` to include the following scripts:
```json
"scripts": {
"serve": "medusa start",
"start": "medusa develop",
"heroku-postbuild": "medusa migrations run",
"prepare": "npm run build",
"build": "babel src -d dist --extensions \".ts,.js\""
},
```
### 6. Launch your Medusa Backend
Finally, commit and push all changes to Heroku:
```bash
git add .
git commit -m "Deploy Medusa Backend on Heroku"
git push heroku HEAD:master
```
This triggers a redeploy of the Medusa backend with all the new configurations.
## Test your Backend
To test your backend, run the following command to retrieve the backend's URL:
```bash
heroku apps:info -a <APP_NAME>
```
Where `<APP_NAME>` is the name of the app. You should see as the output a bunch of info of the app.
The backend's URL is available under "Web URL". You can copy it and perform requests to it to test it out.
For example, you can send a request to `<YOUR_BACKEND_URL>/store/products` and you should receive a JSON response with the products in your store.
### 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](../../admin/configuration.md#build-command-options) command as part of the `serve` 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
#### Inspect Build Logs
If an error occurs during the deployment, you can explore your Heroku app build logs using the following command in the root directory of your Medusa backend:
```bash
heroku logs -n 500000 --remote heroku --tail -a <APP_NAME>
```
Where `<APP_NAME>` is the name of the app.
#### Error: Babel not found
If you get the following error in the logs of your application:
```bash
/bin/sh: 1: /app/node_modules/.bin/babel: not found
```
You can resolve it by creating a file in the root of your Medusa backend directory named `Procfile` (without an extension) with the following content:
```
web: npm run serve
```
Then, push the changes with Git:
```bash
git add .
git commit -m "Added Procfile"
git push heroku HEAD:master
```
Once the application is re-published, you can access your store as expected.
---
## Run Commands on Your Backend
To run commands on your backend, you can use the following command:
```bash
heroku run -a <APP_NAME> -- <COMMAND>
```
Where `<APP_NAME>` is the name of the app and `<COMMAND>` is the command you want to run.
For example, to create an admin user you can run the following command:
```bash
heroku run -a <APP_NAME> -- medusa user -e "<EMAIL>" -p "<PASSWORD>"
```
Where `<APP_NAME>` is the name of your Heroku app, and `<EMAIL>` and `<PASSWORD>` are the credentials you want to use to log in to the Medusa admin dashboard.
---
## Add Environment Variables
Youll likely need to add environment variables later such as Admin Cross-Origin Resource Sharing (CORS) and Store CORS variables.
To set or change an environment variable's value, you can use the following command:
```bash
heroku config:set <ENV_NAME>=<ENV_VALUE> -a <APP_NAME>
```
Where `<APP_NAME>` is the name of your Heroku app, `<ENV_NAME>` is the name of the environment variable, and `<ENV_VALUE>` is the value.
---
## See Also
- [Deploy your Medusa admin](../admin/index.mdx)
- [Deploy your storefront](../storefront/index.mdx)
@@ -0,0 +1,131 @@
---
description: 'Learn step-by-step.'
addHowToData: true
---
# Deploy Your Medusa Backend on Microtica
In this document, you'll learn how to deploy your Medusa backend on your AWS account with Microtica.
:::note
This guide was submitted through a community contribution.
:::
## Overview
[Microtica](https://microtica.com) is a cloud-native delivery platform that enables you to deploy infrastructure and applications to your AWS account, while providing actionable insights to help you optimize resources, performance, & costs.
Alternatively, you can use this button to deploy the Medusa backend to Microtica directly:
<a href="https://app.microtica.com/templates/new?template=https%3A%2F%2Fraw.githubusercontent.com%2Fmicrotica%2Ftemplates%2Fmaster%2Fmedusa-server%2F.microtica%2Ftemplate.yaml&utm_source=medusa&utm_medium=docs&utm_campaign=medusa" className="img-url">
<img src="https://microtica.s3.eu-central-1.amazonaws.com/assets/templates/logos/deploy-with-microtica.svg" alt="Deploy with Microtica" className="no-zoom-img" />
</a>
:::note
The Medusa infrastructure will be provisioned on your own AWS account.
You retain full control over your infrastructure and data while getting all the benefits of infrastructure automation.
:::
### What will be provisioned on AWS
Since Microtica deploys on your cloud account, here are the resources that the platform is going to provision in the environment.
- VPC, subnets and networking
- Container infrastructure based on Fargate
- application load balancer
- persistent storage
- S3 bucket
- Postgres database
- and Redis (in production mode)
---
## Prerequisites
### Medusa Backend
It is assumed that you already have a Medusa backend installed locally. If you dont, please follow the [quickstart guide](../../development/backend/install.mdx).
Furthermore, your Medusa backend should be configured to work with PostgreSQL and Redis. You can follow the [Configure your Backend documentation](../../development/backend/configurations.md) to learn how to do that.
### Needed Accounts
- A [Microtica](https://app.microtica.com/) account. Microtica provides a free plan that you can use.
- An [AWS](https://aws.amazon.com/) account that youll connect to Microtica to provision the infrastructure.
- A [GitHub](https://github.com/) account to create a repository to host your backend's codebase.
### Required Tools
- Gits CLI tool. You can follow [this documentation to learn how to install it for your operating system](../../development/backend/prepare-environment.mdx#git).
---
## Deploy to Microtica
### Step 1: Create/Import a Git repository
Before you can deploy your Medusa backend you need to connect your preferred Git account. Microtica will create a repository on your Git account with a default repo name `medusa-server`.
If you already have a Medusa backend repository that you want to deploy then you need to perform a couple of changes in your source code. Follow [this tutorial to deploy and existing Medusa Backend app](https://docs.microtica.com/medusa-server?utm_source=medusa&utm_medium=docs&utm_campaign=medusa#xUBRz).
### Step 2: Configure the Template
The second step provides customization possibilities by configuring environment variables.
Add an application name and the admin credentials that will be used to create an initial admin user with which you can later sign in to your Medusa Admin.
Then, choose whether you want a production Medusa Backend environment or a development one. The production template will provision your managed Relational Database Service (RDS) PostgreSQL and Redis instances.
### Step 3: Connect an AWS account
In the last step before deployment, select the environment in which you want to deploy the template. An existing default environment called `development` will be preselected here, or you can create a new environment.
Then, connect your AWS account when prompted. This process takes only a few seconds, so afterward only choose the region you want to deploy in.
### Step 4: Deploy to AWS
Finally, a deployment summary of what will be provisioned on your AWS account is presented. Click on the "Deploy" button to trigger a deployment of the template and start creating the infrastructure for a Medusa backend.
It will take around ten minutes for the solution to be deployed on your cloud account.
You can follow the build pipeline in real-time by clicking the "View Logs" button.
Once the build process is complete, a new deployment with the infrastructure resources is triggered. You can follow the logs of the deployment process by clicking the "View deployment" button, and then selecting the deployment from the list.
---
## Test the Backend
After the deployment is finished, navigate to Resources → [AppName] (Medusa in this example) → Overview. Then, under the Resource Outputs section you should see the "AccessUrl". This is the backend's URL that you can use to access API endpoints and test them. You can try getting the list of products using the endpoint `<AccessUrl>/store/products`.
### 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](../../admin/configuration.md#build-command-options) 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 `<AccessUrl>/app`. If you changed the admin path, make sure to change `/app` to the path you've set.
---
## Add Environment Variables
The environment variables can be updated, added, or configured after deployment as well. You can access them with `process.env.<VARIABLE NAME>`.
You can read more about the built-in environment variables, as well as how to specify custom environment variables in the Medusa backend runtime [in Microtica documentation](https://docs.microtica.com/medusa-server?utm_source=medusa&utm_medium=docs&utm_campaign=medusa#z8li6).
---
## Updating your Deployed Backend
Any updates to the backend (for example, updating the Medusa core version) are deployed automatically when changes are committed to the repository. Microtica will handle the entire process of building and deploying your application on your connected AWS account.
@@ -0,0 +1,265 @@
---
description: 'Learn step-by-step.'
addHowToData: true
---
# Deploy Your Medusa Backend to Railway
In this document, youll learn how to deploy your Medusa backend 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 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:
<a
href="https://railway.app/template/zC7eOq?referralCode=TW4Qi0" class="img-url no-zoom-img">
<img src="https://railway.app/button.svg" alt="Deploy with Railway" class="no-zoom-img"/>
</a>
---
## Prerequisites
### Medusa Backend
It is assumed that you already have a Medusa backend installed locally. If you dont, please follow the [quickstart guide](../../development/backend/install.mdx).
Furthermore, your Medusa backend should be configured to work with PostgreSQL and Redis. You can follow the [Configure your Backend documentation](./../../development/backend/configurations.md) 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:
- [Redis Event Module](../../development/events/modules/redis.md)
- [Redis Cache Module](../../development/cache/modules/redis.md)
### Needed Accounts
- A [Railway](https://railway.app/) account.
- A [GitHub](https://github.com/) account to create a repository to host your backend's codebase.
### Required Tools
- Gits CLI tool. You can follow [this documentation to learn how to install it for your operating system](./../../development/backend/prepare-environment.mdx#git).
---
## (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:
```toml
[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:
```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
```
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:
```bash
PORT=9000
JWT_SECRET=something
COOKIE_SECRET=something
DATABASE_URL=${{postgresql.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:
```bash
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](../../admin/configuration.md#build-command-options) 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](https://docs.railway.app/develop/cli#local-shell).
For example, you can run commands on the backend to seed the database or create a new user using [Medusas CLI tool](../../cli/reference.mdx).
### Create Admin User
You can create an admin user by running the following command in the root of your Medusa project:
```bash
railway run npx medusa user --email admin@medusa-test.com --password supersecret
```
---
## See Also
- [Deploy the Medusa Admin](../admin/index.mdx)
- [Deploy the Storefront](../storefront/index.mdx)
@@ -0,0 +1,107 @@
---
description: "General steps for all hosting providers."
addHowToData: true
---
# General Deployment Guide for Medusa Backend
In this guide, youll learn the general steps you need to take when deploying your Medusa backend. This is useful when the [platform-specific deployment guides](./index.mdx) do not include your selected hosting provider.
## Prerequisites
Its assumed you already have a Medusa backend installed and configured on your local machine. If not, check out the [create-medusa-app guide](../../create-medusa-app.mdx) to install a Medusa project.
---
## (Optional) Step 1: Create GitHub Repository
Many hosting providers allow you to deploy your project directly from GitHub. This makes it easier for you to push changes and updates without having to manually trigger the update in the hosting provider.
If your hosting provider supports that, create a GitHub repository and push your backends code to it.
---
## Step 2: Start Script in package.json
Make sure the `start` script in your `package.json` runs migrations, the `build` command, and the `medusa start` command:
```json title=package.json
"start": "npm run build && medusa migrations run && medusa start"
```
---
## Step 3: Set ssl Database Option
In production, its recommended to set the [database_extra option](../../development/backend/configurations.md#database_extra) in `medusa-config.js` to disable the `ssl.rejectUnauthorized` option:
```jsx title=medusa-config.js
module.exports = {
projectConfig: {
// ...
database_extra: { ssl: { rejectUnauthorized: false } },
},
}
```
---
## Step 4: Setup PostgreSQL Database
Your Medusa backend must connect to a remote PostgreSQL database. If your hosting provider doesnt support creating a PostgreSQL database, you can use [Neon](https://neon.tech/).
Once you set up your PostgreSQL database, make sure to have the connection URL to the database at hand so that you can set it later in your environment variables.
---
## (Optional) Step 5: Setup Architectural Services
Aside from PostgreSQL, you may be using modules or plugins that require some additional architectural setup. For example, if youre using the [Redis Events Module](../../development/events/modules/redis.md), you must set up a Redis database and obtain a connection URL to it.
---
## Step 6: Deploy your Backend
You can deploy your backend now to your hosting provider. During or after the deployment process, based on your hosting provider, you need to add the following environment variables:
```bash
DATABASE_TYPE=postgres
DATABASE_URL=<YOUR_DB_URL>
JWT_SECRET=<RANDOM_SECRET>
COOKIE_SECRET=<RANDOM_SECRET>
NODE_ENV=production
NPM_CONFIG_PRODUCTION=false
```
Where:
- `<YOUR_DB_URL>` is the connection URL to the PostgreSQL database you set up in [step 4](#step-4-setup-postgresql-database).
- `<RANDOM_SECRET>` is a random string that will be used to create authentication and cookie tokens. Make sure to set different ones for `JWT_SECRET` and `COOKIE_SECRET`.
Make sure to also add any other environment variables relevant to your backend. For example, if youve setup Redis as explained in [step 5](#optional-step-5-setup-architectural-services), make sure to add an environment variable for the Redis connection URL.
---
## Step 7: Test it Out
After youve deployed your backend, you can test it out in different ways:
- Go to `<BACKEND_URL>/health`, where `<BACKEND_URL>` is the URL to your deployed backend. If the deployment was successful, you should see `OK` printed in your browser.
- If you deployed the admin dashboard, you can go to `<BACKEND_URL>/app` to view the admin dashboard. If you changed the value of the admin plugins `path` configuration, make sure to replace `app` with that instead.
---
## Set Up CORS Configuration
To connect your storefront and, if deployed separately, your admin dashboard to your deployed Medusa backend, make sure to set up the [admin_cors and store_cors configuration](../../development/backend/configurations.md#admin_cors-and-store_cors) in `medusa-config.js` accordingly.
---
## Create Admin User
If your hosting provider gives you access to execute commands in your deployed Medusa backend project, you can create a new admin user by running the following command in the root directory of your deployed Medusa backend:
```bash
npx medusa user --email admin@medusa-test.com --password supersecret
```
@@ -0,0 +1,13 @@
---
hide_table_of_contents: true
description: 'Learn how to deploy your Medusa backend to different hosting providers including DigitalOcean, Heroku, Railway, and more.'
---
import DocCardList from '@theme/DocCardList';
import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
# Medusa Backend Deployment Guides
Find in this page guides to deploy your Medusa backend on different platforms.
<DocCardList items={useCurrentSidebarCategory().items}/>