php RuntimeException: 无法创建缓存目录 (/var/www/sonata/app/cache/dev)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20127884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RuntimeException: Unable to create the cache directory (/var/www/sonata/app/cache/dev)
提问by user2779489
I installed the Sonata admin bundle.
After installation i refresh my page there is the cache problem then i use the following command to remove the cache:
我安装了Sonata 管理包。
安装后我刷新我的页面有缓存问题然后我使用以下命令删除缓存:
rm -rf app/cache app/log
Then I recreate the directory:
然后我重新创建目录:
mkdir app/cache app/log
But I got the following error:
但我收到以下错误:
Runtime Exception: Unable to create the cache directory (/var/www/sonata/app/cache/dev).
运行时异常:无法创建缓存目录 (/var/www/sonata/app/cache/dev)。
回答by l-x
It looks like a file/directory permission problem. The directory has to be writeable by the webserver. After creating the directory you should adjust the permissions with
看起来像是文件/目录权限问题。该目录必须可由网络服务器写入。创建目录后,您应该调整权限
chown -R www-data:www-data app/cache
chown -R www-data:www-data app/log
Or for Symfony 4+:
或者对于Symfony 4+:
chown -R www-data:www-data var
This only works on linux systems. The user and group depends on your distribution. On Debianand Ubuntuthis should be www-data, on CentOSit's afaik apache.
这仅适用于 linux 系统。用户和组取决于您的分布。在Debian和Ubuntu 上这应该是www-data,在CentOS 上它是 afaik apache。
Another solution would be not to delete the whole folders but only their contents via
另一种解决方案是不删除整个文件夹,而只删除它们的内容
$ rm -rf app/log/* app/cache/*
$ rm -rf app/log/* app/cache/*
But please be careful with this command.
但是请小心使用此命令。
回答by juanitourquiza
This solution is correct : https://stackoverflow.com/a/20128013/2400373
此解决方案是正确的:https: //stackoverflow.com/a/20128013/2400373
But is necessary change the 2 commands in Symfony3:
First you should is inside the folder of project:
但是有必要更改 2 个命令Symfony3:首先你应该在项目文件夹中:
$ sudo chown -R www-data:www-data var/cache
$ sudo chown -R www-data:www-data var/logs
After delete the cache:
删除缓存后:
$ sudo rm -rf var/cache/*
$ sudo rm -rf var/logs/*
Regards
问候
回答by Jirson Tavera
I solved it changing user and group of folder var/cacheand var/logs, then I cleaned cache:
我解决了它更改文件夹的用户和组var/cache和var/logs,然后我清理缓存:
sudo chown -R www-data:www-data var/logs
sudo chown -R www-data:www-data var/cache
sudo rm -rf var/logs/* var/cache/*
回答by sjt003
And for centos:
对于centos:
chown -R apache:apache app/cache
if you're coming here for Symfony Help you might have to do this as well if you delete the entire app/logs folder
如果你来这里是为了 Symfony 帮助,如果你删除整个 app/logs 文件夹,你可能也必须这样做
chown -R apache:apache app/logs
回答by Khachornchit Songsaen
回答by Pratik Patel
It is mostly the permission issue.
这主要是许可问题。
I have got resolved it on MAC using command: $ sudo chmod -R 777 <path/to/cache/directory>
我已经使用命令在 MAC 上解决了它: $ sudo chmod -R 777 <path/to/cache/directory>
You might try: $ sudo chmod -R 777 /var/www/sonata/app/cache
你可以试试: $ sudo chmod -R 777 /var/www/sonata/app/cache
回答by user254883
Changing the CHMOD might help but in case the cache annoys you during the deveopment you can just deactivate it. Navigate to your app config file (located in ../app/config/config.yml from your root directory). Scroll to the twig configuration settings (under twig:) and change the cache value (which should be pointing to the cache directory) to false like so:
更改 CHMOD 可能会有所帮助,但如果缓存在开发过程中惹恼了您,您可以停用它。导航到您的应用程序配置文件(位于根目录中的 ../app/config/config.yml 中)。滚动到 twig 配置设置(在 twig: 下)并将缓存值(应该指向缓存目录)更改为 false,如下所示:
twig:
cache: false
If you do not see any cache configuration entry, simply add the line above.
如果您没有看到任何缓存配置条目,只需添加上面的行。
回答by dminer
What is likely happening is that you are trying to create the file under apache/nginx. By default apache or nginx has umask set to 0022.
可能发生的情况是您正在尝试在 apache/nginx 下创建文件。默认情况下,apache 或 nginx 将 umask 设置为 0022。
From: http://www.cyberciti.biz/tips/understanding-linux-unix-umask-value-usage.html
Explain Octal umask Mode 022 And 002
As I said earlier, if the default settings are not changed, files are created with the access mode 666 and directories with 777. In this example:
The default umask 002 used for normal user. With this mask default directory permissions are 775 and default file permissions are 664. The default umask for the root user is 022 result into default directory permissions are 755 and default file permissions are 644. For directories, the base permissions are (rwxrwxrwx) 0777 and for files they are 0666 (rw-rw-rw).
来自:http: //www.cyberciti.biz/tips/understanding-linux-unix-umask-value-usage.html
解释八进制 umask 模式 022 和 002
正如我之前所说,如果不更改默认设置,则创建的文件访问模式为 666,目录为 777。在此示例中:
普通用户使用的默认 umask 002。使用此掩码,默认目录权限为 775,默认文件权限为 664。root 用户的默认 umask 为 022,导致默认目录权限为 755,默认文件权限为 644。对于目录,基本权限为 (rwxrwxrwx) 0777 和对于文件,它们是 0666 (rw-rw-rw)。
You will need to manually set the umask to 0002, and reset it back to its previous setting before you can create the directories.
您需要手动将 umask 设置为 0002,并将其重置为之前的设置,然后才能创建目录。
回答by Tiago Gouvêa
On a maccomputer it will be:
在Mac计算机上,它将是:
$ sudo chown -R _www:_www var/cache
$ sudo chown -R _www:_www var/logs
If none of above work to you, call a "phpinfo()" on your php file and find for "User/Group" value. That's the user group to give permission to.
如果以上都不适合您,请在您的 php 文件中调用“phpinfo()”并找到“用户/组”值。这是要授予权限的用户组。
回答by ToTo
to generalize so everyone can use this two commands can solve everyone problem.
概括一下,大家都可以用这两个命令就可以解决大家的问题。
sudo chown -R $USER var/cache
sudo chown -R $USER var/logs
Further Info: $USERautomatically replaces your username so it can be used by anyone.
更多信息:$USER自动替换您的用户名,以便任何人都可以使用。


