Docs / Cloud & DevOps / How to Use Environment Variables for Configuration

How to Use Environment Variables for Configuration

By Admin · Feb 25, 2026 · Updated Apr 24, 2026 · 233 views · 1 min read

Store sensitive configuration (API keys, database passwords) in environment variables instead of code.

Why?

  • Secrets stay out of version control
  • Easy to change between environments
  • Industry best practice (12-factor app methodology)

Using .env Files

Create a .env file:

DB_HOST=localhost\nDB_NAME=myapp\nDB_USER=myuser\nDB_PASS=secret\nAPP_KEY=random-32-char-string

Add .env to .gitignore — never commit secrets.

Loading in Applications

  • Node.js: Use dotenv package
  • Python: Use python-dotenv
  • PHP: Use vlucas/phpdotenv

Systemd Services

[Service]\nEnvironmentFile=/var/www/myapp/.env

Was this article helpful?