Linux9 min readPublished: 4 Nov 2024Updated: 9 Nov 2026

Linux File Permissions for Web Servers and WordPress

The correct file and folder permissions for a Linux web server, why 777 breaks security, and the commands to reset a WordPress install without breaking it.

G7Cloud Engineering
Platform team
Share:
Abstract illustration representing linux file permissions for web servers and wordpress.
  • Directories 755, files 644, on almost every web server.
  • wp-config.php holds your database password: set it to 640.
  • Your deploy user owns the files; the web server user is in the group.
  • 777 lets every account on the server rewrite your site. It hides problems rather than fixing them.
  • On a normal WordPress site, only the uploads folder needs to be writable by the web server.

What permissions should a web server use?

Set directories to 755 and files to 644. Directories need the execute bit so they can be opened; files only need to be read. The web server should never need to write to your code, only to the folders that hold uploads, cache and logs. Anything looser adds risk and gains nothing.

WhatOctalLooks likeWhy
Directories755drwxr-xr-xOwner can change contents, everyone else can enter and list
Files644-rw-r--r--Owner can edit, the web server can read
wp-config.php640-rw-r-----Holds database credentials: group only, never readable by all
Uploads directory775drwxrwxr-xThe web server group needs to write here
Shell scripts750-rwxr-x---Owner and group can run it; everyone else has no access
SSH private key600-rw-------Owner only. SSH refuses to use anything looser

Learn the first two rows. The rest are exceptions, each with a specific reason.

777 is not a fix

chmod 777 means every account on the server can read, edit and run the file, including accounts you do not control on a shared machine. If a plugin only works at 777, the real problem is ownership, and the fix is chown, not chmod.

How do Linux file permissions actually work?

Every file has three sets of permissions: for its owner, its group and everybody else. Each set says who can read, write or execute it. ls -l shows them in the first ten characters.

Reading ls -l output
$ ls -l /var/www/example.com
-rw-r--r--  1 deploy www-data   405 Jul 29 09:14 index.php
drwxr-xr-x  4 deploy www-data  4096 Jul 29 09:14 wp-content

  -rw-r--r--
  |[-][-][-]
  | |  |  +-- everyone else: read only
  | |  +----- group (www-data): read only
  | +-------- owner (deploy): read and write
  +---------- file type: - is a file, d is a directory
Columns three and four are the owner and the group. They matter as much as the permission letters.

The number form adds the permissions together: read is 4, write is 2, execute is 1. So 6 is read and write, 7 is all three, and 5 is read and execute. There is one digit for each of the three sets.

NumberPermissionsMeaning
7rwxRead, write, execute
6rw-Read and write
5r-xRead and execute (the normal setting for a directory others must enter)
4r--Read only
0---No access

755 reads as: owner 7, group 5, everyone else 5.

Execute means something different on a directory

On a file, execute means you can run it. On a directory, it means you can open it and reach the files inside. A directory set to 644 cannot be opened at all, which is why removing the execute bit from a folder makes a whole site return errors.

Which user should own my website files?

Not the web server: this is the most common mistake. On Debian and Ubuntu the web server runs as www-data; on AlmaLinux, Rocky and CentOS it runs as apache or nginx. If that account owns your files, any code the web server runs can rewrite your whole site, which is exactly what an attacker with a webshell (a malicious script uploaded to your site) wants.

The safer setup is a separate deploy user that owns the files, with the web server user in the group. The web server can then read everything, and can write only where you have given the group write access.

Set ownership correctly
# Find out which user your web server actually runs as
ps aux | grep -E '[a]pache2|[n]ginx|[p]hp-fpm' | awk '{print $1}' | sort -u

# Own the files as the deploy user, group them to the web server
sudo chown -R deploy:www-data /var/www/example.com

# Add the deploy user to the web server group so both can collaborate
sudo usermod -aG www-data deploy
Use apache or nginx instead of www-data on Red Hat-style systems. See adding a user to a group for how group membership works.

Group changes need a new login

Adding yourself to a group does not affect the shell you are already in. Log out and back in, or run newgrp www-data, or the permission you just granted will seem to do nothing.

What are the correct WordPress file permissions?

WordPress follows the general rule with two exceptions. wp-config.php should be tighter than everything else, because it holds your database password. wp-content/uploads must be writable by the web server, because that is where media is saved.

PathPermissionsOwner:groupNotes
All directories755deploy:www-dataThe default for everything
All files644deploy:www-dataThe default for everything
wp-config.php640deploy:www-dataDatabase credentials live here
wp-content/uploads775 folders, 664 filesdeploy:www-dataMedia library writes here
wp-content/cache775deploy:www-dataOnly if a caching plugin needs it
.htaccess644deploy:www-data664 only while you are editing permalinks

Everything not in this table gets 755 for directories and 644 for files.

Updating plugins and core without write access

If the web server cannot write to wp-content/plugins, WordPress asks for FTP details when you click update. Update with WP-CLI instead, or let your host do it, rather than opening up the whole plugin folder.

How do I reset broken WordPress permissions safely?

Never run chmod -R 755 across a site: it sets the execute bit on every file, images and PHP included. Use find to set directories and files separately. Run these commands in this order and nothing is left half-broken.

The full reset, in order
cd /var/www/example.com

# 1. Ownership first: deploy user owns, web server group reads
sudo chown -R deploy:www-data .

# 2. Directories to 755
sudo find . -type d -exec chmod 755 {} \;

# 3. Files to 644
sudo find . -type f -exec chmod 644 {} \;

# 4. Lock down the config file
sudo chmod 640 wp-config.php

# 5. Give the web server group write access to uploads only
#    (folders 775, files 664: chmod -R 775 would make every image executable)
sudo find wp-content/uploads -type d -exec chmod 775 {} \;
sudo find wp-content/uploads -type f -exec chmod 664 {} \;
sudo chmod g+s wp-content/uploads
g+s in step 5 is the setgid bit: new files created in uploads take the www-data group automatically, so the fix holds.
Pro Tip

On a large site, find -exec starts one process per file and can take minutes. find . -type f -print0 | xargs -0 chmod 644 does the same job much faster.

On a live site, take a tested backup first: a reset changes every file and there is no undo. Our backup strategy guide covers what a usable backup looks like.

How do I diagnose a permission problem?

Start from the symptom. Three symptoms cover nearly everything, and each points to a different cause.

SymptomUsual causeCheck
403 Forbidden on every pageA parent directory is missing the execute bitnamei -l /var/www/example.com/index.php
Blank page or 500 errorPHP cannot read a required fileCheck the PHP error log for "failed to open stream: Permission denied"
Media uploads failThe web server cannot write to wp-content/uploadssudo -u www-data test -w wp-content/uploads && echo writable

namei is the tool people forget: it walks every directory in a path and prints the permissions at each level.

Check the whole path, not just the file
# Shows permissions at every level of the path
namei -l /var/www/example.com/wp-content/uploads

# Test as the web server user itself, which is the definitive answer
sudo -u www-data test -r /var/www/example.com/index.php && echo 'can read' || echo 'cannot read'
sudo -u www-data test -w /var/www/example.com/wp-content/uploads && echo 'can write' || echo 'cannot write'
A file can be 644 and still unreadable if any directory above it blocks entry. namei finds that in one command.

If the error log is your next stop, understanding Linux system logs covers where each service writes and how to follow it live.

How do permissions interact with SFTP and deployments?

Most permission drift comes from mixing access methods. You deploy over SSH as deploy, a colleague uploads over SFTP as webmaster, and a plugin writes as www-data: three owners, and files the rest of the team cannot edit.

  • Put every person in the same group and set that group on the site's root folder, so anything anyone creates can be edited by the rest of the team.
  • Set the setgid bit on the root folder with chmod g+s, so new files and folders take its group instead of the creator's default.
  • Set a shared umask of 002 for the deploy accounts. The umask decides the permissions of new files; 002 makes them 664 and new folders 775, instead of 644 and 755.
  • Choose one way to deploy and keep to it. Mixing git pull, SFTP and the WordPress plugin installer on one site is what causes the mess.
A document root that stays consistent
sudo groupadd webdev
sudo usermod -aG webdev deploy
sudo usermod -aG webdev webmaster

sudo chown -R deploy:webdev /var/www/example.com
sudo find /var/www/example.com -type d -exec chmod 2775 {} \;
sudo find /var/www/example.com -type f -exec chmod 664 {} \;
The leading 2 in 2775 is the setgid bit. Every new folder created underneath inherits it, so the setup holds over time.

Isolation limits the damage

On G7Cloud each site runs in its own container with its own files, its own PHP and its own database, so no other customer's account shares your server. The rules above still apply inside the container, but a mistake stays within that one site. See how we secure WordPress hosting.

Frequently asked questions

What are the correct WordPress file permissions?

Directories 755, files 644, wp-config.php 640, and wp-content/uploads 775 for folders (664 for files) so the web server group can save media. Your deploy user should own the files, with the web server user (www-data, apache or nginx) as the group. Nothing on a normal WordPress site needs 777.

Is chmod 777 ever acceptable?

No. 777 lets every account on the server, including any process an attacker runs, rewrite the file. When a plugin only works at 777, the web server user is not in the owning group. Fix it with chown and a group-writable 775 folder.

Should www-data own my WordPress files?

No. If the web server user owns the files, any code it runs can rewrite the whole site, which is what a webshell needs. Have a separate deploy user own the files, put www-data in the group, and give group write access only to uploads and cache.

Why do I get 403 Forbidden after fixing file permissions?

Almost always a directory above the file is missing its execute bit, so the web server cannot get through the path to reach it. Run namei -l on the full path: it prints permissions at every level and shows which directory is blocking.

What is the setgid bit and why should I use it?

setgid (chmod g+s, or a leading 2 in the number form) makes new files and folders take the parent folder's group instead of the creator's default group. On a shared site folder it keeps ownership consistent, so permissions do not drift each time someone different uploads a file.

How do I check permissions as the web server user?

Use sudo -u www-data test -r <path> for read and -w for write. It answers directly, taking account of group membership and every parent folder at once.

Put this into practice on G7Cloud

Every site runs in its own dedicated container behind ScaleShield, with daily backups that are restore-tested every night. Start on the free plan, no card needed.

About G7Cloud Engineering

Platform team

Articles written by the engineers who build and run G7Cloud: UK managed hosting and the AI Website Builder. We write about what we operate every day: containers, backups, databases, and the small-business websites that run on them.

More about G7Cloud →