# .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.

```dotenv
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
```

## 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`:

```bash
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:

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

## Add a new institution

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

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

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

## Settings on the live server

- Set `APP_ENV=production` and **`APP_DEBUG=false`**. If debugging is enabled, any visitor will be able to see your code – and sometimes your passwords – if an error occurs.
- After editing `.env` on the server:
  ```bash
  php artisan config:clear
  php artisan config:cache
  php artisan queue:restart
  ```
- Are you using a hosting provider (Laravel Cloud, Forge, a PaaS)? If so, you should enter the variables in their dashboard, not in a file.

## Docker and GitHub Actions

- **Docker:** Use `env_file: .env` to let Compose read the file. Do not set secrets using `ENV` in your Dockerfile, as this will result in them being included in the image.
- **GitHub Actions:** Set up secrets under *Settings → Secrets and variables → Actions* and use them as `${{ secrets.MOLLIE_KEY }}`.

## 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.

## 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**.

## Skills

- [Laravel environment and config] (/nl/skills/laravel-env-config)
- [Secrets Management] (/nl/skills/secrets-management)

## Related services

- [Maatwerk software en webapplicaties] (/nl/services#service-03)
- [Beoordeling van AI-code] (/nl/services#service-05)

