DEV Community

Cover image for Deploying Strapi 4 to Railway
Cândido Sales Gomes
Cândido Sales Gomes

Posted on • Edited on

Deploying Strapi 4 to Railway

What is Strapi?

Strapi is an open-source headless Content Management System that offers you the chance to develop API with distinctive features. It's built on Node.js and works with any GraphQL enabled Frontend Frameworks.

You can create custom content types, relationships and media libraries for images and audio files with Strapi.

What is Railway?

It's a PaaS (similar to Heroku) that allows you to connect your Git repository and with each commit or new PR you trigger an event to deploy your application. Railway has several very nice features:

  • Uses NixPacks to build the images;
  • Free SSL Certificate;
  • Straightforward UI;
  • Create multiple environments;
  • "Pay as you grow";

However, there are some features I wish it had:

  • More regions available - 🇧🇷 please (See the thread);
  • Access the application console via UI (Similar to Digital Ocean and Render);

Deploy

Create your account and create a new project

Creating a new project

Search by Strapi and select the first option.

Selecting the Strapi option

Add a valid name for your repository.

Adding environment variables

The railway will copy a project into your account during the deployment process. As shown in the image below, you will have to add the environment variables.

The project

You can use the https://randomkeygen.com/ to fill up the ADMIN_JWT_SECRET,JWT_SECRET, APP_KEYS and API_TOKEN_SALT.

In that project is set up the Strapi with Postgres and Cloudinary. Cloudinary is responsible for managing your media library, so you must have a Cloudinary account.

In the Cloudinary, you copy your credentials.

Cloudinary Credentials

Then paste it into the Railway Environment variables and click on Deploy.

Railway Environment variables

Waiting the building and deploy!

Building

It's done!

Deployed

Now you can access the Strapi admin via your https://strapi-production-XXXX.up.railway.app/admin and create a user account.

Troubleshooting

One annoying thing about Strapi is that it doesn't have development mode enabled by default, so I can't create new collections.

Can't create new collections

I wasted a lot of time thinking that changing the NODE_ENV environment variable could solve it, but it didn't.

So I found another way to solve this.

An important thing to note: the current version deployed is v4.1.9.

v4.1.9

To enable development mode and update the project we need to make a change to the project created by Railway in your Github account.

Change the file package.json to be like this:

{
  "name": "strapi",
  "private": true,
  "version": "0.1.0",
  "description": "A Strapi application",
  "scripts": {
    "develop": "strapi develop",
    "start": "strapi develop", // <<----- Here
    "build": "strapi build",
    "strapi": "strapi"
  },
  "devDependencies": {},
  "dependencies": {
    "@strapi/plugin-i18n": "4.1.9", // Upgrade to 4.3.2
    "@strapi/plugin-users-permissions": "4.1.9", // Upgrade to 4.3.2
    "@strapi/provider-upload-cloudinary": "4.1.9", // Upgrade to 4.3.2
    "@strapi/strapi": "4.1.9", // Upgrade to 4.3.2
    "pg": "8.7.3"
  },
  "author": {
    "name": "A Strapi developer"
  },
  "strapi": {
    "uuid": "35d7a084-3088-4d0b-869e-f503693ee8e5"
  },
  "engines": {
    "node": ">=12.x.x <=16.x.x",
    "npm": ">=6.0.0"
  },
  "license": "MIT"
}
Enter fullscreen mode Exit fullscreen mode

To start Strapi in development mode, I changed the value for strapi develop in the command start.

⚠️ Don't forget to run yarn install to update the yarn.lock before pushing your commit.

You can see my commit here: https://github.com/candidosales/strapi-railway-deploy/commit/529cb16521f33a5d824a34a1de3f30868b122ff7

After pushing the commit, the build will start again and when finished ...

Deployed!

You can verify that it will be in the new version, and you can create new collections.

v4.3.2

Enable to create collections


If you want to have more control over the media gallery and don't want to deploy using Cloudinary, in the following article, I'll teach you how to deploy Strapi 4 to Render.

Any feedback is welcome! 😄

Top comments (3)

Collapse
 
fpigeonjr profile image
Frank Pigeon Jr. 🇵🇦🇺🇸

yes this is the way

Collapse
 
7999999166 profile image
Kanik Goyal • Edited

This is The easiest way to deploy strapi successfully ❤️❤️❤️

Collapse
 
candidosales profile image
Cândido Sales Gomes

After deploying the Strapi, Railway will copy a project to your Github account. You have to run the yarn install in that project.