Sphere
Log in Join
☆ 16

permission denied writing to var www even after chmod

RHEL 9, nginx with php-fpm, and the app can't write uploads to /var/www/html/uploads. Directory is 775 and owned root:www-data, php-fpm runs as www-data, POSIX permissions look fine. Still get Permission denied in the PHP error log. chmod 777 didn't help, which is when I remembered SELinux. getenforce says Enforcing. ls -Z /var/www/html/uploads shows unlabeled_t on one of the parent dirs. What's the proper fix, and please don't say setenforce 0.

⚑ Report

4 replies

☆ 0
F ferret ·2026-09-24#1

Use restorecon: restorecon -Rv /var/www/html. If the directory was created by hand or by a deploy script, it never got the httpd_sys_rw_content_t context that /var/www normally gets from the default policy. Check semanage fcontext -l filtered for /var/www to see what the policy expects.

⚑ Report
☆ 0
R rust72 ·2026-10-03#2

restorecon fixed the existing files and uploads work now. New directories created by the deploy script still get the wrong context though. Do I need to add a rule with semanage fcontext? Otherwise the next release breaks it again in the same way.

⚑ Report
☆ 0
M moss ·2026-10-11#3

Yes. semanage fcontext -a -t httpd_sys_rw_content_t for /var/www/html/uploads and everything under it, then restorecon -Rv that directory. That way every new file inherits the right label. Also check the audit log with ausearch -m AVC -ts recent if you hit more denials, it tells you the exact context needed.

⚑ Report
☆ 0
F fern ·2026-10-22#4

One more thing: if the app writes to a directory outside /var/www, like /srv or /opt, set the context there too rather than loosening the whole policy. We had a case where setroubleshoot suggested a boolean that would have disabled most of the web protection. The audit log names the type you need, trust that over the quick suggestion.

⚑ Report
Log in to reply.