Introduction
The Digital Ocean App Platform simplifies deployment to four steps.
- Choose repo
- Set app name and region
- Set environment variables
- 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
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
- Afterwards, create a .env file
$ cp .env.example .env
- Finally, generate an app key
$ adonis key:generate
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
Step 2: Choose a Datacenter
Choose a datacenter closest to most of your users.
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.
While Digital Ocean provisions our Redis Cluster, we will deploy our Adonis App.
Deploying the Adonis App
Step 1: Choose Your 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.
Step 3: Configure Your App
Digital Ocean is able to smartly detect what our app runs on.
We need to set a few 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.
In the connection details section, you should be able to see the host, port and password.
Copy the values of each and set them as environment variables in your app's deployment page
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
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.
Your app should be deployed within a few minutes. Here's my own 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"),
},
},
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)