This guide covers configuration management in CFWheels, including working with environment variables, settings files, and the CLI tools for managing configuration.
Overview
CFWheels provides flexible configuration management through:
Environment-specific settings files
Environment variables (.env files)
CLI commands for configuration management
Environment Variables
CFWheels supports two formats for .env files:
Copy # Database Configuration
DB_HOST = localhost
DB_PORT = 3306
DB_NAME = myapp_development
DB_USER = wheels
DB_PASSWORD = secretpassword
# Application Settings
WHEELS_ENV = development
RELOAD_PASSWORD = myreloadpassword
SECRET_KEY = a1b2c3d4e5f6g7h8i9j0
# Feature Flags
DEBUG_MODE = true
CACHE_ENABLED = false
Copy {
"DB_HOST": "localhost",
"DB_PORT": 3306,
"DB_NAME": "myapp_development",
"DB_USER": "wheels",
"DB_PASSWORD": "secretpassword",
"WHEELS_ENV": "development",
"DEBUG_MODE": true
}
Environment-Specific Files
CFWheels automatically loads environment-specific .env files:
.env
- Base configuration (always loaded first)
.env.{environment}
- Environment-specific overrides (loaded second)
Example structure:
Copy myapp/
├── .env # Base configuration
├── .env.development # Development overrides
├── .env.testing # Testing overrides
├── .env.production # Production settings
└── .gitignore # MUST exclude .env files!
Variable Interpolation
Use ${VAR}
syntax to reference other variables:
Copy # .env
APP_NAME=MyWheelsApp
APP_ENV=development
# Database URLs with interpolation
DB_HOST=localhost
DB_PORT=3306
DB_NAME=${APP_NAME}_${APP_ENV}
DB_URL=mysql://${DB_HOST}:${DB_PORT}/${DB_NAME}
# API endpoints
API_BASE_URL=https://api.example.com
API_USERS_URL=${API_BASE_URL}/users
API_ORDERS_URL=${API_BASE_URL}/orders
Type Casting
CFWheels automatically casts certain values:
Copy # Booleans (cast to true/false)
DEBUG_MODE=true
CACHE_ENABLED=false
# Numbers (cast to numeric values)
MAX_CONNECTIONS=100
CACHE_TTL=3600
REQUEST_TIMEOUT=30
# Strings (remain as strings)
APP_NAME=MyApp
API_KEY=abc123def456
Using Environment Variables in Settings
Access environment variables in your settings files:
Copy // config/settings.cfm
// Using application.env struct
set(dataSourceName = application.env['DB_NAME']);
set(dataSourceUserName = application.env['DB_USER']);
set(dataSourcePassword = application.env['DB_PASSWORD']);
// With defaults
set(cacheQueries = application.env['CACHE_ENABLED'] ?: false);
set(reloadPassword = application.env['RELOAD_PASSWORD'] ?: 'defaultpassword');
// Environment-specific logic
if (application.env['WHEELS_ENV'] == 'production') {
set(showDebugInformation = false);
set(cacheFileChecking = true);
}
Configuration Files
Directory Structure
Copy config/
├── settings.cfm # Base configuration
├── development/
│ └── settings.cfm # Development overrides
├── testing/
│ └── settings.cfm # Testing overrides
├── production/
│ └── settings.cfm # Production overrides
└── maintenance/
└── settings.cfm # Maintenance mode settings
Settings Precedence
Settings are loaded in this order (later overrides earlier):
config/{environment}/settings.cfm
Common Configuration Patterns
Database Configuration
Copy // config/settings.cfm
set(dataSourceName = application.env['DB_NAME'] ?: 'wheelstutorial');
set(dataSourceUserName = application.env['DB_USER'] ?: 'root');
set(dataSourcePassword = application.env['DB_PASSWORD'] ?: '');
Environment-Specific Settings
Copy // config/production/settings.cfm
// Production optimizations
set(cacheQueries = true);
set(cachePartials = true);
set(cachePages = true);
set(cacheActions = true);
set(cacheImages = true);
set(cacheModelConfig = true);
set(cacheControllerConfig = true);
set(cacheRoutes = true);
set(cacheDatabaseSchema = true);
set(cacheFileChecking = false);
// Security
set(showDebugInformation = false);
set(showErrorInformation = false);
// Error handling
set(sendEmailOnError = true);
set(errorEmailAddress = application.env['ERROR_EMAIL']);
CLI Configuration Commands
Dumping Configuration
Export current configuration:
Copy # View current config (masked)
wheels config dump
# Export production config as JSON
wheels config dump production --format=json --output=prod-config.json
# Export as .env format
wheels config dump --format=env --output=config.env
# Export without masking (careful!)
wheels config dump --no-mask
Checking Configuration
Validate configuration for issues:
Copy # Basic check
wheels config check
# Check production with fixes
wheels config check production --fix
# Verbose output with suggestions
wheels config check --verbose
Common checks performed:
Missing required settings
Hardcoded sensitive values
Production security settings
Comparing Environments
Find differences between environments:
Copy # Full comparison
wheels config diff development production
# Show only differences
wheels config diff development production --changes-only
# Export as JSON
wheels config diff testing production --format=json > diff.json
Managing Secrets
Generate secure secrets:
Copy # Generate and display
wheels secret
wheels secret --type=base64 --length=48
# Save to .env
wheels secret --save-to-env=SECRET_KEY
wheels secret --type=uuid --save-to-env=API_KEY
# Multiple secrets
wheels secret --save-to-env=SESSION_SECRET
wheels secret --save-to-env=CSRF_TOKEN
wheels secret --save-to-env=ENCRYPTION_KEY
Environment Variable Management
Setting Variables
Copy # Set single variable
wheels env set DB_HOST=localhost
# Set multiple
wheels env set DB_HOST=localhost DB_PORT=3306 DB_NAME=myapp
# Update production file
wheels env set --file=.env.production API_URL=https://api.example.com
Validating Files
Copy # Basic validation
wheels env validate
# Check required variables
wheels env validate --required=DB_HOST,DB_USER,DB_PASSWORD
# Validate production
wheels env validate --file=.env.production --verbose
Merging Files
Copy # Merge for deployment
wheels env merge .env .env.production --output=.env.deployed
# Preview merge
wheels env merge .env.defaults .env.local --dry-run
Security Best Practices
1. Never Commit Secrets
Always add .env files to .gitignore:
Copy # Environment files
.env
.env.*
!.env.example
!.env.defaults
2. Use Strong Secrets
Generate cryptographically secure values:
Copy # For session secrets
wheels secret --type=hex --length=64 --save-to-env=SESSION_SECRET
# For API keys
wheels secret --type=base64 --length=48 --save-to-env=API_SECRET
# For passwords
wheels secret --type=alphanumeric --length=32
3. Validate Production Config
Run checks before deployment:
Copy # Full production check
wheels config check production --verbose
# Required checks
wheels env validate --file=.env.production \
--required=DB_HOST,DB_USER,DB_PASSWORD,SECRET_KEY,RELOAD_PASSWORD
4. Mask Sensitive Output
Always use masking in logs/output:
Copy // In your code
writeOutput("Database: #application.env['DB_NAME']#");
writeOutput("Password: ***MASKED***"); // Never output passwords
5. Environment-Specific Secrets
Use different secrets per environment:
Copy # .env.development
SECRET_KEY=dev_secret_key_only_for_local
# .env.production
SECRET_KEY=prod_0a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0
Common Patterns
Multi-Environment Setup
Copy # 1. Create base config
wheels env set WHEELS_ENV=development DB_NAME=myapp_dev
# 2. Create production config
wheels env set --file=.env.production \
WHEELS_ENV=production \
DB_NAME=myapp_prod \
DB_HOST=prod.database.com
# 3. Create testing config
wheels env set --file=.env.testing \
WHEELS_ENV=testing \
DB_NAME=myapp_test
# 4. Validate all
wheels env validate
wheels env validate --file=.env.production
wheels env validate --file=.env.testing
Deployment Configuration
Copy # 1. Merge configs for deployment
wheels env merge .env.defaults .env.production --output=.env.deploy
# 2. Validate merged config
wheels env validate --file=.env.deploy \
--required=DB_HOST,DB_USER,DB_PASSWORD,SECRET_KEY
# 3. Check security
wheels config check production --verbose
Local Development
Copy # 1. Copy example file
cp .env.example .env
# 2. Set local values
wheels env set DB_HOST=localhost DB_USER=root DB_PASSWORD=
# 3. Generate secrets
wheels secret --save-to-env=SECRET_KEY
wheels secret --save-to-env=RELOAD_PASSWORD
# 4. Validate
wheels env validate
Troubleshooting
Environment Variables Not Loading
Check file exists and is readable
Verify format (properties vs JSON)
Wrong Environment Loading
Check WHEELS_ENV variable:
Copy wheels env show --key=WHEELS_ENV
Verify environment detection order:
System environment WHEELS_ENV
Interpolation Not Working
Ensure variables are defined before use
Check syntax: ${VAR_NAME}
Maximum 10 interpolation passes (prevent loops)
Sensitive Values Exposed
Run security check:
Copy wheels config check --verbose
Move hardcoded values to .env
Use application.env
references
Best Practices Summary
Use .env files for all environment-specific values
Never commit secrets - use .gitignore
Generate strong secrets with wheels secret
Validate configuration before deployment
Use interpolation to reduce duplication
Environment-specific files for overrides
Check security regularly with CLI tools
Document required variables in .env.example
Mask sensitive values in output
Test configuration changes in development first