Hosting & Deployment

Linux Users Groups and Least-Privilege Permissions

Apply Linux ownership, groups, file modes, and ACLs so web processes can write only to the directories they actually need.

Linux permissions are a security boundary, not merely a way to make an installation work. A web application normally needs to read its code and configuration, but only a small set of directories should be writable by the PHP or web-server process. If an attacker can write PHP files into the document root, a vulnerable upload feature can become persistent code execution.

Understand the permission model

Every file has an owner, a group, and permission bits for owner, group, and other users. Use id, namei, and stat to inspect the identity and path permissions involved:

id www-data
namei -l /srv/www/example/public/index.php
stat -c '%A %U:%G %n' /srv/www/example

The service account differs by distribution and hosting stack. Common names include www-data, apache, and dedicated per-site users. Do not assume one name without checking the running process.

Apply permissions by purpose

A typical pattern is read-only application code, restricted configuration, and narrowly writable runtime directories. For example:

chown -R deploy:sitegroup /srv/www/example/releases
find /srv/www/example/releases -type d -exec chmod 755 {} \;
find /srv/www/example/releases -type f -exec chmod 644 {} \;
chown -R siteuser:sitegroup /srv/www/example/shared/storage
chmod -R 750 /srv/www/example/shared/storage

These are illustrative commands. Adapt ownership and modes to the PHP execution model. With PHP-FPM running each site as its own user, the site user may own runtime directories. With a shared web-server user, a dedicated group or ACL may be needed. Never apply these commands to an entire hosting account without reviewing the paths first.

Diagnose instead of opening everything

When PHP reports “permission denied,” identify the exact file and process identity. Check every parent directory because execute permission is required to traverse directories. Use getfacl when ACLs may override ordinary mode bits. Avoid chmod -R 777; it grants unnecessary access and hides the real ownership problem.

After changes, test uploads, cache writes, log creation, and normal page reads as the real service user. Record the intended ownership model and recheck it after deployments, archive extraction, or control-panel migrations.

Share this page Help other developers discover this resource.