.env supplies environment-specific values to configuration files. Application code reads config(). Once configuration is cached, calling env() outside config/ can return surprising results because the environment file is no longer loaded at runtime.
Keep secrets in a deployment secret store or protected environment, never in Git, logs, exception context or client bundles. Rotate with overlap where providers allow it: deploy support for old and new credentials, switch the secret, verify traffic, then revoke the old value. Restart queue workers and long-lived servers after configuration changes.
Use per-environment configuration deliberately. A local Mailpit host, staging callback URL and production key are not flags to infer from a hostname; they are explicit configuration contracts.
Configuration is a boundary, not a convenience bag
The useful separation is simple: the deployment environment supplies raw
values, files in config/ define their names, defaults and types, and the
application consumes resolved configuration. That gives every setting one
discoverable home and keeps a controller from learning how a secret is stored.
// config/billing.php
return [
'provider' => env('BILLING_PROVIDER', 'stripe'),
'webhook_tolerance_seconds' => (int) env('BILLING_WEBHOOK_TOLERANCE', 300),
'stripe' => [
'secret' => env('STRIPE_SECRET'),
'webhook_secret' => env('STRIPE_WEBHOOK_SECRET'),
],
];
// Application code
$tolerance = config('billing.webhook_tolerance_seconds');
Do not put a business switch into .env simply because it is easy to change.
For example, whether a customer may use an unfinished payment method is product
policy, usually owned by a database-backed setting or a reviewed deployment.
Environment values are best for deployment-specific facts: an endpoint,
credential, region, driver or capacity limit. They are not an invisible admin
panel.
Cast values intentionally. Environment input is text, so 'false', '0' and
an unset value have different failure modes. Use Laravel's configuration helpers
or explicit casts where the consuming code expects booleans, integers, arrays
or URLs. Validate critical configuration at boot or at the integration boundary
with an error that names the missing configuration key, never its secret value.
Secret ownership and exposure paths
A secret is compromised not only when it appears in Git. It can leak through a debug dump, a failed job payload, an exception reporter, a client-side Vite variable, a support bundle or an overly broad Kubernetes read permission. Inventory the paths before choosing a storage product:
| Secret type | Typical owner | Safe consumer |
|---|---|---|
| Database password | platform/deployment | PHP and worker processes |
| Payment API key | deployment secret store | billing adapter only |
| OAuth client secret | deployment secret store | callback/token exchange |
| Browser publishable key | public configuration | browser bundle |
The last row is deliberately different. A publishable browser key is not a
server secret and should not be treated as one; it must still be paired with
server-side authorization and provider restrictions. Conversely, never prefix a
server secret with VITE_ or serialize it into an Inertia prop. Anything placed
in a browser bundle is public by design.
Use the secret mechanism appropriate to the runtime: protected CI variables and platform secrets for a managed host, a cloud secret manager with workload identity for Kubernetes, or a protected deployment environment for a small VPS. The important property is access control and auditability, not a fashionable tool name. The web process, queue workers and an unrelated preview environment should not automatically receive the same credentials.
Rotation is a rollout, not an edit
The safest rotation has an overlap period. First establish whether the provider allows two active keys, accepts multiple webhook signing secrets, or supports a secondary database user. Then perform a controlled transition:
- Create a second credential without revoking the first.
- Deploy code able to read the new credential or verify either signed secret.
- Add the new value to the production secret store and build a fresh release.
- Restart workers, send a bounded real request and inspect provider-side logs.
- Revoke the old credential only after the overlap and verification window.
Webhook rotation illustrates why this matters. A provider can have deliveries in flight signed with the old secret while the new one is active. Verify against an explicit active-secret set during the overlap, then remove the old value:
final readonly class WebhookSignatureVerifier
{
/** @param list<string> $secrets */
public function __construct(private array $secrets) {}
public function verifies(string $payload, string $signature): bool
{
foreach ($this->secrets as $secret) {
if (hash_equals(hash_hmac('sha256', $payload, $secret), $signature)) {
return true;
}
}
return false;
}
}
Keep the overlap short and auditable. Accepting every historical key forever is not rotation; it is accumulating a permanent attack surface. Do not log the candidate signatures or secrets while diagnosing a failed verification.
Configuration caching and long-lived processes
Configuration cache is a release artifact. Build it after dependencies and environment values are available, and before the new release receives traffic:
php artisan config:cache
php artisan horizon:terminate
php artisan about --only=environment
horizon:terminate is an example, not a universal restart command. Every
process that retains Laravel in memory must reload: Horizon workers, ordinary
queue workers, Octane, a Reverb server, scheduler workers and any bespoke daemon.
A web container replacement usually handles this automatically; a process on a
VPS supervised by systemd needs an explicit graceful restart.
Changing the secret-store value alone does not update an existing PHP process. Clearing Redis does not update it either. These are distinct layers: deployment environment, generated configuration cache, process memory and application data cache. Knowing which layer owns a value prevents random cache-clearing during an incident.
Failure modes worth rehearsing
A new key works for web requests but jobs fail. The worker used an old release or was not restarted. Compare its image/release SHA and configuration cache with the web process before changing the provider account.
A staging callback reaches production. The callback URL was inferred or
copied rather than explicitly configured. Keep environment-specific URLs in one
configuration key, expose a health or about check that identifies the
environment without exposing credentials, and verify it after deployment.
A secret appears in an error tracker. Remove it from reporting context, rotate it immediately, then find the source: debug logging, an HTTP client exception, a queued payload or a support export. Redaction is a defence in depth measure, not permission to attach whole request headers to every event.
When not to use environment configuration
Do not use .env for per-user settings, feature rollout state, mutable
catalogue rules or data that must be changed by non-developers with an audit
trail. Those belong in the product's own data model and authorization boundary.
Do not use application configuration to distribute secrets to a client, and do
not use a secret manager as a substitute for authorization in your application.
The goal is not more variables. It is a deliberate path from deployment-owned input to a narrowly scoped consumer, with a rotation procedure that has been tested before an incident makes it urgent.