Unable to write cache symfony2

2020-04-14 20:23发布

问题:

RuntimeException in ClassCollectionLoader.php line 239: 

Failed to write cache file "/var/www/fareast/app/cache/dev/classes.php".

I have this error when I run my project in symfony2. I have nginx as my web server and centOS 7 as my OS. I tried deleting the cache folder manually and by

php app/console cache:clear

then I did a bad practice to app/cache and app/logs

chmod -R 777 

but I have the same error. /var/log/nginx/error.log is also empty. Do you recommend other solutions? Or do I have to install some php5 libraries for cache?

回答1:

Setting 777 permissions do nothing, because when webserver user write folders to it, the folders belong to this user, and you, as command line user haven't access to them, and vice versa. Set permissions in correct way: http://symfony.com/doc/current/book/installation.html#book-installation-permissions



回答2:

In symfony3 i've faced the same issue...

So, problem would not solve only using "cache clear"

You have to change (R/W)permission of cache folder in your project

For ex: "projectname/var/cache"

Now refresh...

If still issue is coming, change(R/W)permission of logs folder in your project

For ex: "projectname/var/logs"


回答3:

You have to use the following script to determine your web server user and grant the needed permissions:

rm -rf var/cache/*
rm -rf var/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 chmod -R +a "$HTTPDUSER allow delete,write,append,file_inherit,directory_inherit" var
sudo chmod -R +a "`whoami` allow delete,write,append,file_inherit,directory_inherit" var