Security & Configuration

Manage Local Environment Variables Without Leaking Secrets

Separate local configuration from source code and prevent development credentials from entering Git history or public directories.

Local PHP applications often need database passwords, mail credentials, API tokens and environment-specific URLs. Hard-coding these values creates avoidable risk and makes it difficult to move the application between machines.

Separate configuration from code

Read environment-specific values from process environment variables or a configuration file excluded from version control. Provide a .env.example containing variable names and safe placeholder values, not real credentials. Document required variables and sensible local defaults.

Keep secrets outside public paths

A web server should expose only the intended public directory. Place private configuration, logs, backups and uploaded private documents outside that directory. If the hosting architecture requires a file under the project root, ensure the web server explicitly denies direct access and test that denial.

Validate configuration at startup

Fail clearly when required variables are missing or malformed. Validate ports, URLs, boolean flags and encryption keys instead of silently falling back to unsafe values. Do not print secret values in exception messages or diagnostic pages.

Protect Git history

Add .env, local override files, private keys and generated secrets to .gitignore. If a secret was committed, removing the file in a later commit is not enough: rotate the credential and follow a repository history cleanup process where appropriate.

Avoid environment confusion

Development, testing, staging and production should have distinct credentials and services. Never point a local test suite at production by default. Use a clear environment name and explicit safeguards for destructive commands.

Further reading

Share this page Help other developers discover this resource.