Skip to main content

Strapi

Recipe Features Explained

Let's breakdown the recipe to deploy a new Strapi site. All examples here uses yarn.

See Full Deployment Script
source: clear
features:
- node lts
nginx:
root: public_html/public
passenger:
enabled: "on"
app_env: development
app_start_command: env PORT=$PORT yarn start
locations:
- match: /admin/
alias: public_html/build/
commands:
- yarn create strapi-app . --quickstart --no-run --skip-cloud
- echo JWT_SECRET=$(openssl rand -base64 24) > .env
- echo APP_KEYS=$(openssl rand -base64 24) >> .env
- echo ADMIN_JWT_SECRET=$(openssl rand -base64 24) >> .env
- echo API_TOKEN_SALT=$(openssl rand -base64 24) >> .env
- echo STRAPI_ADMIN_BACKEND_URL=https://${DOMAIN} >> .env
- STRAPI_ADMIN_BACKEND_URL=https://${DOMAIN} yarn build
source: clear
features:
- node lts

This script clears ~/public_html and install node LTS version.

nginx:
root: public_html/public
passenger:
enabled: "on"
app_env: development
app_start_command: env PORT=$PORT yarn start
locations:
- match: /admin/
alias: public_html/build/

This script declare NGINX Configuration. It means three thing:

  1. app_start_command: env PORT=$PORT yarn start means start using yarn start.

  2. app_env: development this set NODE_ENV to development. Ideally we start from development env, then upgrade to production when it's ready.

  3. match: /admin/ means for /admin pages, do alias: public_html/build/. This bypass proxy for built client admin pages.

commands:
- yarn create strapi-app . --quickstart --no-run --skip-cloud

This script bootstrap a new strapi via create-strapi-app cli.

commands:
- echo JWT_SECRET=$(openssl rand -base64 24) > .env
- echo APP_KEYS=$(openssl rand -base64 24) >> .env
- echo ADMIN_JWT_SECRET=$(openssl rand -base64 24) >> .env
- echo API_TOKEN_SALT=$(openssl rand -base64 24) >> .env
- echo STRAPI_ADMIN_BACKEND_URL=https://${DOMAIN} >> .env

This sets up default envar with secrets and the backend URL.

commands:
- STRAPI_ADMIN_BACKEND_URL=https://${DOMAIN} yarn build

This build the admin interface. Unfortunately we don't know why we have to put STRAPI_ADMIN_BACKEND_URL there.

Using Databases

With default configuration, the database is saved to .tmp/data.db. Not good since it can be accidentally deleted. Try moving it out to home dir.

commands:
- echo DATABASE_FILENAME=$HOME/data.db >> env
- restart

To use PostgreSQL instead, use this:

features:
- postgresql
commands:
- echo DATABASE_CLIENT=postgres >> .env
- echo DATABASE_URL=postgresql://$USERNAME:$PASSWORD@localhost/$DATABASE >> .env
- yarn install pg
- restart

Or to use MySQL instead, use this:

features:
- mysql
commands:
- echo DATABASE_CLIENT=mysql2 >> .env
- echo DATABASE_HOST=localhost >> .env
- echo DATABASE_NAME=$DATABASE >> .env
- echo DATABASE_USERNAME=$USERNAME >> .env
- echo DATABASE_PASSWORD=$PASSWORD >> .env
- yarn install mysql2
- restart

Fix Session Issues

Go to config/server.js and set proxy: true.

config/server.js
module.exports = ({ env }) => ({
// ...rest config...
proxy: true,
});

This is because NGINX proxies your request to HTTP channel so strapi won't know if visitor visits HTTPS unless you tell Strapi to read X-Forwarded-Proto via this settings. If you don't enable this, you'll get Cannot send secure cookie over unencrypted connection when you set NODE_ENV to production.

Fix Infinite Redirection when Logging in to Panel

You need to set STRAPI_ADMIN_BACKEND_URL then rebuild the frontend.

commands:
- echo STRAPI_ADMIN_BACKEND_URL=https://${DOMAIN} >> .env
- yarn build

Accessing Content Builder in the Server

You need to open VS Code and run yarn dev. Before that, make sure to unset STRAPI_ADMIN_BACKEND_URL from .env. VS Code will set up a local port forwarding so localhost:1337 can be opened directly in your browser.