I regularly get this error message:
1 exception(s): Exception #0 (Exception): Warning: file_put_contents(/var/www/html/magento/var/cache//mage-tags/mage---365_COMPILED_CONFIG): failed to open stream: Permission denied in /var/www/html/magento/vendor/colinmollenhour/cache-backend-file/File.php on line 663
To fix it, I remove the cache :
rm -rf var/cache/* var/di/* var/generation/* var/page_cache/* var/view_preprocessed/* pub/static/frontend/*
But it always comes back regularly, although I've set the permissions for two users in development mode :
find var vendor pub/static pub/media app/etc -type f -exec chmod g+w {} \;
sudo find var vendor pub/static pub/media app/etc -type f -exec chmod g+w {} \;
sudo find var vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} \;
sudo chown -R :www-data .
chmod u+x bin/magento
Even after cleaning the cache this way :
rm -rf var/cache/* var/di/* var/generation/* var/page_cache/* var/view_preprocessed/* pub/static/frontend/*
php bin/magento cache:clean
php bin/magento setup:upgrade
php bin/magento setup:di:compile
Is it a bug with Magento 2.2 or did I miss something ?
4 Answers 4
I think its not a bug, I experienced the same after running the cache:clean command as root user.
So by applying the file permission again this issue has been fixed. http://devdocs.magento.com/guides/v2.2/install-gde/prereq/file-sys-perms-over.html
So be careful don't clean the cache as root user.
-
1I never clean Magento2 cache as root user, I always do it with my own user belonging to www-data group, there should be another reason to this problem.DevonDahon– DevonDahon2017年10月24日 14:14:13 +00:00Commented Oct 24, 2017 at 14:14
I think you might be running magento2 cron as sudo user which is generating those files for you, can you check if you have crontab as sudo and without, go to your working directory and write
crontab -l
sudo crontab -l
it will show you.
if you do have running it as sudo you can remove it by typing
sudo php bin/magento cron:remove
or manually through sudo crontab -e
and make sure the cron runs as user that generates webserver friendly files.
P.S first post so go easy on me :D
I think it happens when you execute a cli command and the ownership of the files is set that of your user rather than the www-data user. You can clear the cache and it'll go away for a bit, until you've kicked off a cli command that generates a cache file.
One potential fix I can think of is to set fpm to use the same group as your own user.
Check your code base or 3rd party module, if programmatically cache is clearing, In My case, cache was getting cleared in every 2 minutes by CRON so i was getting this issue.
phprunning aswww-data??chown -R :www-data var/cache/mage-tagsto resolve the issue. It still reoccurs each time a condition from my original post happens.