MANUAL

.env and environment variables

.env and environment variables

Your app has settings that vary depending on the environment (your laptop, the test server, the live server) or that are confidential: database passwords, API keys, the mail server. You shouldn’t put these in your code, but in environment variables. In Laravel, these are stored in the .env file in your project’s root directory.

APP_NAME="Mijn App"
APP_ENV=local
APP_DEBUG=true
APP_URL=http://localhost:8000

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_DATABASE=mijnapp
DB_USERNAME=root
DB_PASSWORD=geheim

01

The golden rule: .env files should never be committed to Git

Otherwise, anyone with access to your repository could see your passwords, even years later. Check that .env is included in your .gitignore:

cat .gitignore | grep .env

What does go into Git: .env.example. This file contains all the names, but without any actual values. A new developer (or your AI assistant) copies that file:

cp .env.example .env
php artisan key:generate

02

Add a new institution

Suppose you’re linking a payment provider. In that case, always follow these three steps:

  1. .env: enter your actual key: MOLLIE_KEY=test_abc123.
  2. config/services.php: load it:
    'mollie' => [
        'key' => env('MOLLIE_KEY'),
    ],
    

You then use config('services.mollie.key') in your code. **Only use env() in the files within config/.

03

Settings on the live server

04

Docker and GitHub Actions

05

Oops: there’s a password in Git

  1. Change the secret immediately with the provider (new password, new API key). This is the most important step.
  2. Then remove it from your code and put it in .env.
  3. Simply clearing the history isn’t enough: someone may already have copied it.

06

Need help?

Are you unsure whether there are any secrets lurking in your project? With AI code review, I’ll check this for you. A comprehensive, secure setup is an essential part of bespoke software and web applications.

07

Skills

08

Related services