Skip to content

Environment Variables

Arthu Santiago edited this page Nov 30, 2026 · 2 revisions

Environment Variables

KeyAnyWhere reads its secrets and its database access from environment variables, never from a versioned file. In production, they live in a single file outside the project, /etc/keyanywhere/keyanywhere.env, which both Apache and the command line read. This page creates that file, connects Apache to it, installs the kaw-cake command that runs the CakePHP CLI with it, and ends with the reference of every variable.

Important

Don't use config/.env in production. It exists for development only: it keeps the secrets inside the project folder, and its presence is one of the signals KeyAnyWhere uses to detect a development environment.

Before you start

The code must be installed as in Installing KeyAnyWhere, and you need the password of the kaw database user created in Preparing the Server.

1. Create the environment file

Warning

Upgrading an existing installation? Don't run the block below: it generates new keys, and a new KEY_CRIPTOGRAFIC makes every stored secret unreadable. Write the file by hand with the values you already use, in the same format.

This step asks for the database password (typing is hidden), generates the three security keys and writes everything to the file. Only root can change it, and only the group www-data (Apache) can read it.

Type the database password of the kaw user (avoid quotes in it):

read -rsp 'Database password: ' KAW_DB_PASSWORD; echo

Create the file:

sudo install -d -m 750 -o root -g www-data /etc/keyanywhere
sudo tee /etc/keyanywhere/keyanywhere.env > /dev/null <<EOF
APP_DEFAULT_LOCALE='pt_BR'
APP_DEFAULT_TIMEZONE='America/Sao_Paulo'
SECURITY_SALT='$(php -r 'echo bin2hex(random_bytes(32));')'
KEY_CRIPTOGRAFIC='$(php -r 'echo sodium_bin2hex(sodium_crypto_secretbox_keygen());')'
CONFIG_INICIAL_TOKEN='$(php -r 'echo bin2hex(random_bytes(32));')'
DB_DEFAULT_HOST='127.0.0.1'
DB_DEFAULT_PORT='5432'
DB_DEFAULT_USER='kaw'
DB_DEFAULT_PASSWORD='$KAW_DB_PASSWORD'
DB_DEFAULT_NAME='keyanywhere'
EOF
sudo chown root:www-data /etc/keyanywhere/keyanywhere.env && sudo chmod 640 /etc/keyanywhere/keyanywhere.env
unset KAW_DB_PASSWORD

The time zone and language above are only the defaults until the administrator picks them in Configurações gerais (General settings); see Administration.

2. Save a copy of the keys

Now is the moment to copy KEY_CRIPTOGRAFIC (and the other two keys) to the safe place you picked in the Production Overview. Without KEY_CRIPTOGRAFIC, no backup can be read.

sudo grep -E '^(KEY_CRIPTOGRAFIC|SECURITY_SALT|CONFIG_INICIAL_TOKEN)=' /etc/keyanywhere/keyanywhere.env

3. Give the variables to Apache

On Debian, Apache reads /etc/apache2/envvars when it starts. This step makes that file load the KeyAnyWhere variables, so PHP sees them in every request.

echo 'set -a; . /etc/keyanywhere/keyanywhere.env; set +a' | sudo tee -a /etc/apache2/envvars > /dev/null
sudo systemctl restart apache2

4. Install the kaw-cake command

The CakePHP CLI (bin/cake) runs outside Apache, so it doesn't see the variables above. kaw-cake is a small script that loads the file and then runs bin/cake inside the application folder. Always run it as www-data, which is the user allowed to read the file and the one that owns the cache files the CLI creates.

sudo tee /usr/local/bin/kaw-cake > /dev/null <<'EOF'
#!/bin/sh
# Runs the KeyAnyWhere CakePHP CLI with the production environment variables.
set -a
. /etc/keyanywhere/keyanywhere.env
set +a
cd /var/www/keyanywhere || exit 1
exec bin/cake "$@"
EOF
sudo chmod 755 /usr/local/bin/kaw-cake

5. Check that it worked

This command runs a CakePHP command through kaw-cake. It only works if the variables load and the salt is long enough.

sudo -u www-data kaw-cake cache list

The output lists the cache configurations (default, _cake_translations_, …). An error that mentions Security.salt means the file wasn't read; check the path and the permissions from step 1.

Reference

All the variables KeyAnyWhere reads, where each one is read, and whether production needs it. Variables not set fall back to the defaults in config/app.php.

Application and secrets

Variable Required Default Purpose
DEBUG no false Read only by a config/app_local.php made from the development example. true shows errors on screen and turns on development behavior. Never use it in production, where debug stays off as set in config/app.php.
SECURITY_SALT yes — Signs cookies and CSRF tokens. At least 32 characters. See Generating Security Keys.
KEY_CRIPTOGRAFIC yes — Encryption key for every stored secret. Never change it.
CONFIG_INICIAL_TOKEN until the first-run setup is done — Opens the first-run setup. At least 32 characters.
APP_DEFAULT_LOCALE no pt_BR Language until the administrator picks one. pt_BR or es.
APP_DEFAULT_TIMEZONE no America/Campo_Grande Time zone until the administrator picks one. Any PHP time zone.
APP_ENCODING no UTF-8 Character encoding. Leave it as is.

Database

Variable Required Default Purpose
DB_DEFAULT_HOST yes — PostgreSQL host.
DB_DEFAULT_PORT no 5432 PostgreSQL port.
DB_DEFAULT_USER yes — Database user.
DB_DEFAULT_PASSWORD yes — Database password.
DB_DEFAULT_NAME yes — Database name.
DATABASE_URL no — The whole connection as a URL, postgres://user:password@host:5432/name. When set, it wins over the DB_DEFAULT_* variables.
DB_TEST_HOST, DB_TEST_PORT, DB_TEST_USER, DB_TEST_PASSWORD, DB_TEST_NAME, DB_TEST_TIMEZONE development only — Database used by the test suite. See Running Tests.

Cache, e-mail and logs (optional)

Variable Default Purpose
CACHE_DEFAULT_URL, CACHE_CAKECORE_URL, CACHE_CAKEMODEL_URL file cache in tmp/cache/ Cache configured as a URL (DSN), for example to use Redis. Format in the CakePHP book.
EMAIL_TRANSPORT_DEFAULT_HOST, EMAIL_TRANSPORT_DEFAULT_PORT (465), EMAIL_TRANSPORT_DEFAULT_TLS (false), EMAIL_TRANSPORT_DEFAULT_USER, EMAIL_TRANSPORT_DEFAULT_PASSWORD, EMAIL_DEFAULT_FROM — SMTP access. The core doesn't send e-mail yet; plugins may. Port 465 uses the ssl:// prefix on the host and TLS=false; port 587 uses no prefix and TLS=true.
LOG_QUERIES_URL — SQL query log configured as a URL. It writes only when the connection has 'log' => true.

Development only

Variable Purpose
DESENVOLVIMENTO_IGNORA_2FA true makes login accept any 2FA code, only in a development environment. Deliberately absent from config/.env.example. See Development Environment.

Next step

Configuration Files

Clone this wiki locally