Error with Symfony's cache and permissions

11,989

Solution 1

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

Solution 2

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.

Share:
11,989
Braian Mellor
Author by

Braian Mellor

Updated on June 04, 2022

Comments

  • Braian Mellor
    Braian Mellor almost 2 years

    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

  • Braian Mellor
    Braian Mellor over 6 years
    hey, thanks for the answer but this does not solve my problem: $ php bin/console cache:clear --env=dev --no-warmup [RuntimeException] Unable to write in the "/project/var/cache/dev" directory
  • KhorneHoly
    KhorneHoly over 6 years
    @BraianMellor you need to fix your permission for the first time by your self, at best by just deleting the whole /prod and /dev folder within the cache directory.
  • Braian Mellor
    Braian Mellor over 6 years
    Well following your sugest I made this: chown -R www-data and sudo chgrp -R www-data for every folder inside var (cache, logs, sessions). Then run the websit and the make php bin/console cache:clear --env=dev --no-warmup [UnexpectedValueException] The stream or file "/project/var/logs/dev.log" could not be opened: failed to open stream: Permission denied. The dev.log file is www-data owner.
  • KhorneHoly
    KhorneHoly over 6 years
    @BraianMellor Check the write permission on the log folder and files, most likely the write permissions are missing. Also check the documentation
  • Braian Mellor
    Braian Mellor over 6 years
    drwxr-xr-x for logs folder and -rw-r--r-- for dev.log file. What's your suggestion?
  • KhorneHoly
    KhorneHoly over 6 years
    @BraianMellor Can you delete the log files and then start the website again? After that, try to clear the session and then try again.
  • Braian Mellor
    Braian Mellor over 6 years
    The only way that manage to do it is to manually delete the dev, logs and sessions folders and run the website. the clear:cache do not work
  • KhorneHoly
    KhorneHoly over 6 years
  • Braian Mellor
    Braian Mellor over 6 years
    I tried that bu terminan didn't like 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)
  • Aleem
    Aleem over 6 years
    Ok. This command is just trying to get the apache/nginx user. Mostly it would be "www-data" if you haven't changed. You can also confirm that in /etc/apache2/envvars. You would see "export APACHE_RUN_USER = <USERNAME>". Once you confirm the apache user come back and run the other two commands. replace $HTTPDUSER with your apache username.
  • Braian Mellor
    Braian Mellor over 6 years
    I figured how to solve it with this answer, but I'll edit some things to make it work in my case