Setup LUYA as Headless API on DigitalOcean

The tutorial below is an example of how you can setup LUYA as a Headless CMS API on DigitalOcean. Since DigitalOcean provides its App Platform its very easy to run a LUYA application with just a few clicks.

Prepare LUYA for DigitalOcean

In our example we use the LUYA HEADLESS CMS KICKSTARTER package as project base.

1.) Add env.php

Add the env.php to your GIT system, as this file needs to be deployed on the server. Example content could be:

$config = require 'config.php'; 

$env = getenv('ENV');

if (empty($env)) {
    $env = \luya\Config::ENV_LOCAL;

return $config->toArray([$env]);

the ENV env variable will be used later to correctly assign the production env config.

2.) Production Config

Add some ENV variables for your production environment, for example the database strings and adjust the request component so it will work behind the load balancer of DigitalOcean:

$config->env(Config::ENV_PROD, function(Config $config) {
    // database setup, which digital oceans based pgsql database
    $config->component('db', [
        'dsn' => getenv('DB_DSN'),
        'username' => getenv('DB_USERNAME'),
        'password' => getenv('DB_PASSWORD'),
        'enableSchemaCache' => true,
        'schemaCacheDuration' => 0,
    // ensure pgsql mutex
    $config->module('admin', [
        'queueMutexClass' => 'yii\mutex\PgsqlMutex',
    // ensure ip of the user is assigned correctly
    $config->webComponent('request', [
        'ipHeaders' => ['HTTP_DO_CONNECTING_IP'],
        'trustedHosts' => [

3.) Extensions

Since DigitalOcean will deploy your app using heroku, the PHP extensions can be defined in your composer.json like following:

"require": {
        "ext-gd": "*"

This will ensure the gd php extension is installed. See the [](https://full list of heroku php extensions).

Setup DigitalOcean App

The DigitalOcean Setup requires a Database (which is pgsql / postgres) and an app Container. In my example i have connected the App trough GitLab, this ensures the app gets deployed for every commit.

1.) Create App


2.) Select the Repository

Select the repository form GitLab or GitHub you like to connect. Whenever you commit, the app will be build automatically.

3.) Define a Name and Region

4.) Configure

The most important step is to configure your app. Add the ENV variables we have previously defined in our configs and change the build and run commands:

**since 13.1.2020 the composer install command is not required anymore and will throw an exception instead. As DigitalOceans App Platform will install those dependencies for you.

  • ENV: prod
  • DB_DSN: pgsql:host=${db.HOSTNAME};port=${db.PORT};dbname=${db.DATABASE}

Add a database to your app:

The database ENV variables can be access trough the db name (db in our case) as prefix. So the postgre db password would be available trough ${db.PASSWORD}. This YouTube Tutorial provides more informations about ENV Variables

5.) Choose Plan and Launch App

6.) Run the setup commands

The deployment might run for a while, as on the first time the database needs to be created first. Here you can see the output of a successful build and run:

When the deployment was successful you can enter the console of the container and run the LUYA setup commands.

That’s it. Visit your apps URL and you are ready to go.