186

I am new to Laravel. I was trying to open http://localhost/test/public/ and I got

Error in exception handler.

I googled around and changed the permission of storage directory using chmod -R 777 app/storage but to no avail.

I changed debug=>true in app.php and visited the page and got Error in exception handler:

The stream or file "/var/www/html/test/app/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied in /var/www/html/test/bootstrap/compiled.php:8423

Then I changed the permissions of storage directory using the command chmod -R 644 app/storage and the 'Error in exception handler' error was gone and a page is loaded. But in there I am getting this:

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

kenorb
  • 155,785
  • 88
  • 678
  • 743
vishnub1626
  • 2,063
  • 2
  • 14
  • 14
  • 2
    looks like permition issue again, chmod recursively all application directories – alou May 08 '14 at 11:14
  • @alou I think I have already done that with chmod -R 777 app/storage. Didn't I? And all the directories inside app has drwxrwxrwx permission. – vishnub1626 May 08 '14 at 11:32
  • 36
    Try: `php artisan cache:clear` then `chmod -R 777 app/storage` finally `php artisan dump-autoload` – vsmoraes May 08 '14 at 17:23
  • @vsmoraes It worked. It'll be really helpful if you can explain what the problem was. – vishnub1626 May 09 '14 at 03:55
  • @tav It's hard to know what was causing that, probably some sort of cache that was created with the error and remained there until you cleaned all the cache. Laravel also have some problems with fresh installs and requires to re-dump the autoload – vsmoraes May 09 '14 at 13:49
  • 9
    vsmoraes's comment was correct however instead of 'php artisan dump-autoload' in should be 'composer dump-autoload' – Elliot Robert Oct 09 '16 at 21:33
  • Please refer this link and make proper permission and group level. [enter link description here](https://stackoverflow.com/questions/30639174/file-permissions-for-laravel-5-and-others) – Harshavardhan Aug 23 '17 at 12:24
  • I managed to fix it as I was only granting permission to via this command: Copy Code sudo chmod -R 775 storage The fix was to add this: Copy Code sudo chmod -R ugo+rw storage – Yahya Ayyoub Aug 28 '22 at 05:55
  • Dont ever do as @vsmoraes suggested... this will basically open up your whole application, and is now a huge security risk... please remove that oddly stupid comment. – ii iml0sto1 Apr 08 '23 at 16:46

33 Answers33

346

Suggestion from vsmoraes worked for me:

Laravel >= 5.4

php artisan cache:clear 
chmod -R 775 storage/
composer dump-autoload

Laravel < 5.4

php artisan cache:clear 
chmod -R 775 app/storage 
composer dump-autoload
TylerH
  • 20,799
  • 66
  • 75
  • 101
ecairol
  • 6,233
  • 1
  • 27
  • 26
74

For those facing this problem with Laravel 5, this is a permission issue caused by different users trying to write at the same log file within the storage/logs folder with different permissions.

What happens is your Laravel config probably is setup to log errors daily and therefore your web server (Apache/nginx) might create this file under a default user depending on your environment it can be something like _www on OSX or www-data on *NIX systems, then the issue comes when you might have run some artisan commands and got some errors, so the artisan will write this file but with a different user because PHP on terminal is executed by a different user actually your login user, you can check it out by running this command:

php -i | grep USER

If your login user created that log file your web server you will not be able to write errors in it and vice-versa because Laravel writes log files with 655 permissions by default which only allows the owner to write in it.

To fix this temporary you have to manually give permissions for the group 664 to this file so both your login user and web server user can write to that log file.

To avoid this issue permanently you may want to setup a proper permissions when a new file is create within the storage/logs directory by inheriting the permissions from the directory this answer https://unix.stackexchange.com/a/115632 can help you to tackle with that.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Adriano Rosa
  • 8,303
  • 1
  • 25
  • 25
57

You should not give 777 permissions. It's a security risk. To Ubuntu users, in Laravel 5, I sugest to change owner for directory storage recursively:

Try the follow:

sudo chown -R www-data:www-data storage

In Ubuntu based systems, www-data is apache user.

RibeiroSt
  • 681
  • 5
  • 6
43

For everyone using Laravel 5, Homestead and Mac try this:

mkdir storage/framework/views
user2226755
  • 12,494
  • 5
  • 50
  • 73
Hans P
  • 431
  • 4
  • 3
38

some times SELINUX caused this problem; you can disable selinux with this command.

sudo setenforce 0
mahrad
  • 381
  • 3
  • 2
  • 5
    This is basically like turning off the entire firewall because it was blocking a port you needed open. – Teh JoE Dec 22 '16 at 15:31
  • I had never heard of this. "Security-Enhanced Linux (SELinux) is a Linux kernel security module that provides a mechanism for supporting access control security policies." I doubt it's a good idea to turn it off. And I bet the upvoters are blindly using this command without understanding the full consequences. – Ryan Jun 26 '17 at 18:32
31

NEVER GIVE IT PERMISSION 777!

go to the directory of the laravel project on your terminal and write:

sudo chown -R your-user:www-data /path/to/your/laravel/project/
sudo find /same/path/ -type f -exec chmod 664 {} \;
sudo find /same/path/ -type d -exec chmod 775 {} \;
sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache

This way you're making your user the owner and giving privileges:
1 Execute, 2 Write, 4 Read
1+2+4 = 7 means (rwx)
2+4 = 6 means (rw)
finally, for the storage access, ug+rwx means you're giving the user and group a 7

Ibrahim W.
  • 615
  • 8
  • 20
18

Problem solved

php artisan cache:clear
sudo chmod -R 777 vendor storage

this enables the write permission to app , framework, logs Hope this will Help

Manoj Rammoorthy
  • 1,384
  • 16
  • 16
  • 15
    never 777 ...in dev or prod as it will give the illusion of things working in dev but they will break in prod unless 777 as well which is never a good idea – Kyle Burkett Jan 09 '17 at 17:41
14

For vagrant users, the solution is:

(in vagrant) php artisan cache:clear

(outside of vagrant) chmod -R 777 app/storage

(in vagrant) composer dump-autoload

Making sure you chmod in your local environment and not inside vagrant is important here!

Brendan
  • 474
  • 4
  • 7
  • 7
    isn't 777 too open? – simo Jul 20 '16 at 07:59
  • 5
    I mean, for production, sure. But this is a local dev environment. 777 was what the original poster was using, and other answers.775 or 755 may work depending. – Brendan Jul 21 '16 at 13:41
  • I'm using laravel and artisan and figured out the key is to do the migrate on the VM, but all the file creating happens on the host (i always get guest v. host confused, but this would be the local develop directory). VM = migrate (for the db running there, make: on the mac. I left my permissions as is and run the commands in the correct place, no trouble. – kaplan Nov 14 '20 at 17:42
13

Try again with chmod -R 755 /var/www/html/test/app/storage. Use with sudo for Operation not permitted in chmod. Use Check owner permission if still having the error.

Khay
  • 1,530
  • 13
  • 28
11

As per Laravel 5.4 which is the latest as I am writing this, if you have any problem like this, you ned to change the permission. DO NOT LISTEN TO ANYONE WHO TELLS YOU TO SET 777 FOR ANY DIRECTORY. It has a security issue. Change the permission of storage folder like this

sudo chmod -R 775 storage

Change bootstrap folder permission like this

sudo chmod -R 775 bootstrap/cache

Now please make sure that you're executing both commands from your application directory. You won't face problems in future regarding permission. 775 doesn't compromise any security of your machine.

Koushik Das
  • 9,678
  • 3
  • 51
  • 50
7

Suggest the correct permission, if for Apache,

sudo chown -R apache:apache apppath/app/storage
Pang
  • 9,564
  • 146
  • 81
  • 122
Till
  • 1,097
  • 13
  • 13
  • Laravel Forge use: sudo chown -R forge:forge ~/project/storage/ sudo chown -R forge:forge ~/project/bootstrap/cache/ – Flappy Mar 11 '19 at 12:30
7

FOR ANYONE RUNNING AN OS WITH SELINUX: The correct way of allowing httpd to write to the laravel storage folder is:

sudo semanage fcontext -a -t httpd_sys_rw_content_t '/path/to/www/storage(/.*)?'

Then to apply the changes immediately:

sudo restorecon -F -r '/path/to/www/storage'

SELinux can be a pain to deal with, but if it's present then I'd STRONGLY ADVISE you learn it rather than bypassing it entirely.

Heather Gaye
  • 1,118
  • 1
  • 11
  • 19
  • my exact problem in fresh centos 7 was similar. it was saying no permission to write but all were 777 for testing. So this post actually saved my time after all general check. – HumaN Dec 31 '16 at 10:50
  • 1
    This is the correct solution, although I think the correct SELinux type should be httpd_sys_rw_content_t `sudo semanage fcontext -a -t httpd_sys_rw_content_t '/path/to/www/storage(/.*)?'` – imabug Jul 03 '18 at 14:07
5

If you have Laravel 5 and looking permanent solution , applicable both php artisan command line usage and Apache server use this:

sudo chmod -R 777 vendor storage
echo "umask 000" | sudo tee -a /etc/resolv.conf
sudo service apache2 restart

See detailed explanation here.

Community
  • 1
  • 1
alexeydemin
  • 2,672
  • 3
  • 27
  • 26
  • 9
    it seems like a bad idea to use 777 – Randy L Dec 15 '15 at 16:14
  • umask 000 in resolv.conf ?! where are this people getting this info? that is a invalid line in resolv.conf. Please ignore this and all 777 "solutions" out there – higuita Dec 18 '17 at 11:04
  • check the url and find no umask option in resolv.conf https://linux.die.net/man/5/resolv.conf – higuita Dec 29 '17 at 13:37
5

I had the same issue and the below steps helped me fix the issue.

  1. Find out the apache user - created a test.php file in the public folder with the code

<?php echo exec('whoami'); ?>

And run the file from the web browser. It would give the apache user. In my case, it is ec2-user as I was using the aws with cronjob installed in /etc/cron.d/. It could be different user for others.

  1. Run the below command on the command line.

sudo chown -R ec2-user:<usergroup> /app-path/public

You need to identify and use the right "user" and "usergroup" here.

KiranD
  • 433
  • 2
  • 7
  • 20
5

I had the same problem but in the views directory:

file_put_contents(/var/www/app/storage/framework/views/237ecf97ac8c3cea6973b0b09f1ad97256b9079c.php): failed to open stream: Permission denied

And I solved it cleaning the views cache directory with the following artisan command:

php artisan view:clear
pableiros
  • 14,932
  • 12
  • 99
  • 105
3

Xampp for use:

cd /Applications/XAMPP/htdocs  
chmod -R 775 test/app/storage

From Setting Up Laravel 4.x on Mac OSX 10.8+ with XAMPP

TylerH
  • 20,799
  • 66
  • 75
  • 101
cristianojeda
  • 339
  • 2
  • 12
2
rm storage/logs/laravel.log  

solved this for me

aad1992
  • 29
  • 2
2

If using laradock, try chown -R laradock:www-data ./storage in your workspace container

Rob Lao
  • 1,526
  • 4
  • 14
  • 38
2

Setting permission to 777 is definitely terrible idea!

... but

If you are getting permission error connected with "storage" folder that's what worked for me:

1) Set "storage" and its subfolders permission to 777 with

sudo chmod -R 777 storage/

2) In browser go to laravel home page laravel/public/ (laravel will create necessary initial storage files)

3) Return safe 775 permission to storage and its subfolders

sudo chmod -R 775 storage/
Nika Tsogiaidze
  • 949
  • 14
  • 18
1

Any time I change app.php I get a permission denied writing bootstrap/cache/services.json so I did this to fix it:

chmod -R 777 bootstrap/cache/
malhal
  • 26,330
  • 7
  • 115
  • 133
1

In my case solution was to change permission to app/storage/framework/views and app/storage/logs directories.

Den
  • 1,456
  • 16
  • 17
1

After a lot of trial and error with directory permissions I ended up with an epiphany...there was no space left on the disk's partition. Just wanted to share to make sure nobody else is stupid enough to keep looking for the solution in the wrong direction.

In Linux you can use df -h to check your disk size and free space.

Frank
  • 530
  • 5
  • 15
1

This issue actually caused by different users who wants to write/read file but denied cause different ownership. maybe you as 'root' installed laravel before then you login into your site as 'laravel' user where 'laravel' the default ownership, so this is the actually real issue here. So when user 'laravel' want to read/write all file in disk as default, to be denied, cause that file has ownership by 'root'.

To solving this problem you can follow like this:

sudo chown -hR your-user-name /root /nameforlder

or in my case

sudo chown -hR igmcoid /root /sublaravel

Footnote:

  1. root as name first ownership who installed before
  2. your-user-name as the default ownership who actually write/read in site.
  3. namefolder as name folder that want you change the ownership.
  • Your answer actually made it clear why not to use 777 permission as well as how to define a new user for a perticular instance. I run my server on ubunto and by default the apache server runs on www-data you can easily find that using command 'ps aux | egrep '(apache|httpd)' and then follow above instructions – Lonare Jul 17 '21 at 18:32
1

If you use Linux or Mac, even you can also run in ssh terminal. You can use terminal for run this command,

 php artisan cache:clear 
 sudo chmod -R 777 storage
 composer dump-autoload

If you are using windows, you can run using git bash.

 php artisan cache:clear 
 chmod -R 777 storage
 composer dump-autoload

You can download git form https://git-scm.com/downloads.

  • 2
    DO NOT LISTEN TO ANYONE WHO TELLS YOU TO SET 777 FOR ANY DIRECTORY – mahen3d May 26 '21 at 06:35
  • 3
    Rashed, are you aware that people can lose millions of $ from your solution? your solution is a severe security breach, you are allowing guest user(MOST PROBABLY A HACKER) of the operating system to read/write/execute the storage of your laravel project!!! – Ibrahim W. Jun 18 '21 at 06:23
0

If anyone else runs into a similar issue with fopen file permissions error, but is wise enough not to blindly chmod 777 here is my suggestion.

Check the command you are using for permissions that apache needs:

fopen('filepath/filename.pdf', 'r');

The 'r' means open for read only, and if you aren't editing the file, this is what you should have it set as. This means apache/www-data needs at least read permission on that file, which if the file is created through laravel it will have read permission already.

If for any reason you have to write to the file:

fopen('filepath/filename.pdf', 'r+');

Then make sure apache also has permissions to write to the file.

http://php.net/manual/en/function.fopen.php

Kyle Burkett
  • 1,375
  • 12
  • 28
0

Just start your server using artisian

php artisian serve

Then access your project from the specified URL:

enter image description here

Wajih
  • 4,227
  • 2
  • 25
  • 40
0

I have the same issue when running vagrant on mac. solved the problem by changing the user of Apache server in https.conf file:

# check user for php
[vagrant] ubuntu ~ $ php -i | grep USER
USER => ubuntu
$_SERVER['USER'] => ubuntu
[vagrant] ubuntu ~ $ 

Run apache under php user instead of user daemon to resolve file access issue with php

# change default apache user from daemon to php user
sudo sed -i 's/User daemon/User ubuntu/g' /opt/lampp/etc/httpd.conf
sudo sed -i 's/Group daemon/Group ubuntu/g' /opt/lampp/etc/httpd.conf

now, php created cache file can be read and edit by apache without showing any access permission error.

nigam214
  • 301
  • 2
  • 7
0

I got same errors in my project...
But found out that I forgot to put enctype in my form.

<form method="#" action="#" enctype="multipart/form-data">

Hopes it helps somewhere somehow...

Skandix
  • 1,916
  • 6
  • 27
  • 36
Vpa
  • 703
  • 2
  • 9
  • 30
0

While working on Windows 10 with Laragon and Laravel 4, it seemed to me there was no way to change the permissions manually, since executing chmod-commands in the Laragon-in-built-terminal had no effect.

However, it was possible in this terminal to go to the storage folder and manually add the desired folders like this:

cd app/storage
mkdir cache
mkdir meta
mkdir views
mkdir sessions

The cd-command in the terminal brings you to the folder (you might need to adjust this path to suit your file structure). The mkdir-command will create the directory with the given name.

I did not have the opportunity to test this approach in Laravel 5, but I expect that a similar approach should work.

Of course there might be a better way, but at least this was a reasonable workaround for my situation (fixing the error: file_put_contents(/var/www/html/laravel/app/storage/meta/services.json): failed to open stream).

Virginia
  • 717
  • 2
  • 6
  • 15
0
  1. First, delete the storage folder then again create the storage folder.
  2. Inside storage folder create a new folder name as framework.
  3. Inside framework folder create three folders name as cache, sessions and views.

I have solved my problem by doing this.

Md. Juyel Rana
  • 540
  • 5
  • 10
  • When Laravel generates those files that you want to delete (storage, storage/framework, storage/framework/ cache|sessions|views) it secures them making only the administrate Operating system user able to read/write/execute them, so when you delete the files and recreate them they can be altered by guest user which is a securing breach, your answer is a security breach!! – Ibrahim W. Jun 18 '21 at 06:21
0

None of the above solution can be useful for me, because I didn't have access by SSH to run the commands to clear cache or giving the recursive permission, so I fixed the issue by removing this file and the issue fixed.

You can delete the bootstrap/cache/config.php file.

Nasser Ali Karimi
  • 4,462
  • 6
  • 34
  • 77
0

Try this. Change user:group to the user:group owner of your http server:

cd storage && sudo chmod 2775 . && sudo chown -R user:group . && sudo chmod -R og-r . && sudo find . -type d -exec chmod g=rwxs '{}' \; && sudo find . -type f -exec chmod g=rw '{}' \; && sudo setfacl -d -m g::rwx . && sudo setfacl -d -m o::rx .

This will make all new files and folders adopt these settings.

Max S.
  • 1,383
  • 14
  • 25
-5

I have tried to give the 777 access to storage folder and it have work for me

1) go to your laravel root directory , (/var/www/html for me) and run the following command

chmod 777 -R storage
Yur Gasparyan
  • 664
  • 5
  • 20
  • 2
    Do not set permissions to 777 as this makes the dir visible and editable for everyone who can see the dir. This is not recommended! – CodeNinja Sep 04 '17 at 09:40