Error with Symfony's cache and permissions

Viewed 13855

I got this continuous error with cache. Something with permissions but I can't figure what's going on. In my local environment when I go a php bin/console cache:clear -e=dev it return me this

  [Symfony\Component\Filesystem\Exception\IOException]                                                                                               
  Failed to remove file "/project/var/cache/de~/pools/ORsqbHaOKl/L/K/iZULk48B-k00dzIKC2qg": unlink(/project/var/cache/de~/pools/ORsqbHaOKl/L/K/iZULk48B-k00dzIKC2qg): Permission denied.    

So I need to make first a chmod -R 777 var/, then again the clear cache and it works. But when I run the website it return me

Failed to create "/project/var/cache/dev/tcpdf": mkdir(): Permission denied.

So I need again to make chmod -R 777 var/

In the production server without erasing anything sometimes i get this error

Warning: rename(C:\project\var\cache\prod/doctrine/orm/Proxies\__CG__AppBundleEntitySomeEntity.php.5a142ad84e8464.47105642,C:\project\var\cache\prod/doctrine/orm/Proxies\__CG__AppBundleEntitySomeEntity.php): Access is denied. (code: 5)

error in vendor\doctrine\common\lib\Doctrine\Common\Proxy\ProxyGenerator.php (line 309 rename())

    $tmpFileName = $fileName . '.' . uniqid('', true);
    file_put_contents($tmpFileName, $proxyCode);
    @chmod($tmpFileName, 0664);
    rename($tmpFileName, $fileName);
}

Local environment: debian 9 Production environment: windows server 2008

2 Answers

You need to set the var folder permissions as suggested by this symfony article.

Run the following commands in your project directory and you will not come across any permission related issues on cache and logs

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 -dR -m u:"$HTTPDUSER":rwX -m u:$(whoami):rwX var
sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:$(whoami):rwX var

I've just played around with it a bit and noticed that every time you call

bin/console cache:clear --env=prod

The user of the folder /prod will be changed to the user that is executing the call. The scripts deletes the folder and creates a new own, but that will be done be the current user set.

You can stop the script from creating a new folder by using the --no-warmup option. With that, I've fixed my problem.

bin/console cache:clear --env=prod --no-warmup

For that to work you need to fix the current situation once, at best by deleting all folders and files within the /var/cache directory.

Related