What's the correct way to set ENV variables in Laravel 5?
Laravel 4 handled environment detection through a static array mapping. Developers would explicitly tell the framework which local environments corresponded to specific configuration files, often looking like $app->detectEnvironment(['local' => ['homestead']]). This approach was rigid; it required you to define the relationship between your codebase and its environment upfront.
Laravel 5 shifted this paradigm significantly. The default detection logic now relies on a closure that checks for getenv('APP_ENV') and defaults to 'production' if that variable is missing. This change fundamentally alters how you think about deploying and configuring your application, moving from file-to-file mapping to environment-variable-driven configuration.
A critical side effect of this shift is the management of sensitive data in your version control system. In Laravel 4, you might have ignored .env.* patterns, but Laravel 5 tightened this security posture by excluding only .env from .gitignore. This ensures that no hardcoded credentials ever make it into your repository. To fill this gap without committing secrets, the framework introduces .env.example. This file provides a template of required variables, such as APP_KEY, DB_USERNAME, and DB_PASSWORD, allowing you to safely clone a repository and generate a local configuration by copying the example file before running artisan commands.
This raises an immediate question for teams managing multiple environments: if I need to support local, staging, and alpha simultaneously, do I simply overwrite the .env file every time? The answer is no. You cannot stack multiple environment definitions within a single .env file using duplicate key assignments like APP_ENV=local followed by APP_ENV=alpha. In PHP's parsing logic, the last value assigned wins, rendering any preceding declarations for that specific key useless. Attempting to define distinct database credentials or settings under different environment headers in one file will result in unpredictable behavior and is a common source of configuration errors during deployment pipelines.
Instead, you must treat each environment as an isolated state. The standard workflow involves maintaining a single .env file per environment directory or creating separate repositories for specific environments if strict isolation is required. For example, your production server would pull from a production/.env, while your local machine uses local/.env. You never commit the actual .env files containing real secrets; you only commit .env.example.
If the .env file is missing entirely, Laravel falls back to its new default behavior defined in the application bootstrap. It will inspect the system environment variable APP_ENV. If that system variable exists and points to a valid string like production, Laravel loads that configuration. If APP_ENV is absent from your server or terminal, Laravel assumes you are running in production mode. This default can be dangerous if you forget to set the variable, as it will attempt to load config/database.php for production, which likely contains credentials you do not want live on your development machine.
To avoid this pitfall, always ensure APP_ENV is set before starting the application. On a Linux or macOS server, you can prepend this to your command:
APP_ENV=production php artisan serve --host=0.0.0.0 --port=8000
Or, on Windows PowerShell:
$env:APP_ENV = "production"; php artisan serve
This explicit setting overrides the default fallback and ensures you are loading the correct configuration block regardless of whether a .env file is present in your current directory.
For teams requiring complex environment management, such as handling different database clusters per region or distinct cache drivers for staging versus production, the single-file-per-environment approach remains the only robust solution. While some developers attempt to use conditional logic inside the .env file to switch configurations based on other variables, this violates the principle of separation of concerns and complicates deployment scripts. Keep your environments distinct, commit only the example templates, and manage the active state via system environment variables or script-level flags.
For deeper insights into managing these workflows in enterprise contexts, resources like https://laravelcompany.com often cover advanced deployment strategies that align with these architectural decisions.