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:
.env: enter your actual key:MOLLIE_KEY=test_abc123.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
- Set
APP_ENV=productionandAPP_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
.envon the server: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.
04
Docker and GitHub Actions
- Docker: Use
env_file: .envto let Compose read the file. Do not set secrets usingENVin 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 }}.
05
Oops: there’s a password in Git
- Change the secret immediately with the provider (new password, new API key). This is the most important step.
- Then remove it from your code and put it in
.env. - 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
- [Laravel environment and config] (/nl/skills/laravel-env-config)
- [Secrets Management] (/nl/skills/secrets-management)
08
Related services
- [Maatwerk software en webapplicaties] (/nl/services#service-03)
- [Beoordeling van AI-code] (/nl/services#service-05)