Laravel .env Generator
Laravel reads .env via vlucas/phpdotenv during bootstrap. This generator produces a Laravel 11 compatible file with APP_KEY, MySQL, Redis, queue, and mailer settings.
APP_NAME=Laravel APP_ENV=local APP_KEY=base64:qAp4cuBfRrGb7NRanqrwc1oevDoQ+f4Lt696iJ8IzGs= APP_DEBUG=true APP_URL=http://localhost DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD= REDIS_HOST=127.0.0.1 REDIS_PORT=6379
Secrets regenerate on every page load. Want to combine stacks? Use the full generator.
How to use this .env in Laravel
- 1.Save as .env in the project root.
- 2.Run php artisan key:generate to regenerate APP_KEY (or keep the one below).
- 3.For production, run php artisan config:cache — this freezes config at build time.
- 4.Only access env() inside config/*.php files — not anywhere else in the app.
Gotchas
- ⚠Calling env() outside config/*.php returns null when config:cache is active.
- ⚠APP_KEY must be exactly 32 bytes. Use php artisan key:generate, never generate manually.
- ⚠Laravel's queue config reads QUEUE_CONNECTION, not QUEUE_DRIVER (pre-9 name).
Common keys explained
APP_KEYRequired. Used for encryption + session cookies.
APP_ENVlocal | production | testing. Controls error verbosity.
APP_DEBUGNever true in production.
DB_CONNECTIONmysql | pgsql | sqlite.
REDIS_HOSTRedis for cache + sessions + queues.
MAIL_MAILERsmtp | mailgun | ses | log. log is great for dev.
Loading and validating the .env in Laravel
<?php
// Read env() ONLY inside config/*.php files.
// After "php artisan config:cache", env() outside config returns null.
return [
'stripe' => [
'secret' => env('STRIPE_SECRET'),
'webhook_secret' => env('STRIPE_WEBHOOK_SECRET'),
],
];
// In app code, always go through config():
$secret = config('services.stripe.secret');
// Generate APP_KEY: php artisan key:generate
// Cache for production: php artisan config:cacheLaravel: env vars in development, staging and production
Laravel ships a .env.example and expects you to copy it to .env on every machine. Locally, APP_ENV=local and APP_DEBUG=true. On production, APP_ENV=production and APP_DEBUG=false, otherwise Laravel's error page shows your .env values, including DB_PASSWORD.
Run php artisan config:cache on deploy. It compiles all config into one file and stops reading .env, which is faster. It also means every env() call outside config/ silently returns null, the most common Laravel production surprise.
Laravel: .env errors and fixes
No application encryption key has been specified.APP_KEY is empty. Run php artisan key:generate, or paste a base64: key from the Laravel APP_KEY generator.
SQLSTATE[HY000] [2002] Connection refusedDB_HOST=127.0.0.1 inside Docker or Sail points at the PHP container itself. Use the database service name, e.g. DB_HOST=mysql.
env('X') returns null in productionConfig is cached. Move the env() call into a config file and read it with config(), then run php artisan config:clear && php artisan config:cache.
Laravel .env FAQ
Can I commit Laravel's .env file?
No. Commit .env.example with empty values and keep .env in .gitignore (Laravel's default .gitignore already does this).
What happens if I change APP_KEY?
Existing encrypted data (encrypted casts, Crypt::encrypt values) and sessions can no longer be decrypted. Laravel 11 supports APP_PREVIOUS_KEYS for gradual rotation.
Do I need quotes around values with spaces?
Yes: APP_NAME="My App". Without quotes, Laravel's dotenv parser throws an error for values containing spaces.