A security checklist for Laravel .env files
What belongs in a Laravel .env, what must never be exposed, and how to rotate secrets (APP_KEY included) after a leak.
Nov 24, 2026 · 4 min read
Your .env holds everything an attacker wants: database passwords, APP_KEY, mail credentials, payment and API keys. This is the checklist we now go through on every Laravel app.
In production
APP_ENV=productionandAPP_DEBUG=false. With debug on, the error page shows anyone who triggers it a stack trace with your source code, and the database queries that ran with their values. The Whoops page in older Laravel versions listed environment variables too.- The web server's document root is
public/, never the project root, or/.envmay be downloadable. - Check that from outside.
curl -I https://your-site.example/.envshould answer 404 or 403, and so should/storage/logs/laravel.log,/bootstrap/cache/config.phpand/vendor/autoload.php. - Run
php artisan config:cacheon deploy. The app then reads cached config rather than the file on every request. That is for speed, not security: every value is copied intobootstrap/cache/config.php, so that file needs the same care as.env. - Only the user PHP runs as can read the file.
chmod 600 .envdoes that when that user owns the file. If you deploy as one user and PHP runs as another, put the file in a group PHP's user belongs to and usechmod 640instead.
In the repository
.envis in.gitignore, and has never been committed. Check withgit log --all -- .env. That only looks for a file named.envat the top of the repository, in the branches and tags your clone has. Rungit fetch --allfirst, and trygit log --all -- '*.env*'to catch copies such as.env.productionor one in a subfolder. If one was ever committed, treat everything in it as leaked, even if a later commit removed it. None of this finds a secret pasted into some other file..env.examplehas the keys but no real values.
Have something watching
A checklist is only as good as the day you ran it. A real value in a committed .env is one of the things Lockhound's secret check looks for, along with API keys, private keys and database URLs with a password in them.
It is off until you turn it on (Settings, then Workspace) and is part of the paid plans, because it reads far more than the other checks: your source and configuration files, where the others read dependency files and the few files the malware check looks at. It reads the files of the current commit, not the git history, so it will not find a secret that was committed and later removed: git log --all -- .env is still the way to check that. It keeps where a secret is and a masked preview, never the whole value, and it never tries it against the provider.
After a leak
Rotate everything in the file, not just the secret you think was taken.
APP_KEY needs the most care. Generate a new one, and don't leave the leaked one anywhere in the app's configuration:
php artisan key:generate
That rewrites the APP_KEY line in .env and nothing else, and in production it asks you to confirm. If you cache config, run php artisan config:cache after every change to .env, or the app carries on with the old values from the cached file.
A new key has a cost:
- Everyone is logged out, and signed links you already sent, such as email verification links, stop working.
- Anything you stored encrypted, such as a column with an
encryptedcast, can't be read with the new key, and Laravel has no command that re-encrypts it.
If you have encrypted data, make the whole change with the app offline. Copy the old key somewhere safe, run php artisan down, generate the new key, and add the old one to APP_PREVIOUS_KEYS. Run a script that reads each encrypted value, sets it again and saves it. Saving alone writes nothing, because nothing has changed. Then take the old key back out and bring the app up.
Re-encrypting does not undo the leak. If someone copied the database as well as the key, they can read the old values, so rotate any credentials you kept in encrypted columns too.
APP_PREVIOUS_KEYS is made for a planned rotation, when the old key has not leaked. Put the key you are replacing in .env, then generate the new one:
APP_PREVIOUS_KEYS=base64:your-old-key
php artisan key:generate
Laravel then encrypts and signs with the new key, and still accepts cookies, signed URLs and encrypted values made with the old one. People are not all logged out at once, and encrypted data stays readable while you re-encrypt it. Remove the old key once that is done. This is also why it is wrong after a leak: for as long as the leaked key is listed, whoever holds it can make cookies and signed URLs your app accepts.
Then rotate the database password, mail credentials, third-party API keys and any OAuth client secrets, and check each provider's logs for use you don't recognise.
Read next
- Hardening Laravel servers on ForgeSite isolation, read-only deploy keys and safer deploy scripts: what we changed on our Forge servers after malware ran in a build.
- You leaked an API key: the first hourA secret was pushed to GitHub, posted in a chat or shipped to the browser. What to do first, what doesn't help, and how to stop it happening again.