Fixing 403 Forbidden When Setting up CentOS 7.X / nginx 1.6.x / PHP 7.X / Enforcing SELinux

I know you’re all here for the command, so here it is:
sudo chcon -v -R --type=httpd_sys_content_t /var/www/

 

It’s no surprise one of the most common SELinux related search terms is “how to disable SELinux.” For the majority of users, the complexities involved in Mandatory Access Control aren’t worth sorting through and they opt to disable it in favor of the classic Linux Discretionary Access Control.

When used correctly, however, SELinux can do a lot for stopping the spread of an attack, or the privileges gained with a successful exploit.

What I’m doing in this oneliner is relatively simple: change the security context recursively for everything in /var/www to “httpd_sys_content_t”. I was able to find the correct security context by running ls -Z /etc/share/nginx/html – nginx already set these correctly when it installed, since this is the default web server content. If your distribution’s package uses a different SELinux context, just use that instead.

Fix your SELinux configuration instead of throwing the baby out with the bathwater and shutting it off!