Search code examples
symfonyfile-permissionssymfony4

Symfony 4 file permissions of var directory change every time


I am setting up a new server and installed Ubuntu 18.04 in combination with Apache2. My project is stored in /var/www/project. In apache2.conf I added

<Directory /var/www/project/>
    AllowOverride All
    Order Allow,Deny
    Allow from All
</Directory>

In my virtualhosts file I point to /var/www/project/public When I go to the Ip address of my server I see my project and everything works, except one thing:

whenever I clear the cache with php bin/console cache:clear the permissions of my directory var are messed up which results in errors in the production environment.

I can fix this with:

chmod -R 777 var/

But the problem returns wheneven I clear the cache again. I tried with different users including root, but always the same problem. I do not understand what is causing this. In the documentation on file permissions it says:

In Symfony 3.x, you needed to do some extra work to make sure that your cache directory was writable. But that is no longer true! In Symfony 4, everything works automatically

Well not for me, but what could cause the problem?


Solution

  • The problem

    The cache directory is owned by the user executing the cache:clear command.

    • Lets say your project files are owned by www-data.
    • Clearing the cache with root user
    • Cache is owned by root
    • www-data can't write in cache directory

    Solution

    execute cache:clear using the user owning the files.

    • Login as www-data: su www-data -s /bin/bash
    • clear the cache ./bin/console cache:clear

    Depending on your settings, your www-data user may be different