
How to Configure Environment Variables in Laravel (.env)
Environment variables are one of Laravel's most powerful features for managing configuration across different environments. The .env file allows you to configure your application without hardcoding values into your code, making your application more secure and portable across different environments.
What is the .env File?
The .env file (environment file) is a simple text file that stores configuration variables as key-value pairs. Laravel automatically loads these variables into the application's environment. This file is never committed to version control, keeping your sensitive data secure.
Basic .env Structure
Here's what a basic .env file looks like:
APP_NAME=Laravel
APP_ENV=local
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
Key Environment Variables Explained
Application Variables
APP_NAME
The name of your application.
APP_NAME=My Awesome App
APP_ENV
The current environment (local, development, staging, production).
APP_ENV=production
APP_DEBUG
Enable or disable debug mode.
APP_DEBUG=false # Set to false in production
APP_URL
The base URL of your application.
APP_URL=https://yourdomain.com
Database Variables
DB_CONNECTION
The default database connection driver.
DB_CONNECTION=mysql
DB_HOST, DB_PORT, DB_DATABASE
Database connection details.
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_laravel_app
DB_USERNAME, DB_PASSWORD
Database credentials.
DB_USERNAME=app_user
DB_PASSWORD=your_secure_password
Cache Variables
CACHE_DRIVER
The cache storage driver.
CACHE_DRIVER=redis # Options: file, redis, memcached, array
QUEUE_CONNECTION
The default queue connection driver.
QUEUE_CONNECTION=redis # Options: sync, database, redis, beanstalkd, sqs
Session Variables
SESSION_DRIVER
The session storage driver.
SESSION_DRIVER=redis # Options: file, cookie, database, redis, memcached
SESSION_LIFETIME
The session lifetime in minutes.
SESSION_LIFETIME=120
Mail Variables
MAIL_MAILER
The default mail driver.
MAIL_MAILER=smtp # Options: smtp, sendmail, log, array, failover
MAIL_HOST, MAIL_PORT
SMTP server details.
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME, MAIL_PASSWORD
SMTP authentication credentials.
MAIL_USERNAME=your_smtp_username
MAIL_PASSWORD=your_smtp_password
MAIL_ENCRYPTION
SMTP encryption type.
MAIL_ENCRYPTION=tls # Options: null, tls, ssl
Security Variables
APP_KEY
The application encryption key.
APP_KEY=base64:your-generated-key-here
BROADCAST_DRIVER
The broadcasting driver for real-time features.
BROADCAST_DRIVER=redis # Options: pusher, redis, log, null
Additional Services
REDIS_HOST, REDIS_PORT
Redis connection details.
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
FILESYSTEM_DISK
The default filesystem disk.
FILESYSTEM_DISK=local # Options: local, public, s3, ftp
Security Best Practices
1. Never Commit .env to Version Control
Add .env to your .gitignore file:
# .gitignore
.env
.env.backup
.env.*.local
2. Use Strong Passwords
Use complex passwords for database and service credentials.
3. Restrict File Permissions
Set proper permissions for your .env file:
chmod 600 .env
4. Use Different Values per Environment
Create different .env files for different environments.
# .env.local
APP_DEBUG=true
# .env.staging
APP_DEBUG=false
APP_URL=https://staging.yourapp.com
# .env.production
APP_DEBUG=false
APP_URL=https://yourapp.com
Accessing Environment Variables
Using env() Helper
The most common way to access environment variables:
<?php
// Basic usage
$dbHost = env('DB_HOST');
// With default value
$port = env('DB_PORT', 3306);
// With boolean conversion
$isDebug = env('APP_DEBUG', false);
Using Config Files
Configure settings in config files and access them globally:
// config/database.php
'connections' => [
'mysql' => [
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', 3306),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
],
],
// Usage elsewhere
$dbHost = config('database.connections.mysql.host');
Environment-Specific Configuration
Using Different Environment Files
You can have multiple .env files:
// .env
APP_ENV=production
APP_DEBUG=false
// .env.local (local overrides)
APP_DEBUG=true
// .env.staging
APP_ENV=staging
APP_URL=https://staging.yourapp.com
Common Issues and Solutions
Environment Variables Not Loading
If .env variables aren't loading:
- Check file permissions:
chmod 600 .env
li>Restart your development server
li>Check for typos in variable names
Value Not Changing After Update
Environment variables are cached. Clear the cache:
php artisan config:clear
php artisan cache:clear
Conclusion
Environment variables are essential for building secure, portable Laravel applications. By properly configuring your .env file, you can easily manage different environments, keep sensitive data secure, and maintain consistent configuration across your development team.
Remember these key points:
- Never commit .env files to version control
- Use strong, complex passwords
- Set proper file permissions
- Clear cache when updating variables
- Use different configurations for different environments
With proper environment variable management, you'll build more secure and maintainable Laravel applications!
Happy coding! 🚀