DEV Community

Cover image for How to deploy an AdonisJS app and Redis to Digital Ocean
Osinachi Chukwujama
Osinachi Chukwujama

Posted on • Edited on

How to deploy an AdonisJS app and Redis to Digital Ocean

Introduction

The Digital Ocean App Platform simplifies deployment to four steps.

  1. Choose repo
  2. Set app name and region
  3. Set environment variables
  4. Choose plan

In this blog, we will learn how to deploy an AdonisJS app which requires Redis to Digital Ocean.

Getting started

  • If you don't have a repo already, fork and clone this one
$ gh repo fork https://github.com/vicradon/tic-tac-toe.git
Enter fullscreen mode Exit fullscreen mode

You can do it manually if you don't have the Github CLI.

  • After cloning, install the adonis CLI globally (use sudo for Linux)
$ npm i -g @adonis/cli
Enter fullscreen mode Exit fullscreen mode
  • Afterwards, create a .env file
$ cp .env.example .env
Enter fullscreen mode Exit fullscreen mode
  • Finally, generate an app key
$ adonis key:generate
Enter fullscreen mode Exit fullscreen mode

This key will be copied to the .env file. Take note of it. We will make use of it later.

Deploying a Redis Cluster

Since our app needs Redis, we have to deploy Redis separately.

Step 1: Navigating to the databases page

Navigate to the new database page to create a Redis Cluster

New database page

Step 2: Choose a Datacenter

Choose a datacenter closest to most of your users.

Data center

Step 3: Choose a Database Cluster Name

You can either leave it as the default or choose a new name. Click the Create a Database Cluster button to continue.
DB Cluster

While Digital Ocean provisions our Redis Cluster, we will deploy our Adonis App.

Deploying the Adonis App

Step 1: Choose Your Repo

choosing a repo

Step 2: Choose an App Name, Region and Branch

As we previously did, choose an app name and region closest to you. You can also select your branch from here.
Selection_009

Step 3: Configure Your App

Digital Ocean is able to smartly detect what our app runs on.
Platform detect

We need to set a few environment variables.
environment variables

To obtain the values of REDIS_HOST, REDIS_PORT and REDIS_PASSWORD, go to the database cluster you deployed earlier and skip the getting started page shown.

skip getting started

In the connection details section, you should be able to see the host, port and password.
host port and password

Copy the values of each and set them as environment variables in your app's deployment page
environment variables

App Key and Redis Connection

Obtain the APP_KEY value from the project's environment file. Ensure you set REDIS_CONNECTION as prod. You should have 5 environment variables in total.

Setting the run command

run command

Set the run command as npm run prod. This will run the app with a disabled .env file.

Step 4: Setting the app's plan

Select the basic plan and click the Launch Basic App button.
Selection_013

Your app should be deployed within a few minutes. Here's my own deployed app.
deployed app

A note about TLS

Redis clusters on Digital Ocean have SSL enabled. This means you won't be able to connect to it using the redis-cli without configuring stunnel. Alternatively, you can use redli to connect to a Redis cluster over SSL.

Adonis uses ioredis as its default redis client. ioredis requires a tls host in its config to connect to a cluster over SSL. If you check config/redis.js file in the app, you'll see the tls object in the prod variables.

  prod: {
    host: Env.get("REDIS_HOST"),
    port: Env.get("REDIS_PORT"),
    password: Env.get("REDIS_PASSWORD"),
    db: 0,
    keyPrefix: "",
    tls: {
      host: Env.get("REDIS_HOST"),
    },
  },
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this tutorial, you learned how to deploy an AdonisJS app and a Redis cluster to Digital Ocean. You experienced the ease of deployment Digital Ocean offers for app developers. You also learned more about Transport Layer Security on Redis clusters.

If you enjoyed this tutorial, you can subscribe to my newsletter on my website and also follow me on Twitter. Thanks for reading. Adios ✌🏾🧡.

Top comments (0)