Issue with deploying Strapi to DigitalOcean

System Information
  • Strapi Version: 4.5.6
  • Operating System:
  • Database: PostgreSQL
  • Node Version: 18.12.1
  • Yarn Version: 1.22.19

Hello!

I’ve been trying to deploy Strapi to a nginx VPS run on ubuntu for the past three days but have continuously run into a CORS issue that I have not been able to solve. I believe it has something to do with SSL certificates but I just can’t figure it out.

I regretfully gave up on this idea and decided to follow the tutorial posted on YouTube by Strapi for deployment to DigitalOcean: https://www.youtube.com/watch?v=tnGqqUzzh6U

I have followed this tutorial exactly and have now run into this error from DigitalOcean:

2023-01-15T22:59:36.825454082Z yarn run v1.22.19
2023-01-15T22:59:36.878302113Z $ strapi start
2023-01-15T22:59:41.230215390Z [2023-01-15 22:59:41.228] e[34mdebuge[39m: :no_entry: Server wasn’t able to start properly.
2023-01-15T22:59:41.230975044Z [2023-01-15 22:59:41.230] e[31merrore[39m: self-signed certificate in certificate chain
2023-01-15T22:59:41.230996037Z Error: self-signed certificate in certificate chain
2023-01-15T22:59:41.231002911Z at TLSSocket.onConnectSecure (node:_tls_wrap:1532:34)
2023-01-15T22:59:41.231009727Z at TLSSocket.emit (node:events:527:28)
2023-01-15T22:59:41.231015245Z at TLSSocket.emit (node:domain:475:12)
2023-01-15T22:59:41.231019777Z at TLSSocket._finishInit (node:_tls_wrap:946:8)
2023-01-15T22:59:41.231023994Z at TLSWrap.ssl.onhandshakedone (node:_tls_wrap:727:12)
2023-01-15T22:59:41.256862759Z error Command failed with exit code 1.
2023-01-15T22:59:41.257082771Z info Visit yarn run | Yarn for documentation about this command.

I have no idea how to solve this and would appreciate any help/advice/direction to relevant documentation or resources to assist.

Thanks in advance!


Hey,
I didn’t watch the tutorial, but I suppose that the problem raises when strapi tries to make a secure connection to the DB. The DB in App Platform provides its own self-signed SSL certificate. I believe that by default Strapi only uses the Node’s certificate authorities and so the connection fails.

You need to tell Strapi to trust the self-signed certificate. You can do that in the config:

// in config/database.js`

module.exports = ({ env }) => ({
  connection: {
  [...]
    connection: {
      [...]
      ssl: {
        ca: env("CA_CERT"),
      },
    },
  },
});

The CA_CERT is a bindable variable provided by the Database resource. You need to inject it into the app by adding an environment variable with a value of ${database.CA_CERT}.

From the docs:

const fs = require('fs');
module.exports = ({ env }) => ({
  connection: {
    client: 'postgres',
    connection: {
      ...
      ssl: {
        ca: fs.readFileSync(`${__dirname}/path/to/your/ca-certificate.crt`).toString(),
      },
    },
  },
});