Strapi

Easy Strapi deployment on a VPS with Dokku

Boaz Poolman

Published on 4 May 2024

This tutorial covers everything from installing Dokku and creating a new app to configuring SSL with Let's Encrypt and deploying your app. Following these instructions, you can deploy and manage web applications, databases, and other services on your servers or cloud providers like DigitalOcean.

What is Dokku?

Dokku is a Docker-powered PaaS that helps you build and manage the lifecycle of applications. It is designed to be a lightweight and simple alternative to Heroku.

You can think of it as a self-hosted Heroku that you can deploy on your own infrastructure. With Dokku, you can easily deploy and manage web applications, databases, and other services on your own servers or cloud providers like DigitalOcean.

Prerequisites

  • SSH access to a server running Ubuntu 22.04 LTS
  • A domain of which the DNS points to the Ubuntu server
  • A Strapi v4 instance

Install Dokku

To start you'll have to SSH to the Ubuntu server. Once there you have to run the following commands:

# for debian systems, installs Dokku via apt-get
wget -NP . https://dokku.com/install/v0.34.3/bootstrap.sh
sudo DOKKU_TAG=v0.34.3 bash bootstrap.sh

For more information about Dokku installation please refer to their documentation here

Create a Dokku app

Once you have Dokku installed you can create your first Dokku app!

# on the Dokku host
dokku apps:create strapi

Setup your .env variables

To run Strapi succesfully you'll need to setup a couple of ENV variables. You can do that with the following command:

# on the Dokku host
dokku config:set strapi APP_KEYS="toBeModified1,toBeModified2" API_TOKEN_SALT=tobemodified ADMIN_JWT_SECRET=tobemodified JWT_SECRET=tobemodified DATABASE_CLIENT=postgres NODE_ENV=production

Please make sure to replace the "toBeModified" keys with actual secrets. If you need help generating these secrets you can use this handy tool

Create a Postgres database

We'll use a Postgres database on the Dokku server as our database for Strapi.

# on the Dokku host
# install the postgres plugin
# plugin installation requires root, hence the user change
sudo dokku plugin:install https://github.com/dokku/dokku-postgres.git

# create a postgres service with the name strapi-database
dokku postgres:create strapi-database

Once you've created the database you'll have to link it to your Dokku app. You can do that by running the following;

# on the Dokku host
# each official datastore offers a `link` method to link a service to any application
dokku postgres:link strapi-database strapi

Optionally you might want to expose your database to the outside world. That way you can use a database inspection tool like TablePlus to interact with it.

To expose your newly created Postgres database, run the following command;

# on the Dokku host
dokku postgres:expose strapi-database 5432

Connect your domain

For the purpose of keeping this tutorial simple I'm assuming you've allready setup the DNS of your domain to point to the Dokku server.

# on the Dokku host
dokku domains:add strapi my-domain.com

Setup the proxy

Once you've setup the domain you'll have to configure the proxy. You can do that by running the following three commands;

# on the Dokku host
dokku ports:add strapi http:1337:1337
dokku ports:add strapi http:80:1337
dokku ports:add strapi https:443:1337

Depending on your Dokku version you might have to use the `dokku proxy:ports-add` instead.

Deploy your app

Once all of that is done you are ready to deploy you Strapi app!

 

To learn how to deploy your Strapi app to a Dokku server automatically using Github Actions you can read my article about the topic:

Strapi CI with Dokku and Github Actions

Boaz Poolman

1 month ago

Setup Let's Encrypt certificate

To generate a Let's Encrypt SSL certificate we'll use the dokku-letsencrypt plugin. You can install that by running the following command

# on the Dokku host
sudo dokku plugin:install https://github.com/dokku/dokku-letsencrypt.git

Once you have the plugin installed you can enable it for your app and it will automatically generate the certificates for the assosiated domains.

# on the Dokku host
dokku letsencrypt:enable strapi

Boaz Poolman

Published on 4 May 2024

More of Strapi