Document root

Also known as: webroot

On this page 7

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 pathResolved fileTypical result
/public/index.htmlIndex file is served
/css/site.csspublic/css/site.cssStatic file is served
/blog/post-12No matching fileRewritten to the application
/../etc/passwdRejected after normalization400 or 404 response
/uploads/Directory without index403 or listing, by configuration
Example of URL-to-file mapping

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 serverDirective or settingCommon default path
ApacheDocumentRoot/var/www/html
Nginxroot (or alias per location)/usr/share/nginx/html
Caddyroot with file_serverSet explicitly per site
Microsoft IISPhysical path of the siteC:\inetpub\wwwroot
LiteSpeed Web ServerDocument root of the virtual hostDefined in the vhost template
lighttpdserver.document-root/var/www/html
Document root directives by web server

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.

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.

DirectoryContentsPublicly reachable
Document rootFront controller, CSS, scripts, imagesYes
Application rootSource code, libraries, templatesNo
Configuration directoryDatabase credentials, API keysNo
Upload or storage directoryUser files, cache, logsOnly through controlled routes
Server rootWeb server binaries and configurationNo
Directories around a typical web application

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.bak inside 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.

SymptomLikely causeFix
403 Forbidden on every pageMissing directory permission or access ruleGrant read and execute, allow the directory
Default welcome page appearsRequest matches the wrong virtual hostCheck server names and default host
Assets return 404 under a prefixroot used where alias was neededCorrect the location mapping
PHP file downloads as textScript handler not appliedConnect the location to PHP-FPM
Source files are downloadableProject root set as document rootPoint the root to the public folder
Troubleshooting document root issues

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.