Skip to content

wp-config.php Without Credentials: WordPress in Containers

Adityo Guni Waluyo

A wp-config.php that reads database credentials, salts, and URLs from compose environment variables, with no literal secrets in the Git repository.

TL;DR

Hardcoded database credentials and salts in wp-config.php shouldn't live in Git; move them to compose environment variables instead. A small helper function reads those variables into define calls, honoring the same contract the official Docker image uses, while a must-use plugin handles hardening like disabling XML-RPC. Verify everything with wp-cli, and keep only custom code tracked.

One wp-config.php file, and inside it a row of literal values: the database password, AUTH_KEY, down to LOGGED_IN_SALT. All of them get committed to the Git repository of a containerized WordPress stack. In the same stack, the WordPress core version is already owned by the Docker image, not by a configuration file. Two sources of truth for one installation.

The natural first assumption: official environment variables such as WORDPRESS_DB_HOST only work on the official image, because the mechanism lives in its entrypoint. The official image documentation describes it more subtly. Those variables are honored "by a custom wp-config.php implementation" [2]. The contract belongs to the configuration file, not to the entrypoint. Any custom PHP-FPM image can honor the same contract without copying a single line from the official one.

The Env Contract Lives in wp-config.php

The conversion leaves behind one helper function and one series of define calls. Dev fallbacks are deliberately allowed to live in exactly one place:

<?php
// /var/www/html/wp-config.php — generic container example

if ( ! defined( 'ABSPATH' ) ) {
    http_response_code( 403 );
    exit;
}

function pras_env( string $key, string $default = '' ): string {
    $v = getenv( $key );
    return ( $v === false || $v === '' ) ? $default : $v;
}

define( 'DB_NAME',     pras_env( 'WORDPRESS_DB_NAME', pras_env( 'MARIADB_DATABASE', 'wordpress' ) ) );
define( 'DB_USER',     pras_env( 'WORDPRESS_DB_USER', pras_env( 'MARIADB_USER', 'wordpress' ) ) );
define( 'DB_PASSWORD', pras_env( 'WORDPRESS_DB_PASSWORD', pras_env( 'MARIADB_PASSWORD', 'dev-only-placeholder' ) ) );
define( 'DB_HOST',     pras_env( 'WORDPRESS_DB_HOST', 'mariadb:3306' ) );

// Salts: real values injected via env in production; the dev fallback is not a secret.
foreach ( array(
    'AUTH_KEY', 'SECURE_AUTH_KEY', 'LOGGED_IN_KEY', 'NONCE_KEY',
    'AUTH_SALT', 'SECURE_AUTH_SALT', 'LOGGED_IN_SALT', 'NONCE_SALT',
) as $salt_const ) {
    define( $salt_const, pras_env( $salt_const, 'dev-only-placeholder' ) );
}

$table_prefix = pras_env( 'WORDPRESS_TABLE_PREFIX', 'wp_' );

define( 'WP_HOME',    pras_env( 'WP_HOME', 'http://cms.example.com' ) );
define( 'WP_SITEURL', pras_env( 'WP_SITEURL', 'http://cms.example.com' ) );

define( 'WP_ENV', pras_env( 'WP_ENV', 'dev' ) );
define( 'WP_DEBUG', filter_var(
    pras_env( 'WP_DEBUG', WP_ENV === 'dev' ? '1' : '0' ),
    FILTER_VALIDATE_BOOLEAN
) );
define( 'WP_DEBUG_LOG',     WP_DEBUG );
define( 'WP_DEBUG_DISPLAY', false );
define( 'WPLANG',           'id_ID' );

define( 'DISALLOW_FILE_EDIT',         true );
define( 'AUTOMATIC_UPDATER_DISABLED', true );
define( 'WP_AUTO_UPDATE_CORE',        false );

if ( WP_ENV !== 'dev' ) {
    define( 'DISABLE_WP_CRON', true );
}

The pras_env() helper treats an empty string the same as an unset variable, so one missing env value cannot slip through silently as empty text. The database fallback chain stops at a dev value that is clearly not a secret; in production, compose injects the real values and the fallback is never touched.

WP_HOME and WP_SITEURL from the environment override the values in the wp_options table without writing them back to the database [1]. Changing domains means changing an env value, not running an update query. The eight salt constants are read from the environment the same way; the official image only fills in random default salts when other environment configuration is present [2].

WP_DEBUG is parsed through filter_var so the strings "0" or "false" from the environment genuinely become boolean false, and WP_DEBUG_DISPLAY is forced off so errors never reach the screen [1].

Automatic updates are disabled through AUTOMATIC_UPDATER_DISABLED plus WP_AUTO_UPDATE_CORE false; the documentation names sites with customizations or host-supplied updates as the reason [1]. In a container stack the case is even more concrete: the core is owned by the image, and an in-container update would be wiped at the next rebuild.

One conscious deviation from the WordPress standard: this file defines its own WP_ENV constant, while core ships WP_ENVIRONMENT_TYPE for environment labeling. The custom constant was chosen because its value drives compose logic and CLI-invoked wp-cron, not WordPress core behavior.

A mu-plugin for Headless Hardening

Application-level hardening goes into a must-use plugin so it runs before regular plugins load and cannot be switched off from the dashboard:

<?php
// wp-content/mu-plugins/pras-core.php

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

if ( ! defined( 'PRAS_REST_PREFIX' ) ) {
    define( 'PRAS_REST_PREFIX', 'pras/v1' );
}

// XML-RPC is never needed by a headless frontend.
add_filter( 'xmlrpc_enabled', '__return_false' );

// Remove the generator meta (version disclosure).
remove_action( 'wp_head', 'wp_generator' );
add_filter( 'the_generator', '__return_empty_string' );

PRAS_REST_PREFIX acts as a contract constant: the same REST namespace is consumed by the API plugin and the frontend. Disabling the xmlrpc_enabled filter follows the official guidance to turn XML-RPC off when it is unused [3]; the WordPress documentation notes that distributed brute-force attempts can overwhelm a site even when they fail [3]. The generator metadata is also removed so the WordPress version is not announced in the HTML. Custom REST plugin includes are loaded through a defensive glob, so boot never fatals just because one file was not staged yet.

A .gitignore entry completes the pattern: WordPress core plus downloaded plugins and themes are kept out of Git. Only custom code is tracked. The repository stops being an archive of a WordPress installation and becomes source code again.

Verifying the Boot with wp-cli

An env-only configuration only counts as working after it passes the following four checks, all from inside the container:

wp core install --url="http://cms.example.com" \
    --title="Example" --admin_user=<ADMIN_USER> \
    --admin_password=<ADMIN_PASSWORD> --admin_email=<ADMIN_EMAIL>
# Success: WordPress installed successfully.

wp option get siteurl                              # http://cms.example.com
curl -s http://cms.example.com/wp-json/ | head -c 120   # JSON response
curl -sI http://cms.example.com/wp-login.php | head -1  # HTTP/1.1 200 OK

wp core install runs the standard WordPress installation process [4]; success means the database env chain is complete from compose down to define. wp option get siteurl must return the same URL as the WP_HOME env, not the dev fallback. The wp-json endpoint must return JSON, proof that the mu-plugin does not block core REST routes. A login page answering HTTP 200 closes the chain: the application is alive and the hardening did not lock the legitimate door.

The end state: the configuration file stops holding secrets and becomes a deterministic translator between the environment and WordPress. Sensitive values live in compose env, the image owns the core version, and Git carries only the code humans actually wrote.

[1] https://developer.wordpress.org/advanced-administration/wordpress/wp-config [2] https://hub.docker.com/_/wordpress [3] https://wordpress.org/documentation/article/brute-force-attacks [4] https://developer.wordpress.org/cli/commands/core

Sources

Related articles