skip to Main Content

Now and then we face a destructive issue within a Laravel project:

Storage log file

production.ERROR: Unable to create lockable file: /var/www/html/storage/framework/cache/data/... Please ensure you have permission to creto create files in this location.
#0 /var/www/html/vendor/laravel/framework/src/Illuminate/Filesystem/LockableFile.php(43)

When the issue happens ls -l gives

drwxrwsr-x+   2 apache   apache     4096 Sep  2 14:36 logs

To solve the issue we run sudo chown -R ec2-user:apache logs/ which gives

drwxrwsr-x+   2 ec2-user   apache     4096 Sep  2 14:36 logs

But this is a manual fix…

Therefore, I would like to ask:

A) How to prevent the file system suddenly changing owner which breaks the coding?

B) Alternatively how to trigger production error notifications within a Laravel project to be warned about such issues?

2

Answers


  1. Answer to your second query, you can use either of them for error reporting:

    1. sentry
    2. flare
    3. busnag

    All the exceptions are handled by AppExceptionsHandler class and in this class, there is a method named register you can configure any of the above tools in there so that exceptions can be reported there. Else you can build your custom report method like sending an email to you with an exception message or sending a slack notification.

    Login or Signup to reply.
  2. To fix this issue permanently you have to give the right permission in the code so go to config/logging.php you will find something like below:

        'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['daily'],
            'ignore_exceptions' => false,
        ],
    
        'single' => [
            'driver' => 'single',
            'path' => storage_path('logs/laravel.log'),
            'level' => 'debug',
        ],
    
        'daily' => [
            'driver' => 'daily',
            'path' => storage_path('logs/laravel.log'),
            'level' => 'debug',
            'days' => 14,
        ],
    
        ...
    
    ],
    

    add 'permission' => 0775, to the channels used, so it will be like below (I have added it to single and daily channels):

        'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['daily'],
            'ignore_exceptions' => false,
        ],
    
        'single' => [
            'driver' => 'single',
            'path' => storage_path('logs/laravel.log'),
            'level' => 'debug',
            'permission' => 0775,
        ],
    
        'daily' => [
            'driver' => 'daily',
            'path' => storage_path('logs/laravel.log'),
            'level' => 'debug',
            'days' => 14,
            'permission' => 0775,
        ],
    
        ...
    
    ],
    

    This fix Tested on Laravel 6

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search