Skip to content

Installing Directus as a headless CMS

Operator install of Directus on Ubuntu with PostgreSQL, Redis for cache, S3 for assets, and the env vars and policies that keep it production-grade.

Directus is a “data-first” headless CMS: point it at an existing or empty SQL database and it generates a REST and GraphQL API plus a polished admin panel on top. This article runs through a clean Directus install on Ubuntu 22.04 against Postgres, with Redis for cache and pub/sub, S3 for assets, and the env vars that turn a fresh npm init directus-project into something you can leave running unattended for clients.

How to verify

A correctly installed Directus deployment shows:

curl -sI https://content.example.org/admin/ | head -1
# HTTP/2 200

curl -s https://content.example.org/server/health | jq '.status'
# "ok"

pm2 status
# │ 0  │ directus │ online │

sudo -u postgres psql -d directus -c "\dt directus_*" | head
# directus_collections | table | directus
# directus_fields      | table | directus
# directus_users       | table | directus

redis-cli -h 127.0.0.1 -n 4 dbsize
# (cache db should grow after first warm)

If /server/health returns {"status":"error","reason":"redis"}, the cache redis is unreachable — fix that before declaring the install good.

What’s happening

Directus runs as a Node.js application (Fastify under the hood). At startup it reads the database, looks for its 24 system tables (directus_collections, directus_users, directus_permissions, etc.), creates them if missing, then introspects the rest of the schema to build a CRUD API and admin UI for every user collection. You can let Directus manage everything from the admin (creating collections through the UI writes to directus_collections and runs ALTER TABLE against your DB) or you can keep schema in migrations and let Directus only read.

Two modes of authentication run in parallel: cookie sessions for the admin panel and JWTs for the API. Permissions are role-based — every operation maps to (role, collection, action, fields). This means a “Public” role with no permissions can browse nothing; explicitly grant read per collection. The admin panel quietly does this through the UI; if you build collections through SQL migrations directly you must seed the permissions or the API returns 401 to public clients.

Assets (files uploaded through the admin) live by default on the local disk under uploads/. Production almost always pushes them to S3, Spaces, GCS, or Azure Blob through the storage drivers built into Directus. The metadata (filename, size, type, folder, hash) stays in directus_files; the blobs go to the configured driver.

The procedure

  1. Install Node 20, Postgres, Redis, Nginx.

    curl -fsSL https://deb.nodesource.com/setup_20.x | sudo bash -
    sudo apt install -y nodejs postgresql redis-server nginx
    sudo npm install -g pm2
  2. Provision the database and user.

    DBPASS=$(openssl rand -hex 24)
    sudo -u postgres psql <<SQL
    CREATE USER directus WITH PASSWORD '${DBPASS}';
    CREATE DATABASE directus OWNER directus;
    SQL
  3. Create the project.

    sudo useradd -m -s /bin/bash directus
    sudo -u directus -- bash -lc '
      cd /home/directus
      npm init directus-project@latest cms -- --skipInstall=false
    '

    The interactive prompts ask for DB driver (pg), host/port/user/password, admin email and password. Answer them, or use the env-based init (next step) to skip the prompts.

  4. Write the production .env. Generate the secret keys and pin everything:

    sudo -u directus -- bash -lc '
      cat > /home/directus/cms/.env <<EOF
    HOST=127.0.0.1
    PORT=8055
    PUBLIC_URL=https://content.example.org
    KEY=$(openssl rand -hex 32)
    SECRET=$(openssl rand -hex 32)
    DB_CLIENT=pg
    DB_HOST=127.0.0.1
    DB_PORT=5432
    DB_DATABASE=directus
    DB_USER=directus
    DB_PASSWORD='"${DBPASS}"'
    CACHE_ENABLED=true
    CACHE_STORE=redis
    REDIS=redis://127.0.0.1:6379/4
    STORAGE_LOCATIONS=s3
    STORAGE_S3_DRIVER=s3
    STORAGE_S3_KEY=AKIA...
    STORAGE_S3_SECRET=...
    STORAGE_S3_BUCKET=directus-assets-prod
    STORAGE_S3_REGION=us-east-1
    STORAGE_S3_ENDPOINT=https://s3.us-east-1.amazonaws.com
    EMAIL_TRANSPORT=smtp
    EMAIL_SMTP_HOST=smtp.example.org
    EMAIL_SMTP_PORT=587
    EMAIL_SMTP_USER=directus
    EMAIL_SMTP_PASSWORD=...
    EMAIL_FROM=cms@example.org
    ACCESS_TOKEN_TTL=15m
    REFRESH_TOKEN_TTL=7d
    RATE_LIMITER_ENABLED=true
    RATE_LIMITER_STORE=redis
    EOF
      chmod 600 /home/directus/cms/.env
    '
  5. Run the bootstrap. This creates the system tables and the admin user:

    sudo -u directus -- bash -lc '
      cd /home/directus/cms
      npx directus bootstrap
    '
  6. Start under PM2.

    sudo -u directus -- bash -lc '
      cd /home/directus/cms
      pm2 start "npx directus start" --name directus
      pm2 save
    '
    sudo env PATH=$PATH:/usr/bin pm2 startup systemd -u directus --hp /home/directus
  7. Nginx in front.

    server {
      listen 443 ssl http2;
      server_name content.example.org;
      ssl_certificate /etc/letsencrypt/live/content.example.org/fullchain.pem;
      ssl_certificate_key /etc/letsencrypt/live/content.example.org/privkey.pem;
      client_max_body_size 200m;
      location / {
        proxy_pass http://127.0.0.1:8055;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
      }
    }
  8. Define a public role and policies. From the admin (or via the API) create a “Public” role with read on the collections that should be publicly queryable. Directus 11 introduced a “Policies” abstraction over roles — use policies if you have more than one tenant.

Common pitfalls

  • The KEY and SECRET env vars are not interchangeable — KEY is per-instance identifier (used to derive cache keys), SECRET is what signs JWTs. Rotate both together and you invalidate every active session.
  • Directus uses directus_files and directus_settings for cross-cutting things — exclude these from the “Public” role or anonymous clients can read your S3 keys via folder metadata.
  • The auto-introspection mode rewrites field types when you change them in the admin. Working through the UI on a schema that’s also under Knex migrations leads to drift; pick one source of truth.
  • Webhooks fire synchronously inside the request — a slow third-party endpoint blocks the editor’s Save button until it times out. Wrap external calls in a Flow with a queue.
  • Out-of-the-box Directus has no built-in rate limiter on /auth/login — turn on the rate limiter or add one in front (Nginx limit_req) before exposing the admin to the internet.

For teams that need a SQL-first headless CMS in production, Stack Harbor operates the Directus stack — Postgres backed up and PITR-capable, Redis monitored, S3 lifecycle on assets, role/policy audits at release — so the content team only sees the admin. That model lives under managed operations, with related Strapi notes alongside.