How to Set File Permissions and Ownership on a Host Without Breaking Your Site

How to Set File Permissions and Ownership on a Host Without Breaking Your Site

Safe WordPress file permissions and ownership on a shared or managed host come down to two rules: files should be 644 and directories should be 755, and every file should be owned by the same user your PHP process runs as. If those two things are true, WordPress can write its own updates and uploads, and nobody else on the server can touch your code. Most "broken site" incidents after a permissions change come from violating the second rule, not the first.

What the numbers actually mean, and why 777 is a trap

Each permission digit is a bitmask for read, write and execute, applied to three classes: owner, group, and other. Read is 4, write is 2, execute is 1, so 644 means the owner can read and write, group can read, and other can read. On a directory, the execute bit is not "run this file", it is "traverse into this directory", which is why directories need 755 rather than 644. A file without the execute bit cannot be entered, listed, or reached by path.

Setting a directory to 777 grants write to every user on the box. On shared hosting that means any other tenant whose PHP runs under a different account can drop a file into your wp-content/uploads directory. That is the exact mechanism behind a large share of injected PHP shells. It also breaks the security model many hosts rely on, where PHP runs under your user via PHP-FPM or suEXEC and therefore already has the write access it needs. If WordPress is asking you for 777, the real problem is ownership, not the mode bits.

Check what you have before changing anything:

find /home/example/public_html -type d -printf '%m %u:%g %p\n' | sort | uniq -c | sort -rn | head
#   412 755 example example /home/example/public_html
#    88 644 example example /home/example/public_html/wp-includes

That gives you a histogram of modes and owners. If you see a large count of 777 or a mix of owners, you have found your problem. The -printf flag is GNU find; on BSD or macOS use find ... -exec stat -f '%Lp %Su:%Sg %N' {} + instead.

WordPress file permissions and ownership on hosting: the PHP user is the key

WordPress writes to disk in three places: wp-content/uploads for media, wp-content/upgrade for core and plugin updates, and occasionally wp-config.php if you use the web installer. It also writes to .htaccess if you use pretty permalinks on Apache. All of those writes happen as whatever user the PHP process runs as, so the question is what that user is.

On most modern hosts PHP runs under PHP-FPM with a pool user, often your account name, sometimes something like web12. On older setups it ran as www-data or apache with suEXEC wrapping it back to your user. Find out which by dropping a temporary file and looking at the owner:

php -r 'file_put_contents("/home/example/public_html/probe.txt", "x");'
ls -l /home/example/public_html/probe.txt
# -rw-r--r-- 1 example example 0 Jan  1 00:00 probe.txt
rm /home/example/public_html/probe.txt

If the owner printed is your shell user, you are in the easy case: chown -R example:example over the docroot, find to set 755 on directories and 644 on files, and you are done. If the owner is www-data or another system user, your shell user cannot write to those files, and you must either run the fix through the host's file manager, through a cron job that runs as the PHP user, or by asking support to reset ownership. Do not "fix" this by chmodding to 777; that swaps a write-permission problem for a security problem.

Group ownership matters too. Some hosts run PHP in a group such as www-data and expect files to be group-writable, which means 664 and 775. If you set 644 there, WordPress cannot update itself and you get the "could not create directory" error on plugin installs. The general shape is: owner is the PHP user, group is the PHP user's group, and the mode is 644/755 unless the host documents group-write, in which case 664/775. Ask support what the PHP user is before you guess.

Fixing wrong permissions without triggering a 500

A 500 after a permissions change is almost always one of two things: a directory lost its execute bit, or wp-config.php became unreadable to the PHP user. Both are easy to cause with a careless recursive chmod. The safe sequence is to fix directories first, then files, then check the sensitive files individually.

cd /home/example/public_html
find . -type d -exec chmod 755 {} +
find . -type f -exec chmod 644 {} +
chmod 600 wp-config.php
chmod 640 .htaccess

That sets wp-config.php to 600 so only the owner can read it, which is the right call if the PHP user is the owner. If PHP runs as a different user, 600 will make the site return a blank page or a 500 because PHP cannot open the config. In that case use 640 with the correct group, or 644 if you have no other option, and rely on the docroot being outside the web root or on a server rule blocking direct access. The .htaccess file should be readable by Apache but not writable by anyone except the owner and the update process.

For wp-content/uploads, the directory needs to be writable by the PHP user. 755 owned by the PHP user is sufficient. Do not make it 777 "to be safe". If uploads fail with 755, the owner is wrong, and you should fix ownership rather than the mode. The same logic applies to wp-content/upgrade and wp-content/cache if a caching plugin uses it.

One more trap: running chown -R as root over a docroot can hand ownership to root, after which neither you nor PHP can write. If you have root, always pass the correct user and group and verify with ls -l before walking away. If you do not have root, do not run chown at all; use the host's file manager or a PHP script executed through the web server that calls chmod on the specific paths, since that script runs as the PHP user.

What to do next

Before you change anything, run the find histogram above and note the current owner of a file WordPress created, such as a recent upload. That tells you the PHP user. Then apply 755 to directories and 644 to files, tighten wp-config.php to 600 or 640 depending on whether PHP is the owner, and test by installing a small plugin and uploading an image. If either fails, the ownership is wrong, so fix that rather than widening the modes. Keep a note of the exact commands you ran so the next person, or you in six months, can reverse them.

Related articles

Subscribe to our newsletter

Get the latest hosting tips, performance insights, and industry news.