Martin Tovmassian
Martin Tovmassian

Reputation: 1458

At some point Symfony cache adapter failed to save key

I am running a Symfony application on a Docker container with this environment specs:

I am recurrently facing a strange behavior where the Symfony cache adapter is no more able to save data on the filesystem due to permission issue. Here an example of the warning emitted:

cache.WARNING: Failed to save key "App%5CController%5CAjaxController" of type array: fopen(/var/www/symfony/var/cache/prod/pools/system/vlNLJCjFzq/d8a651379e96): Failed to open stream: Permission denied {"key":"App%5CController%5CAjaxController","exception":"[object] (ErrorException(code: 0): fopen(/var/www/symfony/var/cache/prod/pools/system/vlNLJCjFzq/d8a651379e96): Failed to open stream: Permission denied at /var/www/symfony/vendor/symfony/cache/Traits/FilesystemCommonTrait.php:99)","cache-adapter":"Symfony\\Component\\Cache\\Adapter\\PhpFilesAdapter"} []

Indeed when I inspect the /var/www/symfony/var/cache/prod file tree I can see that some files / directories like pools are owned byroot whereas they must be owned by www-data user:

drwxr-xr-x  1 www-data www-data   4096 Apr 13 18:05 .
drwxr-xr-x  1 www-data www-data   4096 Mar 31 09:11 ..
-rw-r--r--  1 root     root        743 Apr 13 18:05 App_KernelProdContainer.php
-rw-r--r--  1 root     root          0 Apr 13 18:05 App_KernelProdContainer.php.lock
-rw-r--r--  1 root     root     314018 Apr 13 18:05 App_KernelProdContainer.php.meta
-rw-r--r--  1 root     root     148907 Apr 13 18:05 App_KernelProdContainer.preload.php
drwxr-xr-x  2 root     root      86016 Apr 13 18:05 Container6ZI03W9
-rw-r--r--  1 root     root      14072 Apr 13 18:05 annotations.map
drwxr-xr-x  3 root     root       4096 Apr 13 18:05 doctrine
drwxr-xr-x  4 root     root       4096 Apr 13 18:05 pools
drwxr-xr-x  2 www-data www-data   4096 Apr 13 18:05 translations
drwxr-xr-x 39 www-data www-data   4096 Apr 13 19:17 twig
-rw-r--r--  1 www-data www-data 169603 Apr 13 18:05 url_generating_routes.php
-rw-r--r--  1 www-data www-data  31306 Apr 13 18:05 url_generating_routes.php.meta
-rw-r--r--  1 www-data www-data 181199 Apr 13 18:05 url_matching_routes.php
-rw-r--r--  1 www-data www-data  31306 Apr 13 18:05 url_matching_routes.php.meta

What I can guess is that this state is not the original one since:

drwxr-xr-x 1 www-data www-data   4096 Apr 14 13:39 .
drwxr-xr-x 1 www-data www-data   4096 Mar 31 09:11 ..
-rw-r--r-- 1 www-data www-data    743 Apr 14 13:39 App_KernelProdContainer.php
-rw-r--r-- 1 www-data www-data      0 Apr 14 13:39 App_KernelProdContainer.php.lock
-rw-r--r-- 1 www-data www-data 314018 Apr 14 13:39 App_KernelProdContainer.php.meta
-rw-r--r-- 1 www-data www-data 148907 Apr 14 13:39 App_KernelProdContainer.preload.php
drwxr-xr-x 2 www-data www-data  86016 Apr 14 13:39 Container6ZI03W9
-rw-r--r-- 1 www-data www-data  14072 Apr 14 13:39 annotations.map
drwxr-xr-x 3 www-data www-data   4096 Apr 14 13:39 doctrine
drwxr-xr-x 4 www-data www-data   4096 Apr 14 13:39 pools
-rw-r--r-- 1 www-data www-data 181199 Apr 14 13:39 url_matching_routes.php
-rw-r--r-- 1 www-data www-data  31306 Apr 14 13:39 url_matching_routes.php.meta

So this lets me assume that this files have necessarily been modified by the php-fpm master process which is the one and only in the container executed with the root user.

But why, I have no clue...

Has anyone encountered this behavior before? How to fix it?

Upvotes: 1

Views: 1030

Answers (1)

Pierre Alexaline
Pierre Alexaline

Reputation: 1

See the response of Aleem here for give the right permission to /var:

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

You can verify in /etc/apache2/envvars if group and user are correctly set to www-data.

Upvotes: 0

Related Questions