Hosting & Deployment

Linux Filesystem Layout for Web Applications

Design a safer Linux directory structure for PHP sites by separating public files, private configuration, uploads, logs, and deployment releases.

A production web application needs a filesystem layout that makes the public attack surface obvious. The document root should contain only files that must be served directly: the front controller, public assets, and carefully controlled verification files. Configuration, database credentials, private uploads, backups, logs, and deployment metadata should live outside that directory. This separation is more reliable than trying to protect every sensitive file with a collection of web-server deny rules.

A practical layout might look like this:

/srv/www/example/
  releases/2026-11-22/
  current -> releases/2026-11-22/
  shared/
    config/
    storage/
      uploads/
      cache/
      sessions/
    logs/
  public/

The exact paths depend on the hosting provider. On shared cPanel hosting, the account home directory is often the best private boundary; on a VPS, /srv or /var/www may be appropriate. Do not copy this example blindly if the control panel expects a particular document-root structure.

Separate immutable code from writable data

Treat application releases as read-only after deployment. The web process should not be able to rewrite PHP source files, vendor libraries, or configuration. Give write access only to directories that genuinely need it, such as generated cache, session storage, and user-upload locations. If uploads are not intended to be executable, configure the web server to prevent script execution there and validate file type, size, and names in the application.

Verify the boundary

Inspect the actual document root and its parent directories:

pwd
find /path/to/site -maxdepth 2 -type d -printf '%M %u:%g %p\n'

Test that a private configuration file cannot be requested over HTTP. Also check symbolic links: a public symlink must not expose a private directory. Backups should be stored in a separate protected location and should not be placed beside downloadable public files.

Common mistakes and operational checks

A frequent mistake is moving secrets outside the document root but leaving a copy in an old deployment directory. Another is making the entire tree writable to solve a permissions error. Search release artifacts for .env, SQL dumps, archives, and debug files before publishing. Include filesystem ownership, writable paths, and restore locations in deployment documentation so that a future administrator does not weaken the boundary while troubleshooting.

Share this page Help other developers discover this resource.