Getting error: "failed to open stream: Permission denied"
I recently had to reboot my LAMP-stack Linode running CentOS 7 and received the following error:
The stream or file "/var/www/html/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied
Stacktrace:
#10 UnexpectedValueException in /var/www/html/<redacted>.php:107
#9 :write in /var/www/html/<redacted>.php
#8 :handle in /var/www/html/<redacted>.php
#7 :addRecord in /var/www/html/<redacted>.php
#6 :error in /var/www/html/<redacted>.php
#5 :writeLog in /var/www/html/<redacted>.php
#4 :error in /var/www/html/<redacted>.php
#3 :error in /var/www/html/<redacted>.php
#2 :report in /var/www/html/<redacted>.php
#1 :report in /var/www/html/<redacted>.php
#0 :handleException in [internal]:0
Seems like a permissions issue but not sure where to go from here, new to using Laravel, help appreciated!
1 Reply
Essentially yes, the issue is sort of related to permissions, but in this case it is not managed with the chmod
command as you might assume.
Since you're specifically running Laravel with Centos 7, try the following to adjust some SELinux rules:
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html/storage/logs(/.*)?" # 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 will restart the service to apply the new rules
In the case that the above commands aren't working, make sure you have the proper utility installed:
yum install policycoreutils-python -y
While it's possible that an alternative here might be to disable SELinux altogether with
$ sudo setenforce 0
imo its better to just tackle the issue at hand.
More on the topic can be found in this Stackoverflow post