These integration guides are not official documentation and the Strapi Support Team will not provide assistance with them.
The purpose of this guide is to allow users to deploy Strapi applications on the DigitalOcean App Platform. This guide uses the PostgreSQL development database provided by DigitalOcean, so applications can be tested in a deployed environment. At the end of the guide there is information on how to connect a Strapi application to a DigitalOcean Managed Database. Additional information about migrating local database content to a production database and other deployment topics are provided in the DigitalOcean documentation.
Prior to starting the deployment process each user needs:
Strapi uses environment configurations to maintain multiple environments inside a single application. This section describes how to setup a production environment in a Strapi application.
./config/env/production
.database.js
inside the ./config/env/production
directory.Add the code snippet to the database
configuration file:
1 // path: /config/env/production/database.ts
2 export default ({ env }) => ({
3 connection: {
4 client: 'postgres',
5 connection: {
6 host: env('DATABASE_HOST'),
7 port: env.int('DATABASE_PORT'),
8 database: env('DATABASE_NAME'),
9 user: env('DATABASE_USERNAME'),
10 password: env('DATABASE_PASSWORD'),
11 ssl: {
12 rejectUnauthorized:env.bool('DATABASE_SSL_SELF', false),
13 },
14 },
15 debug: false,
16 },
17 });
Create server.js
inside the ./config/env/production
directory.
Add the code snippet to the server
configuration file:
1// path: /config/env/production/server.ts
2export default ({ env }) => ({
3 proxy: true,
4 url: env('APP_URL'), // Sets the public URL of the application.
5 app: {
6 keys: env.array('APP_KEYS')
7 },
8 });
Add PostgreSQL dependencies by installing pg
package:
With yarn:
yarn add pg
With npm
npm install pg
Verify that all of the new and modified files are saved locally.
Commit the project to a remote repository:
git add .
git commit -m "commit message"
git push
Deploying on the DigitalOcean App Platform requires creating an App, connecting the App to a development database, and setting environment variables. At the end of the following steps a Strapi application should be successfully deployed.
From the DigitalOcean website create and App and connect it to a GitHub repository:
After creating an App attach a development database. To add a development database on the following screen:
In the DigitalOcean App Platform there are Global and Component-level environment variables. Strapi applications in production need Global-level variables to set the database connection, and can use either Component or Global-level variables for secrets storage. The following procedure is to add the database variables at the Global level and store the Strapi secrets at the Component level.
Add the database, URL, and NODE_ENV variables to the global environment table:
Variable name | Value |
---|---|
APP_URL | ${APP_URL} |
DATABASE_HOST | ${db.HOSTNAME} |
DATABASE_PORT | ${db.PORT} |
DATABASE_NAME | ${db.DATABASE} |
DATABASE_USERNAME | ${db.USERNAME} |
DATABASE_PASSWORD | ${db.PASSWORD} |
NODE_ENV | production |
Add the key-value pairs for the Strapi secrets to the component environment variables table:
Variable name | value |
---|---|
APP_KEYS | "unique user-generated secrets here" |
API_TOKEN_SALT | "unique user-generated secret here" |
ADMIN_JWT_SECRET | "unique user-generated secret here" |
JWT_SECRET | "unique user-generated secret here" |
(optional)TRANSFER_TOKEN_SALT | "unique user-generated secret here" |
Click Next.
When the preceding steps are completed DigitalOcean should automatically try to build and deploy the application. The Strapi admin panel is accessed at {your App domain}/admin
once the application is successfully deployed.
DigitalOcean managed databases are a production-scale database solution for a deployed Strapi application. Switching from a development database to a managed database requires modifying the Strapi application and modifying the settings on DigitalOcean:
pg-connection-string
dependency,database
configuration file,When a managed database is attached to a Strapi application, the connection parameters are passed directly from the database to the application .yaml
file (App Spec in the DigitalOcean settings). This requires a modification to the config/env/production/database
file and the addition of the pg-connection-string
dependency.
To add the pg-connection-string
dependency navigate to the project directory and install pg-connection-string
:
With yarn:
yarn add pg-connection-string
With npm:
npm install pg-connection-string
To switch to a managed database modify the config/env/production/database
file to be able to parse the DATABASE_URL
:
1// path: /config/env/production/database.ts
2
3const parse = require("pg-connection-string").parse;
4
5const { host, port, database, user, password } = parse(
6 process.env.DATABASE_URL
7 );
8
9export default ({ env }) => ({
10 connection: {
11 client: 'postgres',
12 connection: {
13 host,
14 port,
15 database,
16 user,
17 password,
18 ssl: {
19 ca: env('DATABASE_CA'),
20 },
21 },
22 debug: false,
23 },
24});
Changing the settings on the DigitalOcean App Platform to incorporate a managed database requires creating and attaching the database to an existing Strapi application. Additionally, the database environment variables must be removed, as managed databases propagate the connection properties automatically.
After creating the managed database, navigate to the application page:
Remove the previously added 'DATABASE_*' global variables added for connecting to the dev database, then set the following environment variables, inserting your database name (e.g. db-postgresql-nyc3-1234
) in place of dbClusterName
:
Variable name | Value |
---|---|
DATABASE_URL | ${dbClusterName.DATABASE_URL} |
DATABASE_CA | ${dbClusterName.CA_CERT} |
After attaching the database, DigitalOcean will attempt to auto-deploy the application. If the deployment is successful a link to the application will be provided at the top of the application page.