[symfony] symfony2 : failed to write cache directory

I have had to use the

app/console cache:clear  command

to solve a problem when generating an entity.

I am now unable to load my homepage on :

  http://localhost/projet_etienne/web/app_dev.php

it says :

RuntimeException: Failed to write cache file "/var/www/projet_etienne/app/cache/dev/classes.php".

I don't understand much about this cache business!

In my app/cache folder, I got a dev, a dev_new, a dev_old folder. Is that normal?

the

app/console cache:clear

generates by the way a :

[ErrorException] Warning: rename(/var/www/projet_etienne/app/cache/dev,/var/www/projet_etien
ne/app/cache/dev_old): Directory not empty in /var/www/projet_etienne/vendo
r/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearComm
and.php line 77

please help!

This question is related to symfony

The answer is


If the folder is already writable so thats not the problem.

You can also just navigate to /www/projet_etienne/app/cache/ and manualy remove the folders in there (dev, dev_new, dev_old).

Make sure to SAVE a copy of those folder somewhere to put back if this doesn't fix the problem

I know this is not the way it should be done but it worked for me a couple of times now.


if symfony version less than 2.8

_x000D_
_x000D_
sudo chmod -R 777 app/cache/*
_x000D_
_x000D_
_x000D_

if symfony version great than or equal 3.0

_x000D_
_x000D_
sudo chmod -R 777 var/cache/*
_x000D_
_x000D_
_x000D_


i executed:

ps aux | grep apache

and got something like that:

root     28147  0.0  5.4 326336 27024 ?        Ss   20:06   0:00 /usr/sbin/apache2 -k start
www-data 28150  0.0  1.3 326368  6852 ?        S    20:06   0:00 /usr/sbin/apache2 -k start
www-data 28151  0.0  4.4 329016 22124 ?        S    20:06   0:00 /usr/sbin/apache2 -k start
www-data 28152  0.1  6.0 331252 30092 ?        S    20:06   0:00 /usr/sbin/apache2 -k start
www-data 28153  0.0  1.3 326368  6852 ?        S    20:06   0:00 /usr/sbin/apache2 -k start
www-data 28154  0.0  1.3 326368  6852 ?        S    20:06   0:00 /usr/sbin/apache2 -k start
www-data 28157  0.0  1.3 326368  6852 ?        S    20:06   0:00 /usr/sbin/apache2 -k start
user     28297  0.0  0.1  15736   924 pts/4    S+   20:12   0:00 grep --color=auto apache

so my user with no access turned out to be www-data thus i executed commands:

sudo chown -R www-data app/cache
sudo chown -R www-data app/logs

and it solved access errors.

Never-ever use unsecure 777 for solving specific access probles:

sudo chmod -R 777 app/cache
sudo chmod -R 777 app/logs

Just use this acl cmd, next time the files inside var are created it will have the r/w/x permission for www-data user.

cd var 
rm -rf *
cd ..
setfacl -d -m u:www-data:rwx var

Cmd explanation:

setfacl -> Set acl command    
-d -> default behavior    
-m -> modify 
u:www-data: -> for user 
rwx -> adding permissions  
var -> on the folder

You probably aborted a clearcache halfway and now you already have an app/cache/dev_old.

Try this (in the root of your project, assuming you're on a Unixy environment like OS X or Linux):

rm -rf app/cache/dev*


Maybe you forgot to change the permissions of app/cache app/log

I'm using Ubuntu so

sudo chmod -R 777 app/cache
sudo chmod -R 777 app/logs
sudo setfacl -dR -m u::rwX app/cache app/logs

Hope it helps..


I move the whole directory from my Windows installation to a unix production server and I got the same error. To fix it, I just ran these two lines in unix and everything started to run fine

rm -rf app/cache/*
rm -rf app/logs/*

If you face this error when you start Symfony project with docker (my Symfony version 5.1). Or errors like these:

Uncaught Exception: Failed to write file "/var/www/html/mysite.com.local/var/cache/dev/App_KernelDevDebugContainer.xml"" while reading upstream

Uncaught Warning: file_put_contents(/var/www/html/mysite.com.local/var/cache/dev/App_KernelDevDebugContainerDeprecations.log): failed to open stream: Permission denied" while reading upstream

Fix below helped me.

In Dockerfile for nginx container add line:

RUN usermod -u 1000 www-data

In Dockerfile for php-fpm container add line:

RUN usermod -u 1000 www-data

Then remove everything in directories "/var/cache", "/var/log" and rebuild docker's containers.


Most likely it means that the directory and/or sub-directories are not writable. Many forget about sub-directories.

Symfony 2

chmod -R 777 app/cache app/logs

Symfony 3 directory structure

chmod -R 777 var/cache var/logs

Additional Resources

Permissions solution by Symfony (mentioned previously).

Permissions solution by KPN University - additionally includes an screen-cast on installation.

Note: If you're using Symfony 3 directory structure, substitute app/cache and app/logs with var/cache and var/logs.