Common Apache2 File Permission Issues and How to Fix Them

File permission issues are one of the most common causes of mysterious Apache errors — a 403 Forbidden with a perfectly valid file, or PHP silently failing to write a log or upload.

# Find out which user Apache actually runs as
ps aux | grep apache2 | grep -v root
# Usually www-data on Debian/Ubuntu, apache on RHEL/CentOS

# Apache needs at minimum:
# - read access to files it serves
# - execute access on directories in the path to reach them
# - write access to directories it needs to write into (uploads, cache, logs)

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

SELinux Adds Another Layer

# On RHEL/CentOS, standard Unix permissions being correct isn't enough -
# SELinux context matters too
ls -Z /var/www/example.com
sudo restorecon -Rv /var/www/example.com

On SELinux-enabled systems, correct standard Unix permissions alone won’t fix a 403 if the SELinux security context is wrong — a frequent source of confusion for admins used to Debian/Ubuntu, where this extra layer doesn’t exist by default.