32
votes

I have installed Symfony 2.0.7 on Ubuntu 11.10. I have this problem:

I changed the permissions of the cache e logs directories using the following commands:

chmod 777 -R cache
chmod 777 -R logs

and all functions as I was expecting.

Then I made:

app/console cache:clear

Clearing the cache for the dev environment with debug true

permissions of cache/dev change to 755 and symfony cannot write in that:

RuntimeException: Failed to write cache file "/var/www/myapp/app/cache/dev/classes.php".

in /var/www/myapp/app/bootstrap.php.cache line 1079 at ClassCollectionLoader::writeCacheFile() in /var/www/myapp/app/bootstrap.php.cache line 1017 at ClassCollectionLoader::load() in /var/www/myapp/app/bootstrap.php.cache line 682 at Kernel->loadClassCache() in /var/www/myapp/web/app_dev.php line 23

How can I solve this apparent bug in Symfony 2?

4
I avoided this issue by adding --no-warmup when clearing the cache so the dev folder won't be created by the CLI user. Then, first time you request a page within your browser, the dev folder is created by the www-data user. I ended using the solution posted by Federico Gallo, below. - VMC

4 Answers

52
votes

See the Setting up Permissions sidenote in the Configuration and Setup section. Use the ACL approach with setfacl.

e.g.

sudo setfacl -R -m u:apache:rwX -m u:`whoami`:rwX app/cache app/logs
sudo setfacl -dR -m u:apache:rwX -m u:`whoami`:rwX app/cache app/logs

(where apache is your HTTPD user)

7
votes

Well, I'm using setfacl to avoid that issues but I still having problems. I'm working on a standard user directory so this do not work for me:

APACHEUSER=`ps aux | grep -E '[a]pache|[h]ttpd' | grep -v root | head -1 | cut -d\  -f1`
sudo setfacl -R -m u:$APACHEUSER:rwX -m u:`whoami`:rwX app/cache app/logs
sudo setfacl -dR -m u:$APACHEUSER:rwX -m u:`whoami`:rwX app/cache app/logs

If you too, do this: (as privileged user if necessary of course )

sudo rm -rf app/cache app/logs

Then as NORMAL user create both directories and then apply the commands below as NORMAL user too (without sudo):

exit (from root if necessary)
mkdir app/cache app/logs

APACHEUSER=`ps aux | grep -E '[a]pache|[h]ttpd' | grep -v root | head -1 | cut -d\  -f1`
setfacl -R -m u:$APACHEUSER:rwX -m u:`whoami`:rwX app/cache app/logs
setfacl -dR -m u:$APACHEUSER:rwX -m u:`whoami`:rwX app/cache app/logs

Note: remember that there are two spaces between -d\ and -f1

Good luck!

0
votes

In my case I just do as root user:

sudo -u apache app/console ca:cl

apache - this will vary depending on the owner your web directory. defaults to apache assuming you are using apache for your web server.

0
votes

By looking at your posts I got to this page

http://symfony.com/doc/current/setup/file_permissions.html

Then I did this:

HTTPDUSER=ps axo user,comm | 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 var

sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:whoami:rwX var

And it solved my problem