SplFileInfo::openFile(/app/tmp/cache/persistent/cake_core_cake_console_):failed to open stream:Permission denied in /lib/.../FileEngine.php line 293
Asked Answered
S

8

21

I am working on a CakePHP 2 project. It originally started out in 2.0.x and then recently migrated to 2.1.0. Throughout the whole development process, I have been receiving the error message below.

It pops up at the top of the page unpredictably. It can be when I am just viewing different pages, or even after I add a record to the database (yet the record properly saves).

Warning:
SplFileInfo::openFile(/var/www/cake_prj/app/tmp/cache/persistent/cake_core_cake_console_): 
failed to open stream: 
Permission denied in 
     /var/www/cake_prj/lib/Cake/Cache/Engine/FileEngine.php on line 293

I recursively set the owner and group of the tmp folder to apache, and still received the message. In addition, I then recursively set the permissions to read, write, and execute for all (chmod 777). The error message still pops up.

Even after changing both the owner, group, and permissions, the file in question:

cake_prj/app/tmp/cache/persistent/cake_core_cake_console_

will have its owner and group set back to root, and its permissions set back to default.

What could be causing this problem? Is there a way to ensure that every time this file is generated, that it will always have be apache:apache with read/write/execute permissions?

Squelch answered 16/3, 2012 at 7:32 Comment(0)
V
15

There was a bug report there http://cakephp.lighthouseapp.com/projects/42648/tickets/2172 but it was considered as not being a bug.

What I personaly noticed is that some file owner may be modified when you use the cake script in the console (for instance to make a bake). The modified files then belong to the user you use in the console.

Would this mean you call cake while being root ? Or do you have any root cron job that calls a Cake shell script ?

Personaly I have now the habit to chmod the whole tmp folder content back to the apache user after having used the cake script and it seems to prevent the warning to appear.

Vogler answered 16/3, 2012 at 10:17 Comment(4)
nIcO, thanks for the response. I do call cake while logged in as root. Although I set my whole tmp folder to have its owner and group as apache:apache, and then chmod 777 the whole folder recursively. And then I ran cake bake but afterwards, the permissions were still correctly set. So the cake script doesn't seem to be causing it.Squelch
I'm not sure it regenerates files everytime you use the cake script. So it may not always recreate files belonging to root even if this is the user you use. By the way, you should probably better not use root to call cake.Vogler
Hasn't happened recently and I haven't ran cake recently so I am wondering if that was the problem. I will keep following this and see if it happens again.Squelch
Just an update. After going a long time without seeing the error message, I recently baked some files again and it started appearing again. So I believe your solution is correct, but I will wait until I understand more about the other solution.Squelch
C
27

You can resolve this by adding a mask to your config in core.php

Cache::config('default', array(
    'engine' => 'File',
    'mask' => 0666,
));
Caracara answered 23/4, 2012 at 19:51 Comment(3)
Do you think you could explain to me what this does?Squelch
I've just run into the same issue and this seemed to fix it for me. As Mark Story explains: "Sounds like permissions issues. This can happen when your cli and apache users are different, or your umask is not permissive enough." The workaround seems to be to use the 'mask' option as described above and here cakephp.lighthouseapp.com/projects/42648/tickets/2172Differentiation
I think this should go in bootstrap.php not core.phpUnburden
V
15

There was a bug report there http://cakephp.lighthouseapp.com/projects/42648/tickets/2172 but it was considered as not being a bug.

What I personaly noticed is that some file owner may be modified when you use the cake script in the console (for instance to make a bake). The modified files then belong to the user you use in the console.

Would this mean you call cake while being root ? Or do you have any root cron job that calls a Cake shell script ?

Personaly I have now the habit to chmod the whole tmp folder content back to the apache user after having used the cake script and it seems to prevent the warning to appear.

Vogler answered 16/3, 2012 at 10:17 Comment(4)
nIcO, thanks for the response. I do call cake while logged in as root. Although I set my whole tmp folder to have its owner and group as apache:apache, and then chmod 777 the whole folder recursively. And then I ran cake bake but afterwards, the permissions were still correctly set. So the cake script doesn't seem to be causing it.Squelch
I'm not sure it regenerates files everytime you use the cake script. So it may not always recreate files belonging to root even if this is the user you use. By the way, you should probably better not use root to call cake.Vogler
Hasn't happened recently and I haven't ran cake recently so I am wondering if that was the problem. I will keep following this and see if it happens again.Squelch
Just an update. After going a long time without seeing the error message, I recently baked some files again and it started appearing again. So I believe your solution is correct, but I will wait until I understand more about the other solution.Squelch
D
7

Instead of setting giving read/write access to everyone on the tmp/cache directory I did this:

chgrp -R www-data app/tmp
chmod -R g+rw app/tmp 
find app/tmp -type d -exec chmod g+s {} \;

Setting the group of the directories to the Apache user and then setting the setgid bit will allow you to ensure that files created in that directory get the proper group permissions regardless of what user runs the shell script. This also allows you to exclude read/write permissions to "other" users.

Dentation answered 9/9, 2013 at 17:43 Comment(0)
Q
2

I think the reason of the problem is already explained, as the cron runs under root user and created files in tmp are not accessible by web user. The other solutions did not work for me and I did not want to set tmp permissions to 777, I ended up setting a cron job for the web user, in debian specifically it would be

crontab -u www-data -e

Taken from this answer How to specify in crontab by what user to run script?

Quarta answered 24/10, 2015 at 14:2 Comment(0)
S
0

If you're encountering the SplFileInfo error in CakePHP2 and you're absolutely certain that your file/directory permissions are set up properly, then one other thing to check is your PHP version. Cake2 requires PHP 5.2.8 or greater and although you'd usually be alerted on the default page if you were using the wrong version, you wouldn't be alerted if you'd developed your app on one server and then moved it to another.

I experienced this error after developing a Cake2 app on a PHP5.3 server and then moving it to a PHP 5.1 server. Upgrading to 5.2.17 (which is above 5.2.8) solved the problem.

Scottie answered 6/5, 2012 at 17:10 Comment(1)
It may have changed servers a few times but I believe the server it ran on always had the minimum PHP requirements (currently the server has 5.3.3). One thing to note though is that it only seems to happen when I run a version of the app on a virtual machine. I'm not sure if that indicates anything else?Squelch
I
0

Use this ..

cd cakephp/app/tmp/cache/persistent 

sudo chmod 666 myapp*

cd ..

cd models

sudo chmod 666 myapp*
Ileanaileane answered 28/8, 2013 at 8:21 Comment(1)
This seems like a pretty big security hole if you make your cache readable and writable by everyone.Dentation
S
0

You need to make the app/tmp directory writable by the webserver. Find out what user your webserver runs as (in my case _www) and change the ownership of the app/tmp directory to that user: $ chown -R _www app/tmp

Stemson answered 21/2, 2016 at 4:57 Comment(0)
P
0

Another solution. Permission conflicting occurred because multi users share same files. Thus, if we split cache directory into multi sub directories, no conflicting occur and no changing default permission of directories and files required.

As following, each sub cache directory is defined by type of php api handler:

define('CACHE', TMP . 'cache' . DS . php_sapi_name() . DS);
  • When browser the website, active user is apache. And the sub directory is cache/apache2handler.
  • When run a batch, active user is root or logging-in user. And the sub directory is cache/cli.

Other side, current user account can be used to name sub directory. Check at How to check what user php is running as?

Philipines answered 22/6, 2017 at 7:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.