ENV vars intro
🌱
Configure once, run across environments.
ENV vars help to create and shape the environment of where the code runs.
Problem
You most likely run at least two environments for your app: a local one for development and one on fortrabbit for production. Both instances probably have access to a database, but your local MySQL credentials will differ from the remote ones. Your config.php file is under Git version control, so how do you manage different environment-specific configurations, from database credentials to API keys? Additionally, how do you work in a team where everyone has their own local settings? How do you separate code from configuration to ensure portability and keep secrets secure?
Solution
Store everything specific to the environment in environment variables, or 'ENV vars' for short. An ENV var is a key-value pair, like so: MY_SQL_PASS=sCRAmblEDegGGs. The concept of ENV vars is widely used across different software systems, including Apache, Node.js, and PHP. Accessing ENV vars from PHP is straightforward, and it is a commonly accepted best practice to use them for configuration management.
Benefits
- Security: Store sensitive information like database credentials and API keys outside of your codebase. See ENV var security.
- Portability: Code can run in different environments without modification. Allow each team member to have their own local settings without affecting the shared codebase.
- Separation: Keep code and configuration apart.