Environment Variables
Modern applications rarely run in a single environment. A local development environment, staging server, and production deployment often require different configuration values while sharing the same application code. Environment variables allow applications to adapt to their environment without modifying source code. In Bejibun, environment variables are the primary mechanism for configuring sensitive values and environment-specific settings.What Are Environment Variables?
Environment variables are key-value pairs that exist outside your application’s source code. Example:Why Use Environment Variables?
Without environment variables, applications often contain hardcoded values. Avoid:- Improved security
- Easier deployments
- Environment-specific configuration
- Better team collaboration
- Cleaner code
The .env File
Bejibun loads environment variables from a .env file located at the root of your project.
Example:
.env file is typically used during local development.
Example Environment Configuration
A typical application may contain:Accessing Environment Variables
Use theEnv service to retrieve values.
Example:
PORT is missing:
Recommended Usage
Environment variables should generally be consumed within configuration files. Good:Environment Files
Applications often require different settings for different environments. Example:Common Application Variables
Most Bejibun applications will use variables similar to the following.Application
- Application metadata
- Environment detection
- Debug settings
Server
- Network configuration
- HTTP server settings
Database
- Database connections
- Migration support
- ORM functionality
Redis
- Cache storage
- Queue processing
- Session storage
- Email delivery
- Notifications
- Password resets
Storage
- File uploads
- Asset storage
- Cloud integrations
Environment Detection
Applications often need to determine their current environment. Example:Validating Environment Variables
Missing configuration values can cause runtime failures. Instead of discovering problems after deployment, Bejibun may validate environment variables during startup. Example:- Early error detection
- Safer deployments
- Improved reliability
Type Conversion
Environment variables are always stored as strings. Example:Protecting Sensitive Data
Environment variables often contain secrets. Examples:Add .env to Git Ignore
Example:
Use .env.example
Instead of committing real secrets:
Production Environments
Production environments often inject variables directly through the hosting platform. Example:.env file may not exist.
The framework should work seamlessly regardless of where variables originate.
Possible sources:
Environment Variables and Security
Environment variables improve security but are not a complete security solution. Follow these recommendations:Use Strong Secrets
Good:Rotate Credentials
Periodically update:- API keys
- Database passwords
- Access tokens
- Encryption keys
Limit Access
Only authorized personnel should have access to production environment variables.Common Mistakes
Committing .env
Avoid:
Hardcoding Secrets
Avoid:Missing Defaults
Avoid:Skipping Validation
A missing variable can cause failures later. Validate critical variables during startup whenever possible.Example Configuration Flow
The following diagram illustrates how environment variables move through the application.Best Practices
When working with environment variables:- Store secrets outside source code
- Commit
.env.example, not.env - Use configuration files as an abstraction layer
- Validate required variables
- Provide defaults when appropriate
- Use strong secrets in production
- Rotate credentials regularly
What’s Next?
Now that you understand how Bejibun manages configuration and environment-specific settings, it’s time to learn what happens when an HTTP request enters your application. Continue to:- Request Lifecycle
