How to fix Error: laravel.log could not be opened?
Asked Answered
K

32

345

I'm pretty new at laravel, in fact and I'm trying to create my very first project. for some reason I keep getting this error (I haven't even started coding yet)

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

I've read this has something to do with permissions but chmod -R 775 storage didn't help at all.

Permissions

Korten answered 1/5, 2014 at 16:2 Comment(6)
Are you sure you're in the right directory when running that chmod command? Try chmod -R 755 /var/www/laravel/app/storage. Also, what are the user and group set to? Try ls -al /var/www/laravel/app/storageBrasilein
same results (I updated my original post to show the permissions of the folder)Korten
:/ for some reason it still doesn't work. I didn't mention it before, but I'M using vagrant. so my www folder is on a VM (apache, php and everything else is running on it). I'm not sure if that has something to do with anything but I thought I'd mention it just in case. (I'm creating my project withing the vm, using composer)Korten
You need to change the group to be the user who your web server runs as (www usually) rather than yourself, if you want to keep the permissions as 755. "chgrp -R www /var/www/laravel/app/storage"Brasilein
Those guys who suggest 777, try google this phrase: "production db_password filetype:env inurl:com"Titillate
Disabling SELINUX worked for me.Bimetallic
K
89

To fix this issue, you need to change the ownership of the directory to the unix user that the webserver uses.

  1. Get out of the VM
  2. Using the console, go to your synced folder (vagrant)
  3. sudo chown -R $USER:www-data storage
  4. chmod -R 775 storage

Even though I created the project within the VM using the VM user, the folder belonged to the user in the real computer; so, when trying to

Now it's working.

Thanks to all those that helped me figure this thing out

EDIT:

Actually, it still wasn't working, it still gave me a "permission denied" problem.

Here's what I did, I modified my Vagrantfile like this:

config.vm.synced_folder "./app","/var/www/", create:true,
:owner => "vagrant",
:group => "www-data",
:mount_options => ["dmode=775","fmode=664"]
Korten answered 1/5, 2014 at 19:30 Comment(9)
I did that... and I AM gROOT! Still not working... laravel 5... renamed the log file, laravel created new one... same errorGynecology
I don't have config.vm.synced_folder because I'm using homestead, how can I fix this issue?Repairman
You shouldn't be using 777 all willy nilly.Harneen
I don't see a problem on using 777 on a folder I know exactly what's inside (a dummy sample project)Korten
What else should we use?Newmann
@RameshPareek See Antonio Carlos Ribeiro his answer. 775 or 755Uella
This should absolutely not be the accepted answer. It's terrible. Never set 777. Ever.Ebullient
Permission 777 should not be given ever.Trod
Prove this is the problem by turning off selinux with the command setenforce 0 This should allow writing, but you've turned off added security server-wide. That's bad. Turn SELinux back setenforce 1 Then finally use SELinux to allow writing of the file by using this command chcon -R -t httpd_sys_rw_content_t storage And you're off!Deadlight
P
655

Never set a directory to 777. you should change directory ownership. so set your current user that you are logged in with as owner and the webserver user (www-data, apache, ...) as the group. You can try this:

sudo chown -R $USER:www-data storage
sudo chown -R $USER:www-data bootstrap/cache

then to set directory permission try this:

chmod -R 775 storage
chmod -R 775 bootstrap/cache

Update:

Webserver user and group depend on your webserver and your OS. to figure out what's your web server user and group use the following commands. for nginx use:

ps aux|grep nginx|grep -v grep

for apache use:

ps aux | egrep '(apache|httpd)'

Papaya answered 14/8, 2017 at 11:24 Comment(18)
thanks! In short you can also do : sudo chown -R {your current user}:www-data storage bootstrap/cacheNewmann
@RameshPareek You're right but I just wanted to be more clear ;)Papaya
This doesn't work for me. I just get chown: www-data: illegal group name. Setting the directory to 777 is the only thing that worksAntoinetteanton
@MattD I have updated the answer. please check again and warn me if you need more help. that would make it easier if I know your OS.Papaya
@HamidParchami I'm on Mac. The nginx one returns nothing and the apache one returns like 10 things. One has my username, one says root and the rest say daemon. Is daemon the correct webserver user?Antoinetteanton
@MattD I guess the apache group on mac is _www try this sudo chgrp -R _www bootstrap/cache . it would be helpful to take a look at this post: https://mcmap.net/q/88381/-correct-owner-group-permissions-for-apache-2-site-files-folders-under-mac-os-xPapaya
@HamidParchami It ran without errors, but it still doesn't workAntoinetteanton
@HamidParchami I figured it out! I set the user and group to daemon. I guess that's the new _wwwAntoinetteanton
For anyone with this problem in the future, only set the group to daemon, not the user. Otherwise you wont be able to write to that directory.Antoinetteanton
This should be the chosen answer, 100% agree that sysadmins should not just allow access to everything to avoid working out the real issue.Profess
I agree with Blake 100%!! Never set permissions to 777. And thanks for showing who owns the processes running.Perfective
Better explanation. Indeed should never set permissions to 777Dismast
Worked perfectly for me on Ubuntu 18.04/Laravel 6Horribly
How do I do this rightly from within a deployment script without having to use sudo in the deployment script?Landsman
This should be the accepted answer, it works perfect with 775 permissionAlto
Laradock users: if you still have errors, inside the workspace container, try with # chown -R $USER:laradock storage bootstrap/cache and then chmod -R 775 storage bootstrap/cacheAnguiano
@HamidParchami This works, but everytime I restart my pc I need to provide the permission again.Amritsar
for centos/redhat/amzn linux user user apache instead of www-dataMacedonia
C
192

Never use 777 for directories on your live server, but on your own machine, sometimes we need to do more than 775, because

chmod -R 775 storage

Means

7 - Owner can write
7 - Group can write
5 - Others cannot write!

If your webserver is not running as Vagrant, it will not be able to write to it, so you have 2 options:

chmod -R 777 storage

or change the group to your webserver user, supposing it's www-data:

chown -R vagrant:www-data storage
Customable answered 1/5, 2014 at 17:0 Comment(4)
Didn't need the chmod in my case as it was set okay already, However on Fedora 20 it needed: chown -R apache:apache laravelprojectRoxieroxine
Was having problems mounting folders via samba/vagrant - chown -R vagrant:www-data storage did it for me, thanks.Pragmatist
chmod -R 777 storage worked for me. I tried changing the group, but I kept getting www-data: illegal group nameAntoinetteanton
@antonio I need to provide the permission everytime I restart the computer. Any suggestion how could I fix it?Amritsar
K
89

To fix this issue, you need to change the ownership of the directory to the unix user that the webserver uses.

  1. Get out of the VM
  2. Using the console, go to your synced folder (vagrant)
  3. sudo chown -R $USER:www-data storage
  4. chmod -R 775 storage

Even though I created the project within the VM using the VM user, the folder belonged to the user in the real computer; so, when trying to

Now it's working.

Thanks to all those that helped me figure this thing out

EDIT:

Actually, it still wasn't working, it still gave me a "permission denied" problem.

Here's what I did, I modified my Vagrantfile like this:

config.vm.synced_folder "./app","/var/www/", create:true,
:owner => "vagrant",
:group => "www-data",
:mount_options => ["dmode=775","fmode=664"]
Korten answered 1/5, 2014 at 19:30 Comment(9)
I did that... and I AM gROOT! Still not working... laravel 5... renamed the log file, laravel created new one... same errorGynecology
I don't have config.vm.synced_folder because I'm using homestead, how can I fix this issue?Repairman
You shouldn't be using 777 all willy nilly.Harneen
I don't see a problem on using 777 on a folder I know exactly what's inside (a dummy sample project)Korten
What else should we use?Newmann
@RameshPareek See Antonio Carlos Ribeiro his answer. 775 or 755Uella
This should absolutely not be the accepted answer. It's terrible. Never set 777. Ever.Ebullient
Permission 777 should not be given ever.Trod
Prove this is the problem by turning off selinux with the command setenforce 0 This should allow writing, but you've turned off added security server-wide. That's bad. Turn SELinux back setenforce 1 Then finally use SELinux to allow writing of the file by using this command chcon -R -t httpd_sys_rw_content_t storage And you're off!Deadlight
O
83

It also may be SELinux. (Centos, RedHat)

Determine status of SElinux on terminal:

$ sestatus

If status is enabled, write command to disable SElinux

$ setenforce Permissive

Or you may execute this command

$ sudo setenforce 0

Outdoor answered 16/7, 2018 at 8:44 Comment(9)
This is the only thing that worked, can you please explain what does it do?Exurb
@hack4mer you can read more information about seLinux. en.wikipedia.org/wiki/Security-Enhanced_LinuxUam
the weird things I've ever seen, why the hell it works after searching for more than 6 hoursMonteux
The only solution that worked for me.. thank you. But the problem comes back every time the server restarts, do yo know how to make it apply even if the server is restarted?Selfconfessed
@JuanAngel you have to Permanently disable service. open with editor vim /etc/sysconfig/selinux and then change the directive SELinux=enforcing to SELinux=disabledUam
This worked for me to. I don't like disabling a security feature so I'll have research why this is necessary. I used the following command to save as disabled: sed -i 's/enforcing/disabled/' /etc/sysconfig/selinuxBilyeu
@NickW you are right, that is a short way to fix the problemUam
OMG!!!! thanks a bilion. I was getting mad and I totally forgot SElinuxSatanism
Rather than change the mode to Permissive, which is discouraged, try changing the SELinux context, by chcon -Rv --type=httpd_sys_rw_content_t /path/to/your/write.filesChitterlings
A
48

You need to adjust the permissions of storage and bootstrap/cache.

  • cd into your Laravel project.
  • sudo chmod -R 755 storage
  • sudo chmod -R 755 bootstrap/cache

You can try 777 if 755 doesn't work. 777 is not secure though!

Depending on how your web server is setup, you may be able to be more specific with your permissions, and only grant them to your web server user. Google WEB SERVER NAME Laravel file permissions for more information.

At the time of writing, this is for Laravel 5.4

Anomalism answered 20/6, 2017 at 8:21 Comment(3)
Whenever something works with 777 and not with 755 nor 775, it means your server is not using the corresponding user, eg.: nginx, apache, httpd, www-data, etc ...Anguiano
@daniel This code works but everytime after restarting the laptop, I need to provide the permission again. What should I do :(Amritsar
Used ..755 commands. Not worked. Used 777 - worked. Then set back to 755 and it continued to work. Then I've checked API. Was not working, and I did 777->755 and API started to work. Anybody knows why?Polly
E
37

It might be late but may help someone, changing directory permissions worked for me.

Assuming that your Laravel project is in /var/www/html/ directory. Goto this directory.

cd /var/www/html/

Then change permissions of storage/ and bootstrap/cache/ directories.

sudo chmod -R 775 storage/
sudo chmod -R 775 bootstrap/cache/

If permission 775 does not work, try setting 777. (Warning! This is the most relaxed permission, use with care).

sudo chmod -R 777 storage/
sudo chmod -R 777 bootstrap/cache/

cPanel: If you are on cPanel and don't have terminal available you can change permission by right clicking on the mentioned directory and it's sub-directories.

Endurance answered 11/5, 2020 at 9:30 Comment(1)
Giving 777 permission to the storage directory means you're giving the attackers a key to blow up the whole system. DON'T DO THIS.Motivity
A
21

Add to composer.json

"scripts": {
    "post-install-cmd": [
          "chgrp -R www-data storage bootstrap/cache",
          "chmod -R ug+rwx storage bootstrap/cache"
     ]
}

After composer install

Aftonag answered 9/10, 2018 at 10:6 Comment(0)
M
18

Run following commands and you can add sudo at starting of command depends on your system:

chmod -R 775 storage/framework
chmod -R 775 storage/logs
chmod -R 775 bootstrap/cache 
Maribelmaribelle answered 24/4, 2018 at 6:14 Comment(1)
Never use 777. With 777 permissions, you are giving anyone with a connection full access to the files or directories with those permissions. They may alter them in any way they choose, including maliciously. Many account hacking incidents stem from 777 permissions.Fess
U
15

1- ‍The nginx user and php-fpm user and app owner-user must be the same:

run command sudo vi /etc/nginx/nginx.conf change like bellow:

user nginx nginx;

run command sudo vi /etc/php-fpm.d/www.conf change like bellow:

listen.owner = nginx
listen.group = nginx
listen.mode = 0660
user = nginx
group = nginx

then restart nginx and php-fpm service

run below command

sudo chown nginx:nginx -R "your_project_path"

2- change file SELinux security context by run the following commands in the project path

chcon -R -t httpd_sys_content_t .
chcon -R -t httpd_sys_rw_content_t .
Umbrageous answered 13/1, 2021 at 5:56 Comment(1)
Very important answer in case of SELinux is enabled. Despite few print mistakes in commands, this answer saved my hours!Aeronaut
D
13

For all Centos 7 users on a Laravel context, there is no need to disable Selinux, just run these commands:

yum install policycoreutils-python -y # might not be necessary, try the below first

semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html/laravel/storage(/.*)?" # add a new httpd read write content to sellinux for the specific folder, -m for modify
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html/laravel/bootstrap/cache(/.*)?" # same as the above for b/cache

restorecon -Rv /var/www/html/ # this command is very important to, it's like a restart to apply the new rules

Lastly, make sure your hosts, ips and virtual hosts are all correctly for remote accessing.

Selinux is intended to restrict access even to root users, so only the necessary stuff might be accessed, at least on a generalist overview, it's extra security, disabling it is not a good practise, there are many links to learn Selinux, but for this case it is not even required.

Doorn answered 13/8, 2018 at 16:14 Comment(0)
L
13

If you use cmd

sudo chown -R $USER:www-data storage
sudo chown -R $USER:www-data bootstrap/cache

If you use GUI

First go to the project and right click on the storage and check the properties and go to the Permissions tab

enter image description here

Change the permissions using below code

sudo chmod -R 777 storage

Then your file properties may be

enter image description here

Then check your settings and execute laravel command it will work :)

Lubumbashi answered 4/2, 2019 at 8:27 Comment(0)
K
10

I stuck on this issue tried different commands but these will help to solve the problem

php artisan route:clear
php artisan config:clear
php artisan cache:clear

Hope it's helped others too.

Kohn answered 10/8, 2020 at 6:14 Comment(1)
You can also try php artisan optimize:clear it will run all the clear commandsFenugreek
T
9

In Laravel, you should set ACL on storage and cache directory so that web server user can read/write on the directory. Open a new terminal and run following:

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 bootstrap/cache storage/
sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:$(whoami):rwX bootstrap/cache storage/

References:

https://symfony.com/doc/3.4/setup/file_permissions.html#using-acl-on-a-system-that-supports-setfacl-linux-bsd

https://linux.die.net/man/1/setfacl

Trod answered 4/7, 2019 at 5:17 Comment(1)
Brilliant. Thank you. This one worked for me. nginx was running as user nginx but the command above returned apache, which solved it! It's horrifying how many people think blindly setting 777 permissions is okayClew
F
9

Just run the following command from Project root Directory -

sudo chmod -R 775 storage
sudo chown -R $USER:www-data storage
Fault answered 15/7, 2021 at 6:59 Comment(2)
Nope, this does not work on apacheAnticathode
@FernandoTorres Bro, It's the Permission Problem. I am still use Apache and solve by the commands. Make sure that you are the Project root path.Fault
C
6

Maximum people's are suggesting to change file permission 777 or 775, which I believe not an appropriate approach to solve this problem. You just need to change the ownership of storage and bootstrap folder.

In below Image you can see all my files/folder are under the root user(except storage and bootstrap, because I changed the ownership ),but I logged in as a administrator(before changing ownership) that's why it always giving permission denied. So I need to change the ownership of this two folder to administrator

So how I did this, go to your project directory and run below commands. sudo chown -R yourusername:www-data storage, sudo chmod -R ug+w storage, sudo chown -R yourusername:www-data bootstrap, sudo chmod -R ug+w bootstrap

Crusade answered 19/10, 2020 at 4:52 Comment(0)
A
5

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
Ansel answered 27/8, 2022 at 5:43 Comment(0)
B
4

try this

  1. cd /var/www/html
  2. setenforce 0
  3. service httpd restart
Bunt answered 20/4, 2018 at 19:21 Comment(3)
Could you explain what this does?Amend
No need to change directories to use setenforce but in any case it is wrong to completely disable SELinux just to fix one permission issue.Precis
i always forget this command, u save one more time. <3Buckley
A
3

This is what I do if I'm running Apache:

sudo chown -R $USER:www-data my_laravel_project/

sudo chmod -R 775 my_laravel_project/storage

sudo chmod -R 775 my_laravel_project/bootstrap/cache

cd my_laravel_project

php artisan optimize:clear
Ammoniacal answered 2/11, 2021 at 11:44 Comment(2)
is this working?Cityscape
i have tested it today, it's working! @JenuelGanawedCarson
C
2

This solution is specific for laravel 5.5

You have to change permissions to a few folders: chmod -R -777 storage/logs chmod -R -777 storage/framework for the above folders 775 or 765 did not work for my project

chmod -R 775 bootstrap/cache 

Also the ownership of the project folder should be as follows (current user):(web server user)

Carnarvon answered 20/10, 2017 at 6:58 Comment(0)
L
1

I wasn't too keen on changing my folder permissions to 777. Here's how I went about fixing this issue.

First, I changed the user who is running the web server on my local machine(I run nginx, but the principles apply everywhere):

$> sudo vim /etc/nginx/nginx.conf
user <my_user> #inside nginx.conf
service nginx reload

Afterwards, I created another index.php file under the public/ folder to find out who was running my php-fpm version and where I would go about changing that:

<?php
phpinfo();
?>

Reloading the page, I found out that www-data was the user(under the environment section). I also found out I was running php 7.1. I proceeded to change the user:

$> sudo vim /etc/php/7.0/fpm/pool.d/www.conf 
#Look for www-data or the following variables: user, group, listen.user, listen.group.

Finally, I gave the following permissions to folders:

sudo chmod -R 775 ./storage/

Now, I made sure that I was the owner of the folders by using a simple:

ls -al

If you set the server and php-fpm users to yourself and the folders are owned by root for example, then you will keep encountering this issue. This can happen if you did a sudo laravel new <project> as root. In that case, make sure you use a recursive chown command on your project to change the user:group settings. In most default cases, www-data is the main setting for the server and php, in that case it's a matter of making sure the folder isn't out of www-data's reach.

My project is setup in my home directory. On Ubuntu 16.04 and Laravel 5.5.

Lajuanalake answered 4/12, 2017 at 16:33 Comment(0)
S
1

In Linux

sudo chown -R www-data:root /var/www/name-project-Laravel
sudo chmod 755 /var/www/name-project-Laravel/storage
Steamy answered 27/7, 2022 at 12:45 Comment(0)
M
0

In my particular case I had a config file generated and cached into the bootstrap/cache/ directory so my steps where:

  1. Remove all generated cached files: rm bootstrap/cache/*.php
  2. Create a new laravel.log file and apply the update of the permissions on the file using:

    • chmod -R 775 storage
Merell answered 14/2, 2019 at 3:24 Comment(0)
K
0

Tried anything suggested here without success.

What worked for me was:

sudo chmod -R ugo+rw storage
sudo chmod -R ugo+rw storage/logs
Kikelia answered 10/3, 2022 at 19:11 Comment(0)
E
0

Mac OS solution

I fixed this problem simply by giving the necessary permission to my folders.

  • Right Click on the logs folder and click on Get Info
  • At the bottom, you will see Sharing & Permissions. Now give Read & Write access to the folder.

Follow the attachment.

enter image description here

Next step👇🏻

enter image description here

Esdraelon answered 20/12, 2022 at 8:3 Comment(0)
G
0

This worked for me:

RootFolder/storage/logs/

then just delete the laravel.log

Grudge answered 28/7, 2023 at 18:37 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Johnette
A
0

please check which user tries to write or open your laravel.log like www-data default user to write laravel.log if you use nginx.

set permission for storage folder for this user

sudo chown -R $USER:www-data storage
chmod -R 775 storage
Alceste answered 24/10, 2023 at 17:16 Comment(0)
J
0

This error in Homestead happens when PHP version it is not correct for the project in my case I just added the appropriate version to the Homestead.yaml file:

- map: web-api.test
    to: /home/vagrant/code/web-api/public
    php: "8.1"
Jessen answered 1/2 at 3:43 Comment(0)
D
-1

as you probably already know this issue is caused due to absence of write permission on the log folder which is a sub folder of storage.

To solve this problem walkedthrough these sequence of steps

  1. Update composer
 sudo composer self-update
  1. Change storage folder write permission
 sudo chmod -R ugo+rw storage

Now storage folder should have permission drwxrwxrwx

To check permissions run the following command from project root

ls -l 

Also if you face the following error after the step above

 ErrorException chdir(): No such file or directory (errno 2)

Just create a folder named public on the project root folder using

sudo mkdir public

ps. For more information about the chmod commands check this

Dunn answered 10/4, 2022 at 8:31 Comment(1)
777 permissions lol. Definitely not the way to go.Zeeland
E
-1

below command would work for sure.

sudo chmod -R ugo+rw storage
Elman answered 29/6, 2022 at 9:18 Comment(0)
J
-1

On systems that may have SELinux enabled, such as Centos or RedHat:

Determine status of SElinux on terminal:

sestatus

If enabled, rather than change the mode to Permissive, which is discouraged, try changing the SELinux context, by:

chcon -Rv --type=httpd_sys_rw_content_t /path/to/your/write.files – 
Jesher answered 6/3 at 9:9 Comment(0)
C
-2

In Centos & Rockylinnux

chown root:nginx FOLDER_PROJECT -Rf
chmod 775 FOLDER_PROJECT -Rf
cd FOLDER_PROJECT
chmod 777 storage -Rf

setenforce 0

please rate :)

Caviness answered 16/8, 2022 at 10:39 Comment(0)
T
-3

Not write any command or not gives any permission simplest way to solved this issue

just restart your system and try it again

it's work for me

Tocology answered 15/11, 2021 at 10:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.