How to fix a publicly accessible .env file
If anyone can open yourdomain.com/.env in a browser, treat every password and key inside it as stolen: change them first, then block access to the file or move it out of the public folder.
A .env file is a small text file that many websites and apps use to store secrets: database passwords, API keys, mail server logins and app encryption keys. It is meant to be read by your application, never by visitors. When the web server is set up wrongly, anyone can type https://yourdomain.com/.env and see the contents as plain text.
What people usually see is a page of lines like DB_PASSWORD=... and APP_KEY=.... Sometimes the file is not exactly at that address but a copy is, such as .env.backup, .env.old or .env.save.
Why it matters
This is one of the more serious problems a small site can have, because the file hands over the keys directly. Nobody has to hack anything.
- Security: attackers and automated bots scan the whole internet for
/.envall day. If yours is exposed, assume it has already been fetched. - Your data: a database password may let someone read or change customer records, orders and user accounts.
- Money: payment, email and cloud keys can be used to send spam, run up usage bills or access paid services in your name.
- Trust and sales: a leak of customer data can trigger legal duties to notify people, and it damages trust that is slow to rebuild.
- Search: hacked sites often get spam pages injected, and search engines may flag or drop them.
How to check if your site has it
These checks are free and take a few minutes.
- Open a private browser window and go to
https://yourdomain.com/.env. A normal result is a 404 "not found" or 403 "forbidden" page. If you see text with equals signs and passwords, the file is exposed. - Try the same with
/.env.backup,/.env.old,/.env.productionand/.git/config. These are common leftovers. - If you are comfortable with a terminal, run
curl -I https://yourdomain.com/.envand look at the first line.200means the file was served.403or404is what you want. - Check any subdomains too, such as
staging.yourdomain.comandapp.yourdomain.com. Test sites are often the ones left open.
An automated security scan also looks for this. The week 2 OpenVAS scan from SiteMonday checks for exposed files like this once you have proven you own the site.
How to fix it
The order matters. Do the first step before anything else.
Step 1: replace every secret in the file. Blocking access does not undo the leak. Change the database password, API keys, mail passwords, payment keys and the app key, then update the .env file with the new values. Some changes have side effects (for example, changing a Laravel APP_KEY logs users out and can break encrypted data), so ask your developer if unsure.
Step 2: stop the file being served. There are two parts, and the best fix is both.
- Make sure the web server's document root (the folder visitors can reach) does not contain the
.envfile. The file should sit one level above it. - Add a rule that refuses to serve any file starting with a dot, but keep the
/.well-known/folder reachable, because certificate renewal (such as Let's Encrypt) and other standard checks need it.
Step 3: delete stray copies. Remove .env.backup, .env.old, zipped copies and anything similar from the public folder. See exposed backup files for the wider problem.
Fixing it on common platforms
Exact menus and file locations depend on your host and version, so treat these as the usual places to look.
Laravel. The web server should point at the public folder inside the project, not the project root. If it points at the root, .env is reachable. Change the document root in your hosting panel or server config. The Laravel guide goes through it.
Nginx. Add a rule inside your server block, then test and reload Nginx. The Nginx guide has more detail.
location ~ /\.(?!well-known) {
deny all;
}
Apache. Put this in the site's config or .htaccess file (Apache 2.4):
<FilesMatch "^\.env">
Require all denied
</FilesMatch>
cPanel. Open File Manager and look in public_html. If .env is there, move it one level up to your home folder or the app folder, and point your app at it. Turn on "Show Hidden Files" in File Manager settings or you will not see it.
Plesk. Under Websites and Domains, open Hosting Settings and check the Document root. It should be the public folder of your app, not the folder containing .env.
Next.js. Next.js does not serve .env files by itself. Exposure usually happens when the whole project folder is the web root, or the file was copied into public. Also remember that variables starting with NEXT_PUBLIC_ are deliberately included in browser code, so never put secrets in them.
Cloudflare. A firewall (WAF) rule that blocks requests to paths containing /.env is a useful extra layer, but it is not a fix. If the file is still in the public folder, it can be reached directly on the server. Fix the server first.
How to confirm the fix worked
- Reload
https://yourdomain.com/.envin a private window. You should get a 403 or 404, not file contents. If you use Cloudflare or another cache, clear the cache and test again. - Test the backup names and subdomains from the checking section.
- Confirm the site still works, including login, checkout and email, since you changed secrets.
- Look at your server logs for requests to
.envbefore the fix. If you see successful ones (status 200), your secrets were likely read, so also check your database, payment and email accounts for activity you do not recognise.
How to stop it coming back
- Keep the deployment simple: only the public folder of an app should be the document root.
- Never upload a whole project folder by drag and drop into
public_htmlorwww. - Add
.envto.gitignoreso it is not stored in your code repository, and keep a separate.env.examplewithout real values. - Do not leave backup copies of config files on the server. Store backups outside the web root.
- Keep a dot file rule in your server config so a future mistake does not expose anything.
- Rescan after changes to hosting, servers or deployments. Staging sites and new subdomains are the usual weak spots.
Other weak points often show up in the same review, such as server version disclosure. Fixing the file is the urgent part, but a regular check keeps one small mistake from sitting open for months.
Step by step for your platform
- Exposed .env file on Laravel
- Exposed .env file on Nginx
- Exposed .env file on Apache
- Exposed .env file on cPanel
- Exposed .env file on Next.js
- Exposed .env file on Cloudflare
- Exposed .env file on Plesk
Questions
Is an exposed .env file really that dangerous?
Yes. It often contains database passwords, payment keys and app encryption keys in plain text, and bots look for it constantly. Assume the secrets are compromised and replace them.
Is deleting the .env file enough?
No. Your app needs the file, and deleting it only breaks the site. Move it above the public folder, block dot files in the server, and replace the secrets that were exposed.
Will changing the document root break my site?
It can if your app expects a different layout. Frameworks like Laravel are built to run from their `public` folder, so it is usually safe, but test on a staging copy or ask your developer first.
How do I know if someone already read my file?
Check your server access logs for requests to `/.env` that returned status 200. If you cannot find logs, assume it was read and review your database, email and payment accounts for odd activity.
Does a WAF or Cloudflare rule solve this?
It adds protection but does not remove the cause. If the file is still in the public folder, it may be reachable another way, so also move it and add a server rule.
Can SiteMonday fix this for me?
SiteMonday finds and explains the problem in its week 2 security scan, after you prove you own the site. The fix itself is for you or your developer, and fixes can be quoted separately.