PortGate

Resources · Security guides · Security guide

Keep .env and .git out of the web root

Point the vhost at public/, deny dotfiles except .well-known, and confirm /.env and /.git/config answer 403 or 404 from outside.

Check command

curl -s -o /dev/null -w '%{http_code}\n' https://example.com/.env

Copied.

Bots request /.env and /.git/config on every domain they find, all day. A Laravel .env holds APP_KEY, database passwords and payment keys. A readable .git directory lets anyone rebuild your source code, including old commits that still contain secrets.

Check from outside first

curl -s -o /dev/null -w '%{http_code}\n' https://example.com/.env
curl -s -o /dev/null -w '%{http_code}\n' https://example.com/.git/config

403 or 404 is correct. 200 means the file is public: fix it, then treat every secret in it as leaked and rotate it.

The usual cause: the wrong document root

Laravel, Symfony and most modern PHP frameworks serve from a public/ directory. The vhost must point there, not at the project root:

server {
    server_name example.com;
    root /var/www/example.com/current/public;
}

With the right root, .env sits one directory above anything nginx can serve.

Deny dotfiles anyway

A second layer catches deploy mistakes, such as a .git directory copied into public/:

location ~ /\.(?!well-known) {
    deny all;
}

Let's Encrypt and security.txt live under /.well-known, which this rule leaves alone. For Apache, in the vhost:

<DirectoryMatch "/\.(?!well-known)">
    Require all denied
</DirectoryMatch>
<FilesMatch "^\.">
    Require all denied
</FilesMatch>

Reload the web server and run the curl checks again.

Deploy without the .git directory

Build artifacts (git archive, rsync --exclude .git, a container image) instead of running git pull inside the web root. If you must pull on the server, keep the repository outside the document root and deploy into it.

If a .env was public

Rotate everything in it: APP_KEY (Laravel will invalidate existing sessions and encrypted values, so plan for that), database passwords, mail credentials, Stripe and cloud keys. Then check your access logs for requests to /.env that got a 200 to learn when the exposure started.