How to Solve Error Message “laravel.log” could not be opened: failed to open stream: Permission denied

Posted on

This is an article written where the laravel.log file could not be opened because of the failure to open the stream since it is denied because of the permission. The stream which is mentioned cannot be opened is the stream for creating a new file. The file is a laravel.log file which is created by default in the following directory of laravel root folder directory shown in the tree format :

user@hostname:/var/www/html/laravel-test# tree -a storage/
storage/
├── app
│   ├── .gitignore
│   └── public
│   └── .gitignore
├── framework
│   ├── cache
│   │   └── .gitignore
│   ├── .gitignore
│   ├── sessions
│   │   └── .gitignore
│   ├── testing
│   │   └── .gitignore
│   └── views
│   └── .gitignore
└── logs
└── .gitignore

8 directories, 8 files
user@hostname:/var/www/html/laravel-test#

The laravel.log file supposed to be created in the folder named ‘storage/logs’ inside the laravel root folder. Checking the permission of the ‘storage/logs’ directory. The error itself can be shown in the following image :

How to Solve Error Message "laravel.log" could not be opened: failed to open stream: Permission denied
How to Solve Error Message “laravel.log” could not be opened: failed to open stream: Permission denied
root@hostname:/var/www/html/laravel-test# ls -al | grep storage
drwxrwxr-x 5 user user 4096 Aug 30 16:55 storage
root@hostname:/var/www/html/laravel-test# 

Not only the storage folder but also the logs folder inside of it :

root@hostname:/var/www/html/laravel-test/storage# ls -al | grep logs
drwxrwxr-x 2 user user 4096 Aug 30 16:55 logs
root@hostname:/var/www/html/laravel-test/storage#

As shown in the above output, the owner of the folder named storage/logs is not the one whom is running the Webserver’s service. In the context of this article, the user whom is running is ‘www-data’ or it can be ‘apache’ if the Webserver is an Apache Webserver providing the service for HTTP service.

This error happened because Apache Webserver’s user in this context does not have any permission for creating or writing file called ‘laravel.log’ in the associated folder which is in the ‘storage/logs’.

So, in order to realize it or to solve the problem, just change the ownership of the folder located not limited to the logs folder but for the entire laravel root folder installation as shown below :

root@hostname:/var/www/html# chown -Rv www-data.user /var/www/html/laravel-test/
....
changed ownership of '/var/www/html/laravel-test/storage' from user:user to www-data:user
changed ownership of '/var/www/html/laravel-test/storage/logs' from user:user to www-data:user
....
changed ownership of '/var/www/html/laravel-test/logs/localhost.laravel.test.web-error_log' from root:root to www-data:user
changed ownership of '/var/www/html/laravel-test/logs/localhost.laravel.test.web-access_log' from root:root to www-data:user
changed ownership of '/var/www/html/laravel-test/logs' from user:user to www-data:user
changed ownership of '/var/www/html/laravel-test/' from user:user to www-data:user
root@hostname:/var/www/html#

After changing the ownership of the folder, if there are nothing more hindrance that can affect the the laravel.log file to be created and to be written. The file will should be available there.

One thought on “How to Solve Error Message “laravel.log” could not be opened: failed to open stream: Permission denied

Leave a Reply