5

When I navigate to Laravel app on my CentOS server, it gives HTTP ERROR 500

enter image description here

So, when I checked my server error log, it says this error

PHP Fatal error:  Uncaught UnexpectedValueException: The stream or file "/var/www/html/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied in /var/www/html/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:107\nStack trace:\n#0 

I have already set 777 permission to storage and bootstrap directories.

This very Laravel application runs fine on another server. So, what's the big deal here ?

Tharindu Thisarasinghe
  • 3,846
  • 8
  • 39
  • 70
  • 2
    it might be related to file permission of SELinux, try searching for it, and try disabling SELinux first, if it is the culprit, then you need to find a way to enable SELinux while allowing your site to function properly – am05mhz Nov 23 '17 at 05:02
  • @am05mhz Ohh I will search for it but, that's to Greek for me since I'm kinda new to this server thing. – Tharindu Thisarasinghe Nov 23 '17 at 05:05
  • here for a starting line https://www.tecmint.com/disable-selinux-temporarily-permanently-in-centos-rhel-fedora/ – am05mhz Nov 23 '17 at 05:06
  • Try this https://stackoverflow.com/questions/31975204/laravel-5-failed-to-open-stream-permission-denied-exception-when-connecting – Ankur Tiwari Nov 23 '17 at 05:09
  • you should never set permissions to 777. Try to `touch /var/www/html/storage/logs/laravel.log` and make your php user the owner, e.g. use `chown`. Then check if it works. – Gordon Nov 23 '17 at 05:51
  • Thanks all of you. Actually, the cause of the problem was SE-Linux. So, thanks again @am05mhz for pointing that out. :) – Tharindu Thisarasinghe Nov 23 '17 at 12:44

3 Answers3

7

This error can be fixed by disabling SE-Linux.

Check if it has been enabled by typing...

$ sestatus

So, disable it by typing...

# setenforce 0

It is said that the system needs to be restarted to to take effect the changes.

However, for me, restarting Apache was enough and fixed the problem :-)

Hope this helps!

Tharindu Thisarasinghe
  • 3,846
  • 8
  • 39
  • 70
  • 8
    it is very bad to keep selinux off !! instead use `chcon -R -t httpd_sys_rw_content_t storage` from the project directory then restart http, – George Mar 20 '18 at 08:57
  • just used @George method using chcon, it's more secure than disabling selinux. Thanks. – Leutecia Feb 21 '23 at 09:35
2

If your application is laravel and you use sail so add these lines in end of .env file:

WWWGROUP=1000
WWWUSER=1000

I know its late to answer but maybe helps for others.

akbar
  • 625
  • 6
  • 12
  • Also, try stopping Docker (if using Laravel Sail), then running: `chown yourusername:yourgroup -R .` (refer to output of `ls -la` in Laravel directory for username/group), `chmod 750 -R .`, start Docker, and run `php artisan route:clear`, `php artisan config:clear`, and `php artisan cache:clear` – Tyler Jun 22 '21 at 17:22
1

I use Ubuntu 22.04. If you don't want to disabling SE Linux, the solution is: SSH into Docker container: docker exec -it app_name-laravel.test-1 /bin/bash Change the owner of storage/framework and storage/logs to sail not www-data:

chown -R sail:sail storage/framework
chown -R sail:sail storage/logs

If it still don't work, try to install htop inside that docker container. Run htop. Find the user of /var/www/artisan serve. Then repeat the commands above but with that user, not sail. Good luck!

Update

Or better solution. Create a group for root and user from docker (mine is sail):

groupadd sailroot

then add both root and sail to the group:

usermod -a -G sailroot sail
usermod -a -G sailroot root

Change group owner of storage logs and framework to the group:

chown root:sailroot -R storage/framework

Change file permission to 775 for both:

chmod 775 -R storage/framework

In this way, you can using php artisan serve using php installed locally, or with docker using sail. Remember to leave the user to root, and group sailroot (or whatever you named)

Enkracken
  • 181
  • 2
  • 6