Skip to content
prod e051e98
Browse

5 · Wire .env templates

Objective — replace the vendor’s single .env with a three-environment template set (local / staging / production), preserving the original first, and generate a unique APP_KEY for local.

The vendor ships a .env (or .env.example). You’re about to replace it with a three-environment template set — but first preserve the original, because it often documents required keys you’d otherwise miss.

flowchart LR
V["vendor .env /<br/>.env.example"] -->|back up| Vault["project vault<br/>(gitignored)"]
Vault --> T["lay down<br/>.env.local / .staging / .production"]
T --> A["cp .env.local .env"]
A --> K["php artisan key:generate"]

Preserve the originals in the gitignored vault — your record of what the author expected.

  1. Copy the vendor env into the vault.

    Terminal window
    mkdir -p Admin-Local/1-Project/2-ProjectVault
    [ -f .env ] && cp .env Admin-Local/1-Project/2-ProjectVault/.env.vendor-original
    cp .env.example Admin-Local/1-Project/2-ProjectVault/.env.vendor-example
    # Expected: vendor .env (if present) and .env.example copied into the vault
    • ✅ The vendor’s original env files are preserved in Admin-Local/1-Project/2-ProjectVault/.

Create one template per environment in the vault. Keep real secrets out of git — only .env.example is ever tracked.

  1. Create the three templates from .env.example.

    Terminal window
    cp .env.example Admin-Local/1-Project/2-ProjectVault/.env.local
    cp .env.example Admin-Local/1-Project/2-ProjectVault/.env.staging
    cp .env.example Admin-Local/1-Project/2-ProjectVault/.env.production
    # Expected: three .env.<environment> files created in the vault
    • .env.local, .env.staging, .env.production exist in the vault.
  2. Customize each — at minimum APP_NAME, APP_ENV, APP_DEBUG, APP_URL, and the DB_* credentials created in Prerequisites.

    • ✅ Each template carries this project’s values, with secrets pasted only into the gitignored variants.
  1. Activate the local env and generate its key.

    Terminal window
    cp Admin-Local/1-Project/2-ProjectVault/.env.local .env
    php artisan key:generate
    # Expected: INFO Application key set successfully.
    • grep "^APP_KEY=base64:" .env returns a key line.

The same keys take different values per environment. The ones that matter most:

VariableLocalStagingProduction
APP_ENVlocalstagingproduction
APP_DEBUGtruefalsefalse
APP_URLhttps://app.testhttps://staging.x.comhttps://x.com
APP_KEYuniqueuniqueunique
LOG_LEVELdebuginfoerror
MAIL_MAILERlogsmtpsmtp
SESSION_SECURE_COOKIEfalsetruetrue

Do not mark this step done until every box below is checked.

  • 🤖 Vendor env backed up.env / .env.example copied to the vault.
  • 🔀 Templates created.env.local / .env.staging / .env.production exist in the vault.
  • 🤖 Local active + keyed.env active and a unique APP_KEY generated (grep "^APP_KEY=base64:" .env).
  • 👤 Off-local debug off — staging + production templates set APP_DEBUG=false.
  • 🤖 No secrets stagedgit status shows no real .env files staged.