Symfony Apache 2 : Unable to create the cache directory /var/cache - symfony

I know this issue has been reported multiple times, but I tried every single solution and nothing seems to work.
I am running Symfony 3 on a Debian 9 Stretch, and theres a permission issue that I can't fix
cat /var/log/apache2/project_error.log
PHP Fatal error: Uncaught RuntimeException: Unable to create the cache directory (/var/www/mobileoutfitters.fr/public_html/var/cache/prod)\n in /var/www/mobileoutfitters.fr/public_html/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php:676\nStack
In /etc/apache2/envvars :
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data
My user is actually part of this group. I tried all the chown -R 777 commands possible, and also as said in the Symfony documentation, these 2 commands :
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
I tried to delete the var folder, delete its content, clear the cache... But still this error.

Turns out that my Symfony database was created but empty.
After running : bin/console doctrine:schema:update --force it now works

I had about the same issue, for me it worked after I disabled selinux on my local Centos virtualbox VM with setenforce Permissive.

Related

symfony classes.map failed to open stream: No such file or directory

I get this error when I tried to deploy symfony in a Docker swarm node
ContextErrorException in Kernel.php line 338:
Warning: file_put_contents(/home/backend_microservice/private_webapp/var/cache/dev/classes.map): failed to open stream: No such file or directory
I tried to remove the cache manually, I remove the vendor directory and I generate a new one with composer, but still give the same error.
The same config works fine with only docker engine but does not work in swarm mode.
OS : CentOS 7.3
Virtualization : Docker version 17.06.0-ce, build 02c1d87 Swarm mode
Symfony :3.2.9
Any help or suggestion??
thank you in advance.
OK, I managed to generate the cache. I was not able to write to the cache directory even when I change the permission to the 777. So, I just remove the following lines from the docker start script - which is recommended by the symfony doc :-( :
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
&& setfacl -R -m u:"$HTTPDUSER":rwX -m u:whoami:rwX var
&& setfacl -dR -m u:"$HTTPDUSER":rwX -m u:whoami:rwX var

SULU CMS installation errors with Laravel Homestead

Follow this document http://docs.sulu.io/en/latest/book/getting-started.html at the end of installation process I got this error:
Target: cache
cache:clear ({"--no-optional-warmers":true,"--no-debug":true,"--no-interaction":true})
// Clearing the admin cache for the dev environment with debug true
[Symfony\Component\Filesystem\Exception\IOException]
Failed to remove directory "/home/vagrant/Code/sulu/var/cache/admin/de~/doctrine": .
sulu:build [-D|--nodeps] [--destroy] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-s|--shell] [--process-isolation] [-e|--env ENV] [--no-debug] [--] <command> [<target>]
Before that I was trying to set up file permissions with this:
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 -R -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX var/cache var/logs var/uploads var/uploads/* web/uploads web/uploads/* var/indexes var/sessions
sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX var/cache var/logs var/uploads var/uploads/* web/uploads web/uploads/* var/indexes var/sessions
and also had these errors:
setfacl: web/uploads: Operation not supported
setfacl: web/uploads/media: Operation not supported
setfacl: web/uploads/media: Operation not supported
My host OS : Ubuntu 16.04
Vagrant : v.1.9.3
VirtualBox : v.5
Homestead: v.5.2.1
Does anyone have successful installation Sulu CMS with Homestead?
What is the option to solve these my issues?
Sulu CMS looks very promising but unfortunately I could not install it locally still with many attempts.
UPDATE
After comment of Daniel I've tried other way to install Sulu but again got error at the very end of installation:
Executing builders
==================
Target: cache
cache:clear ({"--no-optional-warmers":true,"--no-debug":true,"--no-interaction":true})
// Clearing the admin cache for the dev environment with debug true
[Symfony\Component\Filesystem\Exception\IOException]
Failed to remove directory "/home/vagrant/Code/sulu/app/cache/admin/de~/annotations": .
Opened test.app in browser I see this error:
Fatal error: Uncaught
Symfony\Component\Debug\Exception\FatalThrowableError: Call to a member function getLocale() on null in /home/vagrant/Code/sulu/vendor/sulu/sulu/src/Sulu/Bundle/WebsiteBundle/Twig/Content/ContentPathTwigExtension.php on line 70
Symfony\Component\Debug\Exception\FatalThrowableError: Call to a member function getLocale() on null in /home/vagrant/Code/sulu/vendor/sulu/sulu/src/Sulu/Bundle/WebsiteBundle/Twig/Content/ContentPathTwigExtension.php on line 70
Tried to delete cache folders manually - the same error.
All console commands work fine.
Any ideas?
P.S. I have a good experience of installation other Symfony-based applications under Homestead and all went smooth basically (Sylius, eZ etc.). So I am very surprised....
To get around this, you need to remove this folder from the host machine, because it's an artifact from the previous process of trying to set things up and it's in god-mode, created by a no-longer existing almighty entity.
vagrant destroy (needed to release filesystem locks)
Remove folder on host: rm -rf .../sulu/app/cache/admin
type: nfs on folder binding
vagrant up
Should work then, encountered this same problem recently.
If you're using Homestead Improved, use the built-in "sulu" project type for a decent Nginx auto-setup:
sites:
- map: homestead.app
to: /home/vagrant/Code/Project/web
type: sulu

Unable to write cache symfony2

RuntimeException in ClassCollectionLoader.php line 239:
Failed to write cache file "/var/www/fareast/app/cache/dev/classes.php".
I have this error when I run my project in symfony2.
I have nginx as my web server and centOS 7 as my OS.
I tried deleting the cache folder manually and by
php app/console cache:clear
then I did a bad practice to app/cache and app/logs
chmod -R 777
but I have the same error.
/var/log/nginx/error.log is also empty.
Do you recommend other solutions?
Or do I have to install some php5 libraries for cache?
Setting 777 permissions do nothing, because when webserver user write folders to it, the folders belong to this user, and you, as command line user haven't access to them, and vice versa. Set permissions in correct way: http://symfony.com/doc/current/book/installation.html#book-installation-permissions
In symfony3 i've faced the same issue...
So, problem would not solve only using "cache clear"
You have to change (R/W)permission of cache folder in your project
For ex: "projectname/var/cache"
Now refresh...
If still issue is coming, change(R/W)permission of logs folder in your project
For ex: "projectname/var/logs"
You have to use the following script to determine your web server user and grant the needed permissions:
rm -rf var/cache/*
rm -rf var/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 chmod -R +a "$HTTPDUSER allow delete,write,append,file_inherit,directory_inherit" var
sudo chmod -R +a "`whoami` allow delete,write,append,file_inherit,directory_inherit" var

Unable to write cache

Hi all im working with Symfony2. I dont have problems with the project in my computer but when i upload the files in the web server, fails for cache permissions.
I set the permissions in my computer with this steps:
$ rm -rf app/cache/*
$ rm -rf app/logs/*
$ APACHEUSER=`ps aux | grep -E '[a]pache|[h]ttpd' | grep -v root | head -1 | cut -d\ -f1`
$ sudo setfacl -R -m u:$APACHEUSER:rwX -m u:`whoami`:rwX app/cache app/logs
$ sudo setfacl -dR -m u:$APACHEUSER:rwX -m u:`whoami`:rwX app/cache app/logs
As sugest The Doc.
So, upload the app/cache and app/logs (empty) folders to my webserver.
Whe in try access to the web project, Symfony says:
Fatal error: Uncaught exception 'RuntimeException' with message 'Could
not create cache directory
"/home/coleman/public_html/apps/app/cache/prod/annotations"
I check the folder with Filezilla, and the permissions are 666 (read and write for all).
I dont know that are wrong.
Any ideas ?.
See this link:
Configuration and Setup
It's pretty common issue. You need to configure permissions though ACL...
Is your project name "apps" ? If not, seems like you put your web folder content at root without modify the relative paths in your app.php

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!
For a GOOD and definite solution see the Setting up Permissions section in Installing and Configuring Symfony section :
Setting up Permissions
One common issue when installing Symfony is that the app/cache and
app/logs directories must be writable both by the web server and the
command line user. On a UNIX system, if your web server user is
different from your command line user, you can try one of the
following solutions.
Use the same user for the CLI and the web server
In development environments, it is a common practice to use the same
UNIX user for the CLI and the web server because it avoids any of
these permissions issues when setting up new projects. This can be
done by editing your web server configuration (e.g. commonly
httpd.conf or apache2.conf for Apache) and setting its user to be the
same as your CLI user (e.g. for Apache, update the User and Group
values).
Using ACL on a system that supports chmod +a
Many systems allow you to use the chmod +a command. Try this first,
and if you get an error - try the next method. This uses a command to
try to determine your web server user and set it as HTTPDUSER:
$ rm -rf app/cache/*
$ rm -rf app/logs/*
$ HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1`
$ sudo chmod +a "$HTTPDUSER allow delete,write,append,file_inherit,directory_inherit" app/cache app/logs
$ sudo chmod +a "`whoami` allow delete,write,append,file_inherit,directory_inherit" app/cache app/logs
Using ACL on a system that does not support chmod +a
Some systems don't support chmod +a, but do support another utility
called setfacl. You may need to enable ACL support on your partition
and install setfacl before using it (as is the case with Ubuntu). This
uses a command to try to determine your web server user and set it as
HTTPDUSER:
$ HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1`
$ sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX app/cache app/logs
$ sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX app/cache app/logs
For Symfony 3 it would be:
$ HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1`
$ sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX var/cache var/logs
$ sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX var/cache var/logs
If this
doesn't work, try adding -n option.
Without using ACL
If none of the previous methods work for you, change the umask so that
the cache and log directories will be group-writable or world-writable
(depending if the web server user and the command line user are in the
same group or not). To achieve this, put the following line at the
beginning of the app/console, web/app.php and web/app_dev.php files:
umask(0002); // This will let the permissions be 0775
// or
umask(0000); // This will let the permissions be 0777
Note that using the ACL is recommended when you have access to them on your server
because changing the umask is not thread-safe.
http://symfony.com/doc/current/book/installation.html#checking-symfony-application-configuration-and-setup
source :
Failed to write cache file "/var/www/myapp/app/cache/dev/classes.php" when clearing the cache
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.
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.
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/*
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
if symfony version less than 2.8
sudo chmod -R 777 app/cache/*
if symfony version great than or equal 3.0
sudo chmod -R 777 var/cache/*
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.
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

Resources