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 ?

  • is your php running as www-data??
    – MagenX
    Oct 24, 2017 at 8:03
  • I guess it does : I tested it by executing the command line sudo -u www-data php -r 'shell_exec("touch a");' in magento2 root folder, and it created a file with www-data as owner and group.
    – DevonDahon
    Oct 24, 2017 at 8:20
  • 4
    I have a similar thread with more details here. I've been running chown -R :www-data var/cache/mage-tags to resolve the issue. It still reoccurs each time a condition from my original post happens.
    – jtlindsey
    Nov 10, 2017 at 13:17
  • you can follow this post to avoid permission denied: mrvts.wordpress.com/2018/08/11/…
    – V.Tran
    Aug 12, 2018 at 5:08
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Aug 12, 2018 at 16:23

3 Answers 3


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.

  • 1
    I 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
    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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.