12

My Symfony 2 website has recently been giving me problems when I try to clear the cache. I type the following command in the terminal:

php app/console cache:clear --env=dev

And get the following error:

[ErrorException]                                                                                                                                                                                                                                   
 Warning: rename(/var/www/corpsite/corpsite/app/cache/dev,/var/www/corpsite/corpsite/app/cache/dev_old): Directory not empty in /var/www/corpsite/corpsite/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php line 74  

So I change the permissions on that file to 777, and re-run the clear cache command, which gives me this error:

  [ErrorException]                                                                                                                                                                                                                         
  Warning: unlink(/var/www/corpsite/corpsite/app/cache/dev_old/twig/6b/e9/4491e41b895786689b86f32f446f.php): Permission denied in /var/www/corpsite/corpsite/vendor/symfony/src/Symfony/Component/HttpKernel/Util/Filesystem.php line 100  

I can get round the problem by deleting the 'dev_old' folder, but I want to solve the problem that is causing the issue.

P.S - I am aware the site is running in dev mode. The site has been live for 10 months and this has never been an issue before.

Any help is appreciated!

richelliot
  • 588
  • 2
  • 10
  • 30

3 Answers3

33

You need to get your access rights on both cache & logs folders. To do that, you can follow the indications given here : http://symfony.com/doc/current/book/installation.html#configuration-and-setup

There are several ways, depending on your OS (replace www-data with your apache user):

If it supports chmod +a:

$ rm -rf app/cache/*
$ rm -rf app/logs/*

$ sudo chmod +a "www-data allow delete,write,append,file_inherit,directory_inherit" app/cache app/logs
$ sudo chmod +a "`whoami` allow delete,write,append,file_inherit,directory_inherit" app/cache app/logs

Else, if it supports setfacl (see https://help.ubuntu.com/community/FilePermissionsACLs):

$ sudo setfacl -R -m u:www-data:rwX -m u:`whoami`:rwX app/cache app/logs
$ sudo setfacl -dR -m u:www-data:rwx -m u:`whoami`:rwx app/cache app/logs

Else, put those lines in the beginning of app/console, web/app.php & web/app_dev.php (not recommended):

umask(0002); // This will let the permissions be 0775

// or

umask(0000); // This will let the permissions be 0777
Hugo Briand
  • 1,683
  • 20
  • 27
  • 1
    Thanks for this. I used the second set of commands (setfacl) after removing the app/cache/dev_old folder and it has worked. – richelliot Apr 09 '13 at 11:20
  • @hugo-briand, please expand why not recommend umask(0002)? Security issue? – Dos Oct 29 '14 at 11:37
  • From the indications given in symfony installation, this method is not thread-safe. This means that if you have parallel access to your script (through fpm for instance, or queries + CLI), you might run into issues. From PHP umask doc (http://php.net/manual/en/function.umask.php): Avoid using this function in multithreaded webservers. It is better to change the file permissions with chmod() after creating the file. Using umask() can lead to unexpected behavior of concurrently running scripts and the webserver itself because they all use the same umask. – Hugo Briand Oct 30 '14 at 09:38
  • Thank you so much, you just made my day with solution 2! – Ivan Marjanovic Aug 12 '16 at 10:35
2

in Symfony3 folders with cache moved from app to var, so the command will be:

$ rm -rf var/cache/*
$ rm -rf var/logs/*
$ sudo chmod +a "www-data allow delete,write,append,file_inherit,directory_inherit" var/cache var/logs
$ sudo chmod +a "`whoami` allow delete,write,append,file_inherit,directory_inherit" var/cache var/logs
Stan Fad
  • 1,124
  • 12
  • 23
  • I stopped using Symfony 2 ages ago and don't miss it one bit. It makes the easy things difficult and the difficult things easy. Every project was a headache. What is Symfony3 like? – richelliot Jun 29 '17 at 13:06
-5

You need to get your access rights on both cache & logs folders. To do that, you can follow the indications given here :

  • 1
    Given where? :) Before you post just that link: it would be better to post the link *and* have the relevant content in the answer itself, as links may become invalid. – Benjamin W. May 15 '16 at 22:47