24

I was playing around with a working Laravel 4 installation and moved everything into a sub folder. Then I decided to not do that and I moved it all back (all via the command line):

mv * /folder

and then

cd folder
mv * ../

Now, the site is throwing the following error:

file_put_contents(/var/www/quantquote/app/storage/meta/services.json): failed to open stream: Permission denied

Here is a screenshot of the full error:

https://i.stack.imgur.com/J8uTM.png

I've already tried setting permissions on the /storage folder to 777 to no avail.

Rubens Mariuzzo
  • 28,358
  • 27
  • 121
  • 148
JasonStockman
  • 532
  • 2
  • 4
  • 9

8 Answers8

29

Spent a whole day for solving this and this command simply solved my problem.

If your permissions are 777 for Laravel App folder and you still get this error, it's because SEliux has blocked it. You can easily unblock your application's folder using this command:

su -c "chcon -R -h -t httpd_sys_script_rw_t /usr/share/nginx/YOUR_LARAVEL_APP/"

That's it!

BTW never disable SElinux: http://stopdisablingselinux.com/

Community
  • 1
  • 1
Sky
  • 4,244
  • 7
  • 54
  • 83
  • 1
    thanks so much! I'd like to add that my command ended up being `chcon -R -h -t httpd_sys_script_rw_t /var/www/laravel` since I am on a local machine running as root already, in case anyone else is vaguely sleep deprived and not reading things correctly. Again, thanks! – maggiekh May 18 '15 at 00:18
  • 1
    This is the answer to so many questions with Laravel and logging. Thanks for that. Saved another day of debugging – prog_24 Feb 01 '16 at 13:52
23

As I stated in my comment:

find app/storage -type d -exec chmod 777 {} \;

find app/storage -type f -exec chmod 777 {} \;

This will chmod all directories and files in app/storage to 777.

Collin Henderson
  • 1,154
  • 10
  • 22
  • 5
    I would set the permissions to 775 instead of 777 and configure the file ownerships and groups accordingly. i.e. the group of the web server and the app/storage folder will be the same. – eaykin May 22 '14 at 10:52
  • 4
    As a security precaution, I'd recommend `chmod -R ug+rwX,o-rwx .` This will set directories only to execute, and prevent people outside of the group doing horrible things to your files. – DanielM Dec 01 '14 at 17:31
  • 1
    To read how to set your directory permissions in the right way check this answer: http://stackoverflow.com/a/37266353/987864 – Daan Oct 13 '16 at 08:30
11

As the problem is related to permissions try re-giving the right permissions to app/storage and all its sub-directories and file.

You can do so from the root path of your Laravel app typing:

chmod -Rvc 777 app/storage

If for some reason this doesn't work, try:

find app/storage -type d -exec chmod -vc 777 {} \;
find app/storage -type f -exec chmod -vc 777 {} \;
Rubens Mariuzzo
  • 28,358
  • 27
  • 121
  • 148
8

When chmod 777 or chmod 775 fails check that subdirectories really exists:

app/storage/
├── cache
├── logs
├── meta
├── sessions
└── views

in case these folders does not exists you must to create it:

cd app/storage/
mkdir meta logs cache sessions views
cd ../
find storage -type d -exec chmod 775 {} \;
find storage -type f -exec chmod 664 {} \;

UPDATE 2016 for laravel 5+

In a default Laravel 5+ application structure, storage is at the same level than app. So the previous command becomes:

cd storage/
mkdir meta logs cache sessions views
find . -type d -exec chmod 775 {} \;
find . -type f -exec chmod 664 {} \;

https://laravel.com/docs/5.0/structure

Igor Parra
  • 10,214
  • 10
  • 69
  • 101
  • 1
    This answer helped me, I needed to create the folder structure under the storage folder. But in my case (for Laravel 5.0) the views, sessions and cache folders had to be in a subfolder named "framework". – BruceHill Jan 21 '16 at 21:44
7

If you're using CentOS7, follows these steps:

  1. Change app/storage directory ownership

    chown apache:apache app/storage
    
  2. Change app/storage permissions

    chmod -R 775 app/storage
    
  3. Prevent SELinux from blocking the app/storage directory

    su -c "chcon -R -h -t httpd_sys_script_rw_t [fullpath]/app/storage"
    
6

just type this on terminal:

php artisan cache:clear
Ori Lentz
  • 3,668
  • 6
  • 22
  • 28
AmirAgha
  • 135
  • 2
  • 6
5

Never run a 777, in recursive mode. It's a security issue. For now, remove the services.json file, and try this :

Laravel 4: Failed to open stream: Permission denied

Community
  • 1
  • 1
François
  • 706
  • 1
  • 7
  • 9
  • 3
    Please post the relevant parts from the link, if the link dies, then the answer will not be helpful in the future. – Popo Mar 05 '14 at 21:18
0

Try these commands one by one:

setenforce 0
setsebool -P httpd_can_network_connect_db on
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
  • Why there is no up vote for this one? This works like a charm! :D thanks! – Ram Guiao Jul 31 '17 at 13:47
  • Because it is disabling SELinux, instead of learning how to use it, which is dangerous advice. Additionally, the poster hasn't actually explained what this 'solution' is doing. – charliefortune Nov 21 '19 at 12:29