Benjamin Vison Benjamin Vison - 26 days ago 12
PHP Question

Symfony2 NGINX Unable to create the cache directory

I know this may seem as a repeated question but so far I have 4 hours digging around unable to find a straight answer to this. I have setup a brand new CentOS 7 and I installed NGINX (https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-centos-7) + PHP 5.6 (https://webtatic.com/packages/php56/) and MariaDB.

Following the guides I was able to make the NGINX serve PHP files successfully.

I have a project in Symfony2 which I am trying to get in this server, my problem is when I try to open up http://server/web/app.php. I get the following error:

Fatal error: Uncaught exception 'RuntimeException' with message 'Unable to create the cache directory (/usr/share/nginx/html/app/cache/prod) ' in ................

I read in the documentation (http://symfony.com/doc/current/book/installation.html) that I should work around my permissions so I was able to apply the following:

$ HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1`
$ sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX app/cache app/logs
$ sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX app/cache app/logs


Since I have CentOS 7 from scratch that was the option that I was able to apply. However, I still have the same issue, after this approach these are the other options I've tried:


  1. changed the owner for app/cache and app/logs to nginx:nginx

  2. changed the permissions to 777 to the above folders

  3. verified and re-verified that PHP is running under the nginx user, I had to change this in the www.conf

  4. if I do ls -Al it shows that app/cache and app/logs are owned by nginx

  5. restarted services after each single change

  6. restarted the server completely

  7. cleared the cache and performed warmup and still

  8. Tried the umask option provided by symfony and still.



So after all these options I still get the same issue, to other people out there with this problem it's simply following the Symfony2 guidelines for applying the ACL and puff, done. But this is not the case.

What am I missing?

Ps: I double checked the permissions of the folder..and I am in the server as root.

Update 1

I re-installed CentOS 7 from scratch and followed https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-centos-7 and in combination with Symfony's suggestion for the configuration file and still I get the " Uncaught exception 'RuntimeException' with message 'Unable to create the cache directory" error

Answer

You might be running into SELinux issues, since CentOS 7 ships with SELinux running by default.

You can test by executing setenforce 0 to see if that solves the problem. If that works, you need to configure SELinux to allow the nginx user to write to the cache directory, and then re-enable SELinux. SELinux does provide some good security, so it would be best to figure out how to configure it properly, as opposed to turning it off completely.