PHP "Permission Denied" on Folders: chmod 755, 775 or 777? (How to Fix It)
Quick answer: "Permission denied" means the user PHP runs as (usually www-data, apache or nginx) can't read or write that file or folder. Fix the owner first with chown, then use 755 for folders and 644 for files; folders PHP must write to (uploads, cache, logs) can be 775 with a shared group. On RHEL and Amazon Linux, also check SELinux. Never use 777.
Typical messages:
Warning: file_put_contents(/var/www/site/cache/data.json): Failed to open stream: Permission denied
Warning: mkdir(): Permission denied in /var/www/site/app/Storage.php on line 18
Warning: move_uploaded_file(/var/www/site/uploads/photo.jpg): Failed to open stream: Permission denied
They are among the most common PHP errors after a deploy, a server migration or uploading files via FTP. This guide explains how Linux permissions work for PHP and how to fix them properly.
How Linux permissions work (quickly)
Each file and folder has an owner, a group and three sets of permissions: for the owner, the group and everyone else. Each set combines read (4), write (2) and execute (1):
| Mode | Owner | Group | Others | Typical use |
|---|---|---|---|---|
| 644 | read, write | read | read | Normal files, PHP scripts |
| 755 | all | read, enter | read, enter | Normal folders |
| 775 | all | all | read, enter | Folders shared by your user and the web server group |
| 600 / 640 | read, write | none / read | none | Config files with passwords |
| 777 | all | all | all | Never on a web server |
On folders, "execute" means "can enter". To reach /var/www/site/uploads/photo.jpg, PHP needs execute permission on every folder in the path, not just the last one.
Step 1: find out which user PHP runs as
<?php
echo posix_getpwuid(posix_geteuid())['name']; // e.g. www-data, apache, nginx
Or on the server: ps aux | grep -E 'php-fpm|httpd|apache2' | head. With PHP-FPM, the user is set in the pool file (user = and group =). Remember that cron jobs and CLI scripts run as whoever runs them, which is why a cache file created by a cron job as root can block the website later.
Step 2: see the current owner and permissions
ls -la /var/www/site/
namei -l /var/www/site/uploads/photo.jpg # permissions of every folder in the path
Step 3: fix the owner
Most "Permission denied" problems are about ownership, not modes. For folders PHP must write to:
# Debian / Ubuntu
sudo chown -R www-data:www-data /var/www/site/uploads /var/www/site/cache
# RHEL / Amazon Linux (Apache)
sudo chown -R apache:apache /var/www/site/uploads /var/www/site/cache
A common, safer layout: your deploy user owns the code (PHP can only read it), and the web server only owns the folders it writes to. That way, a vulnerability in the site can't modify the PHP files.
Step 4: set the modes
cd /var/www/site
find . -type d -exec chmod 755 {} \;
find . -type f -exec chmod 644 {} \;
When your user and the web server both need to write: 775 with a group
If you upload files via FTP/SFTP as your user and PHP also writes to the same folder, share a group instead of opening it to everyone:
sudo usermod -aG www-data deploy # add your user to the web server group
sudo chown -R deploy:www-data /var/www/site/uploads
sudo chmod -R 775 /var/www/site/uploads
sudo chmod g+s /var/www/site/uploads # new files inherit the group
The g+s (setgid) bit makes new files inside the folder belong to the same group, so neither side locks the other out. Log out and back in for the group change to apply to your user.
Why not 777?
777 lets any user and process on the server write to the folder. If an attacker manages to upload or create a PHP file there, it can run as your site. Many hosts and PHP handlers (suPHP, some PHP-FPM setups) even refuse to execute files in world-writable folders and return a 500 error. If 777 is the only thing that "works", the owner or group is wrong: fix that instead.
SELinux: permissions look right but still denied
On RHEL, CentOS, Rocky and Amazon Linux, SELinux adds a second check. Files under /var/www are readable by the web server, but not writable unless they have the right context:
ls -Z /var/www/site/uploads
sudo chcon -R -t httpd_sys_rw_content_t /var/www/site/uploads
# permanent rule, survives relabeling
sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/site/uploads(/.*)?"
sudo restorecon -Rv /var/www/site/uploads
Check for denials with sudo ausearch -m avc -ts recent. Don't disable SELinux to make the error go away.
Errors that look similar but aren't permissions
- "No such file or directory": the path is wrong or the folder doesn't exist. Use absolute paths with
__DIR__; - "open_basedir restriction in effect": PHP is limited to certain folders by configuration, regardless of permissions;
- "Read-only file system": the disk is mounted read-only, often after a disk error;
- A 403 in the browser: the web server can't read the file; see 403 Forbidden on PHP sites.
On Windows (XAMPP, IIS)
Windows uses ACLs instead of chmod. On IIS, give the application pool identity (IIS AppPool\YourSite) "Modify" permission on the writable folders. On XAMPP, Apache runs as your user, so permission errors usually come from folders protected by Windows (such as Program Files) or files open in another program.
Checklist
- Know which user PHP runs as (web and cron);
- Code owned by the deploy user, writable folders owned by the web server;
- 755 for folders, 644 for files, 775 + setgid where two users write;
- Execute permission on every folder in the path;
- SELinux context set on writable folders;
- No 777 anywhere.
Conclusion
"Permission denied" in PHP is almost always an ownership problem: the web server user doesn't own the folder it needs to write to. Find the user, fix the owner, keep 755/644, use a shared group with 775 when needed, and set the SELinux context on Red Hat-based systems. That fixes the error without opening the site to attacks. For upload-specific problems, see PHP file upload not working.
Frequently asked questions
Should I use 755 or 775 for uploads?
755 if the web server user owns the folder. 775 with a shared group if both your user and the web server need to write.
Why does it work with 777 but not with 755?
The folder's owner isn't the user PHP runs as. With 777 anyone can write; fix the owner and 755 will work.
Which user does PHP run as?
Usually www-data on Debian/Ubuntu and apache or nginx on RHEL/Amazon Linux. Check the PHP-FPM pool file to be sure.
Why did the site break after a cron job ran?
The cron job ran as root or another user and created files the web server can't write. Run the job as the web server user.
Permissions are correct but I still get "Permission denied". Why?
On RHEL-based systems, SELinux is probably blocking it. Set the httpd_sys_rw_content_t context.