The document root is the top-level directory on the server file system from which a web server delivers files in response to HTTP requests. Among web servers, it defines the point where the URL path meets the file system: a request for /images/logo.png is answered from images/logo.png inside that directory. The synonym webroot is common in hosting panels and deployment scripts.
Every virtual host or site usually has its own document root. Choosing it correctly affects what the public can reach, how applications are deployed and how much damage a misconfiguration can cause, so it is one of the first settings an administrator defines.
How the document root maps URLs to files
The web server builds a file path by appending the decoded URL path to the document root. With a document root of /var/www/example/public, the request GET /docs/guide.html resolves to /var/www/example/public/docs/guide.html. If the path points to a directory, the server looks for an index file such as index.html or index.php, or returns a listing when directory indexing is enabled.
Before serving the file, the server normalizes the path to remove sequences such as ../. This step prevents path traversal attacks that try to escape the document root and read files like system configuration or private keys. Requests that cannot be mapped to an existing file either return 404 Not Found or are passed to an application, for example through rewrite rules that send every request to a front controller.
The table below shows how the same document root resolves typical requests.
| Requested URL path | Resolved file | Typical result |
|---|---|---|
/ | public/index.html | Index file is served |
/css/site.css | public/css/site.css | Static file is served |
/blog/post-12 | No matching file | Rewritten to the application |
/../etc/passwd | Rejected after normalization | 400 or 404 response |
/uploads/ | Directory without index | 403 or listing, by configuration |
Configuring the document root on common servers
Each web server names the setting differently, but the concept is identical. The directive is usually defined per virtual host or server block, so one machine can serve many sites from separate directories.
| Web server | Directive or setting | Common default path |
|---|---|---|
| Apache | DocumentRoot | /var/www/html |
| Nginx | root (or alias per location) | /usr/share/nginx/html |
| Caddy | root with file_server | Set explicitly per site |
| Microsoft IIS | Physical path of the site | C:\inetpub\wwwroot |
| LiteSpeed Web Server | Document root of the virtual host | Defined in the vhost template |
| lighttpd | server.document-root | /var/www/html |
In Nginx, the difference between `root` and `alias` is a frequent source of errors. With root, the location path is appended to the directory; with alias, the location prefix is replaced by the given path. In Apache, the DocumentRoot must also be allowed by a matching Directory block, otherwise requests end in 403 Forbidden.
When PHP runs through PHP-FPM or another FastCGI backend, the web server passes the resolved path in the SCRIPT_FILENAME parameter. The backend must see the same directory layout, which matters when the web server and the application runtime run in different containers.
Document root compared with related directories
The document root is only one of several directories in a typical web application. Keeping their roles separate is a basic defense-in-depth practice.
| Directory | Contents | Publicly reachable |
|---|---|---|
| Document root | Front controller, CSS, scripts, images | Yes |
| Application root | Source code, libraries, templates | No |
| Configuration directory | Database credentials, API keys | No |
| Upload or storage directory | User files, cache, logs | Only through controlled routes |
| Server root | Web server binaries and configuration | No |
Modern frameworks such as Laravel or Symfony place only a `public` subdirectory in the document root and keep the rest of the project one level above it. Older applications, including many WordPress installations, put the entire application inside the document root and rely on rules in .htaccess or the server configuration to block sensitive files.
Security and permission practices
A safe document root contains only files meant to be public. Anything placed there is potentially downloadable, including backups, version control metadata and environment files.
- Keep configuration and secrets outside the document root, never in files such as
config.bakinside it. - Block access to hidden paths such as `.git` and `.env`, which attackers scan for automatically.
- Give the web server process read-only access wherever writing is not required.
- Disable script execution in upload directories, so that an uploaded file cannot run as code.
- Turn off directory listing unless it is intentionally offered.
File ownership deserves equal care. A common pattern gives a deployment user ownership of the files and lets the server process, such as www-data or nginx, read them, with permissions of 644 for files and 755 for directories. Granting 777 to fix an error is a serious misconfiguration, because any local process can then alter the site.
Common mistakes and their fixes
Most document root problems show up as 403 or 404 errors or as files appearing where they should not. The table summarizes typical symptoms.
| Symptom | Likely cause | Fix |
|---|---|---|
| 403 Forbidden on every page | Missing directory permission or access rule | Grant read and execute, allow the directory |
| Default welcome page appears | Request matches the wrong virtual host | Check server names and default host |
| Assets return 404 under a prefix | root used where alias was needed | Correct the location mapping |
| PHP file downloads as text | Script handler not applied | Connect the location to PHP-FPM |
| Source files are downloadable | Project root set as document root | Point the root to the public folder |
In simple terms
The document root is the shop window of a website. A web server keeps many folders on its disk, but visitors can only see what sits in this one folder and the folders inside it.
When someone types a web address, the server takes the part after the domain name and looks for a matching file in that window. Everything kept elsewhere, such as passwords and program code, stays in the back room. Put only public things in the window, and the rest stays private.
A practical example
A small agency moves a client site built with a PHP framework to a new Nginx server. The developer first sets the server block root to /srv/client/ and the site works, but a security scan soon shows that /.env returns the database password. The whole project was exposed because the document root pointed to the project folder.
The developer changes the root to /srv/client/public, adds a rule that denies hidden files, and routes all unknown paths to index.php. File permissions are set to 644 and 755, and the upload folder no longer executes PHP. A repeated scan finds no sensitive file reachable, and the site behaves exactly as before for visitors.