Introduction
Proper environment configuration is critical for security and functionality in production. This guide covers environment variables for both backend and frontend, security considerations, and common configuration patterns.Backend Environment Variables
Core Configuration
Create a production.env file in your Laravel backend:
.env
Database Configuration
.env
- MySQL/MariaDB
- PostgreSQL
- SQLite
Session Configuration
.env
Set
SESSION_DOMAIN to your root domain with a leading dot (.yourdomain.com) to share sessions across subdomains.Cache Configuration
.env
- Redis (Recommended)
- Database
- File
Queue Configuration
.env
Mail Configuration
.env
- SMTP
- Mailgun
- AWS SES
Logging Configuration
.env
Set
LOG_LEVEL=error in production to reduce log noise. Use debug only for troubleshooting.Frontend Environment Variables
Next.js Configuration
Create.env.production in your Next.js frontend:
.env.production
Environment Variable Validation
Create a validation file to ensure required variables are set:lib/env.ts
CORS Configuration
Backend CORS Setup
The CORS configuration is inconfig/cors.php:
config/cors.php
Multiple Frontend URLs
If you have multiple frontend domains (staging, production):config/cors.php
.env
Sanctum Configuration
Backend Sanctum Setup
.env
config/sanctum.php:
config/sanctum.php
Frontend Axios Configuration
Configure Axios to send cookies:lib/axios.ts
API URL Configuration
Development vs Production
- Development
- Production
- Staging
Backend (.env)Frontend (.env.local)
Security Best Practices
Generate Strong APP_KEY
Generate Strong APP_KEY
Secure Database Credentials
Secure Database Credentials
- Use strong passwords (16+ characters, mixed case, numbers, symbols)
- Create database users with minimal privileges
- Use different credentials for each environment
- Store credentials in secure vault (e.g., AWS Secrets Manager)
Restrict CORS Origins
Restrict CORS Origins
config/cors.php
Environment File Permissions
Environment File Permissions
Use Environment-Specific Files
Use Environment-Specific Files
Don’t use the same Add all to
.env file across environments:.gitignore:Common Configuration Patterns
Full Production Backend .env
.env
Full Production Frontend .env.production
.env.production
Environment Variable Management Tools
Doppler
Centralized environment variable management
- Sync across environments
- Team collaboration
- Audit logs
- Secret rotation
AWS Secrets Manager
AWS-native secret management
- Automatic rotation
- Fine-grained access control
- Encryption at rest
- Integration with AWS services
HashiCorp Vault
Enterprise secret management
- Dynamic secrets
- Encryption as a service
- Secret leasing
- Audit logging
Dotenv Vault
Encrypted .env files in version control
- Encrypted commits
- Team synchronization
- Multiple environments
- Simple integration
Troubleshooting
CORS Errors
CORS Errors
Symptoms:
- “No ‘Access-Control-Allow-Origin’ header”
- Authentication fails
- Cookies not set
- Verify
FRONTEND_URLin backend.env - Check
allowed_originsinconfig/cors.php - Ensure
supports_credentialsistrue - Clear Laravel config cache:
php artisan config:clear - Verify both apps use HTTPS in production
Authentication Issues
Authentication Issues
Symptoms:
- Login succeeds but user not authenticated
- Cookies not being set
- CSRF token mismatch
- Check
SESSION_DOMAINis set correctly (.yourdomain.com) - Verify
SANCTUM_STATEFUL_DOMAINSincludes frontend domain - Ensure axios has
withCredentials: true - Verify HTTPS is enabled
- Check browser DevTools → Application → Cookies
Environment Variables Not Loading
Environment Variables Not Loading
Backend (Laravel):Frontend (Next.js):
Database Connection Failed
Database Connection Failed
Check Connection:Common Issues:
- Wrong database host or port
- Incorrect credentials
- Database server not running
- Firewall blocking connection
- Missing database
Checklist
Before deploying to production:Backend Environment
- Set
APP_ENV=production - Set
APP_DEBUG=false - Generate unique
APP_KEY - Configure production database
- Set correct
APP_URLandFRONTEND_URL - Configure
SESSION_DOMAINandSANCTUM_STATEFUL_DOMAINS - Set up mail configuration
- Configure cache and queue drivers
- Set
LOG_LEVEL=error
Frontend Environment
- Set
NEXT_PUBLIC_BACKEND_URLto production API - Remove development-only variables
- Validate all required variables are set
- Test build locally:
npm run build && npm start
Security
- Verify
.envis not in version control - Set proper file permissions (600 for .env)
- Use strong passwords for all services
- Restrict CORS to specific origins
- Enable HTTPS on both apps
Next Steps
Backend Deployment
Deploy your Laravel backend with these configurations
Frontend Deployment
Deploy your Next.js frontend
Authentication
Learn about Laravel Sanctum authentication
Architecture
Understand the full-stack architecture