Linux File Permissions Explained: How to Secure /var/www the Right Way

Linux File Permissions Explained: How to Secure /var/www the Right Way

Most web server permission guides give you one command to paste and move on. That habit causes two common failures: files the web server cannot read, which break the site, and files the web server can write, which let an attacker change the site. To understand linux file permissions is to understand three numbers and three roles, and once those click, securing /var/www becomes a set of deliberate choices instead of guesswork. This guide explains the model, then gives a layout that works for Nginx and PHP sites.

The model has three roles for every file: owner, group, and others. Each role gets three rights: read, write, and execute. The familiar numbers come from adding read as 4, write as 2, and execute as 1. A file with mode 644 gives the owner read and write, and gives group and others read only. A directory with mode 755 gives the owner full rights and everyone else read plus execute, and execute on a directory means permission to enter it and list its contents. A file with mode 777 gives everyone full rights, and that setting should never appear on a production web root.

Linux File Permissions and the /var/www Layout

Start with ownership. The web server process runs as a specific user, often www-data on Ubuntu with Nginx. The deploy user, the account you log in with, is a different user. A sound layout makes the deploy user the owner, puts both users in a shared group, and gives the group read rights on files and read plus execute rights on directories. The web server can then read everything it needs to serve, but it cannot rewrite your code.

sudo chown -R deploy:www-data /var/www/example.com
sudo find /var/www/example.com -type d -exec chmod 755 {} ;
sudo find /var/www/example.com -type f -exec chmod 644 {} ;

Check the result with ls -l on a few files and one directory. The first column shows the mode in letters, such as -rw-r--r-- for a 644 file and drwxr-xr-x for a 755 directory. If you see a different pattern, the chmod commands above did not reach that path, which often happens when a file was created by a different user after the bulk change ran.

The var www permissions question comes up most often when uploads or caches need write access. The answer is narrow: give the web server write rights only on the specific directories that need it, such as an uploads folder or a cache folder, and nowhere else. A WordPress uploads directory at 775 with group www-data lets the server write uploaded images while the rest of the code stays read-only to it. Our guide on securing wp-config.php and WordPress file permissions applies this same idea to a WordPress install, including the config file that holds database credentials.

Directories That Need Write Access and Ones That Must Not Have It

Write a short list of writable paths for your stack and keep it short. For a plain static site, the list is empty: nothing in the web root needs server write access. For WordPress, the list is usually wp-content/uploads and a cache directory if a caching plugin uses one on disk. For a PHP app with file uploads, it is the upload directory alone. Every path outside that list stays read-only to the web server user.

sudo chgrp www-data /var/www/example.com/wp-content/uploads
sudo chmod 775 /var/www/example.com/wp-content/uploads

Config files deserve stricter treatment than normal files. A file holding database passwords or API keys should be 640 or 600, owned by the deploy user or root, with group read only where the app needs it. The wp-config.php file is the classic case: readable by the web server, writable by nobody except the owner, and never 644 in a shared environment where other users on the same machine could read it.

Watch for the trap of fixing a permission error by opening everything up. When a site shows a blank page after a deploy, the cause is often one file owned by root from a sudo deploy, not a mode problem. Fix the ownership on that file instead of running a blanket 777. A blanket 777 turns a small bug, such as a file upload flaw, into full code execution, because the web server can then overwrite any file in the tree, including executable scripts.

Groups, sudo Users, and the Principle of Least Access

Groups are the tool that keeps this layout usable. Add your deploy user to the www-data group with usermod -aG www-data deploy, then log out and back in for the group change to take effect. New files the deploy user creates will still carry the deploy user’s primary group by default, so set the setgid bit on shared directories if you want new files inside them to inherit the web group automatically.

sudo chmod g+s /var/www/example.com/wp-content/uploads
sudo usermod -aG www-data deploy

The setgid bit shows as an s in the group execute position, such as drwxrwsr-x. It solves the recurring annoyance where an uploaded file belongs to the wrong group and the web server cannot read it. It does not change who can write: write rights still come from the mode bits, not from group membership alone.

Keep the number of accounts with write access small. Every developer, deploy script, and CI key that can write to the web root is a path an attacker can use if that account is compromised. Our guide to creating sudo users and managing permissions on a VPS covers how to give people the access they need without handing out root. The same thinking applies inside /var/www: write access is a privilege to hand out sparingly, not a default for everyone on the team.

Checking Permissions After Deploys and Plugin Installs

Permissions drift after deploys, because a deploy tool running as root creates root-owned files, and after plugin installs, because a plugin can create directories with loose modes. Add a check to your routine: after each deploy, run a find command that lists files writable by the web server user outside your approved list, and files with mode 777 anywhere in the tree.

sudo find /var/www/example.com -type f -perm 777
sudo find /var/www/example.com -type f -not -user deploy -ls

An empty result from the first command is the expected state. The second command shows files owned by someone other than the deploy user, which is worth a look after any deploy that ran with sudo. Fix ownership with chown on those paths rather than loosening modes to work around the mismatch.

One more check matters on shared servers. If several sites live on one machine, each site should have its own group or its own PHP pool user, so a breach in one site cannot read another site’s config files. A single shared www-data user across all sites means a compromised site can read every other site’s database credentials. Separate pools cost a little setup time and remove a whole class of cross-site damage. Our WordPress security basics guide for VPS owners covers pool separation as part of a wider hardening pass.

The shorthand chmod 755 644, directories 755 and files 644, is the default to return to whenever a permission drift leaves you unsure. The layout to remember is short: deploy user owns the files, the web group reads them, directories are 755, files are 644, config files are tighter, and only named upload or cache directories are writable by the server. When a permission error appears, fix the ownership or the one mode that is wrong, and leave the rest of the tree alone. For a full LEMP install to apply this to, see our LEMP stack install guide for WordPress on a VPS.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.