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.
Background
Section titled “Background”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"]1. Back up the vendor env
Section titled “1. Back up the vendor env”Preserve the originals in the gitignored vault — your record of what the author expected.
-
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-originalcp .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/.
- ✅ The vendor’s original env files are preserved in
2. Lay down per-environment templates
Section titled “2. Lay down per-environment templates”Create one template per environment in the vault. Keep real secrets out of git — only .env.example is ever tracked.
-
Create the three templates from
.env.example.Terminal window cp .env.example Admin-Local/1-Project/2-ProjectVault/.env.localcp .env.example Admin-Local/1-Project/2-ProjectVault/.env.stagingcp .env.example Admin-Local/1-Project/2-ProjectVault/.env.production# Expected: three .env.<environment> files created in the vault- ✅
.env.local,.env.staging,.env.productionexist in the vault.
- ✅
-
Customize each — at minimum
APP_NAME,APP_ENV,APP_DEBUG,APP_URL, and theDB_*credentials created in Prerequisites.- ✅ Each template carries this project’s values, with secrets pasted only into the gitignored variants.
3. Activate local & generate the key
Section titled “3. Activate local & generate the key”-
Activate the local env and generate its key.
Terminal window cp Admin-Local/1-Project/2-ProjectVault/.env.local .envphp artisan key:generate# Expected: INFO Application key set successfully.- ✅
grep "^APP_KEY=base64:" .envreturns a key line.
- ✅
4. Per-environment values (reference)
Section titled “4. Per-environment values (reference)”The same keys take different values per environment. The ones that matter most:
| Variable | Local | Staging | Production |
|---|---|---|---|
APP_ENV | local | staging | production |
APP_DEBUG | true | false | false |
APP_URL | https://app.test | https://staging.x.com | https://x.com |
APP_KEY | unique | unique | unique |
LOG_LEVEL | debug | info | error |
MAIL_MAILER | log | smtp | smtp |
SESSION_SECURE_COOKIE | false | true | true |
Checklist
Section titled “Checklist”Do not mark this step done until every box below is checked.
- 🤖 Vendor env backed up —
.env/.env.examplecopied to the vault. - 🔀 Templates created —
.env.local/.env.staging/.env.productionexist in the vault. - 🤖 Local active + keyed —
.envactive and a uniqueAPP_KEYgenerated (grep "^APP_KEY=base64:" .env). - 👤 Off-local debug off — staging + production templates set
APP_DEBUG=false. - 🤖 No secrets staged —
git statusshows no real.envfiles staged.