The Importance of Config Files Outside Web Root
Protecting credentials properly from the start
Most websites rely on configuration files that quietly hold everything together — database logins, API keys, SMTP credentials, secret salts. These files are often created early in a project and then forgotten. The problem is where they live.
What “outside the web root” actually means
Your web root is the publicly accessible folder — typically something like /public_html, /www, or /var/www/html. Anything inside it can potentially be accessed via a browser if misconfigured.
Placing config files outside this directory means they physically cannot be served over HTTP, even if something goes wrong.
A simple example:
/var/www/
├── public/ ← web root
│ └── index.php
└── config/
└── database.php
Your application loads the config file internally, but it’s invisible to the outside world.
Why this matters more than you think
Config files are one of the most common causes of catastrophic breaches — not because they’re inherently insecure, but because they’re exposed accidentally.
Typical failure points include:
- A server misconfiguration exposing
.envor.inifiles - Backup files (
.bak,.old) left in public directories - Debug routes or scripts dumping config contents
- Misconfigured NGINX/Apache rules after migrations
If the file is inside the web root, all of these become real risks. If it’s outside, most of them simply stop mattering.
The “.env file” false sense of security
Modern frameworks often promote .env files for managing secrets. That’s fine — but they’re only safe if:
- The server is correctly configured to block access
- The file is never accidentally renamed, copied, or exposed
- No edge case bypass exists (and they do happen)
Relying on server rules alone is fragile. File placement is a stronger guarantee.
Defence in depth (not just one layer)
Good security assumes something will eventually go wrong.
Even if you already:
- Block access via
.htaccessor NGINX rules - Restrict file types
- Disable directory listing
…you’re still trusting configuration to remain perfect forever. Moving sensitive files outside the web root adds a physical separation layer — a much harder failure to accidentally introduce.
Real-world scenarios where this saves you
- A staging site goes live with debug settings enabled
- A plugin exposes arbitrary file reads
- A developer uploads a backup archive into
/public - A misconfigured CDN caches sensitive files
In each case, credentials stored outside the web root remain protected.
How to implement it (simple approach)
- Move config files one directory above your web root
- Reference them using absolute paths in your application
- Keep only non-sensitive bootstrap files in
/public
Example in PHP:
require_once __DIR__ . '/../config/database.php';
That’s usually all it takes.
When you can’t move them
Some shared hosting setups don’t make this easy. If that’s the case:
- Deny access explicitly via server rules
- Rename files to avoid predictable names (
.env,config.php) - Ensure backups are never stored in public directories
But treat this as a compromise — not the ideal setup.
The bigger picture
Security issues rarely come from a single dramatic mistake. They come from small, reasonable decisions stacking up over time. Keeping config files inside the web root feels harmless — until something else goes wrong.
Moving them outside is a simple, one-time decision that removes an entire class of risk. And those are the best kinds of fixes.