0Pricing
PHP Academy · Lesson

Environment Variables and .env Files

Manage sensitive config with .env files and dotenv libraries in PHP.

Why Use Environment Variables?

Hardcoding credentials or environment-specific config in source code is insecure and makes deployments fragile. Environment variables separate config from code.

The .env File

A .env file contains key-value pairs loaded into the environment at boot. Add it to .gitignore — NEVER commit it to version control.

# .env
APP_ENV=production
APP_KEY=base64:...
DB_HOST=127.0.0.1
DB_NAME=myapp
DB_USER=myapp_user
DB_PASS=supersecret

REDIS_HOST=127.0.0.1
MAIL_HOST=smtp.mailtrap.io

All lessons in this course

  1. Nginx and PHP-FPM Configuration
  2. Environment Variables and .env Files
  3. Deploying with GitHub Actions
  4. Dockerizing a PHP Application
← Back to PHP Academy