DEV Community

Cover image for Strapi QuickStart on DigitalOcean App Platform
Thew
Thew

Posted on • Updated on

Strapi QuickStart on DigitalOcean App Platform

This is the first article of the series Trying DigitalOcean App Platform which is part of DigitalOcean App Platform Hackathon

In this post, I will deploy a Strapi quickstart to DigitalOcean App Platform.

Update 2022: This post was created with Strapi version 3, the latest Strapi version is 4. There is an official video on Deploying Strapi V4 to DigitalOcean on YouTube.

Setup strapi

Create a strapi project in to my-project directory.

npx create-strapi-app my-project --quickstart
Enter fullscreen mode Exit fullscreen mode

The quickstart project will use a local sqlite as a database. The default sqlite database file is at ./.tmp/data.db.

After the project has been created, the browser will be opened with admin setup page automatically. The admin user and content you create at this step will be available only on your computer since it is stored in local sqlite.

Admin setup page

Production database config

To tell Strapi to use DigitalOcean database when we deploy it, create the following ./config/env/production/database.js file.

module.exports = ({ env }) => ({
  defaultConnection: "default",
  connections: {
    default: {
      connector: "bookshelf",
      settings: {
        client: "postgres",
        host: env("DATABASE_HOST", "localhost"),
        port: env.int("DATABASE_PORT", 5432),
        database: env("DATABASE_NAME", "strapi"),
        username: env("DATABASE_USERNAME", "strapi"),
        password: env("DATABASE_PASSWORD", "strapi"),
        ssl: {
          ca: env("DATABASE_CA", ""),
        },
      },
    },
  },
});
Enter fullscreen mode Exit fullscreen mode

Develop Strapi

Develop Strapi as usual. If you are new to Strapi, you can try Quick Start Guide

Push code to GitHub repository

For example, here is my repository.

Launch DigitalOcean App

  1. Go to DigitalOcean App Platform Console and choose the repository. Choose the repository
  2. Choose the region that is close to your user Choose region
  3. Set the following environment variables and change HTTP port to 1337. Notice the db prefix in environment variables.
    • NODE_ENV = production
    • DATABASE_HOST = ${db.HOSTNAME}
    • DATABASE_PORT = ${db.PORT}
    • DATABASE_NAME = ${db.DATABASE}
    • DATABASE_USERNAME = ${db.USERNAME}
    • DATABASE_PASSWORD = ${db.PASSWORD} (Encrypt)
    • DATABASE_CA = ${db.CA_CERT}Set enivronment variables and HTTP port
  4. Add database, set its name to db. The name must be the same as the prefix you used in environment variables.Add database
  5. Choose your plan and container size. The lowest cost plan is $12 (Basic $5/mo). Then launch your Strapi App! 🎉 Choose pricing plan

Congratulations! 🎊

The Strapi application is being deployed and will be ready in around 10 minutes. You can visit https://<your-url>/admin to setup production admin account.

Buy Me A Coffee

Oldest comments (6)

Collapse
 
desdamo profile image
desdamo

Hello. I have a question about it. The Quick start created a SQLite dB and you use postgres. Are you sûre it will work fine? Because i followed tout tutorial and digital ocean cant deploy my app.

Collapse
 
thewdhanat profile image
Thew • Edited

Yes. You have to make sure that you set NODE_ENV variable to production and has ./config/env/production/database.js as I wrote above.

Collapse
 
johnsickels profile image
John Sickels • Edited

Thanks for the resource. I also needed to npm i pg strapi-provider-upload-do and add this property to the config/database.js:

module.exports = ({ env }) => ({
  defaultConnection: "...",
  connections: {
    default: {
      connector: "...",
      settings: {...},
      options: {}, // <----- missing this
    },
  },
});
Enter fullscreen mode Exit fullscreen mode
Collapse
 
osspro profile image
Osiel

The sintaxis of the database.js changed for Strapi v4, this is the new one:

module.exports = ({ env }) => ({
  connection: {
    client: 'postgres',
    connection: {
      host: env('DATABASE_HOST', '127.0.0.1'),
      port: env.int('DATABASE_PORT', 5432),
      database: env('DATABASE_NAME', 'strapi'),
      user: env('DATABASE_USERNAME', 'strapi'),
      password: env('DATABASE_PASSWORD', 'strapi'),
      schema: env('DATABASE_SCHEMA', 'public'), // Not required
      ssl: {
        rejectUnauthorized: env.bool('DATABASE_SSL_SELF', false), // For self-signed certificates
      },
    },
    debug: false,
  },
});
Enter fullscreen mode Exit fullscreen mode
Collapse
 
agdgdag profile image
agdgdag

if you only deploy Strapi to DO app platform thats fine, but if you attempt to deploy both Strapi and a static site (like Next Js for example) together you'll run into problems with http request routes. The way Strapi is configured was meant to be used with the default root route, but in most cases we want to point the static site to default route "/". So the workaround needed is to create 2 separate apps here, or avoid their app platform and create a droplet configuring everything manually. Hope this helps someone

Collapse
 
kkdeve profile image
KKDDEV

can basic 5usd/mo plan work for strapi? mine is a practice project?