8

I am using Laravel 6.x inside a Homestead environment and have recently come up with some weird errors when I attempt to login via an Angular frontend - once it hits the login endpoint it gives me some strange errors (see below

The stream or file "/home/vagrant/code/abc-backend/storage/logs/laravel-2020-07-23.log" could not be opened: failed to open stream: Permission denied

The file above doesn't exist for some reason (other log files for other dates exist but not the one from the 23rd July). I have then manually created the file as follows & chmod'd it to 777:

touch storage/logs/laravel-2020-07-23.log
chmod 777 storage/logs/laravel-2020-07-23.log

When I reload the page the previous error has gone but I now get the following error instead :

file_put_contents(/home/vagrant/code/abc-backend/storage/framework/cache/data/1c/6e/1c6ea8378a1030f85a05f4cb2262de1e2164efa6): failed to open stream: No such file or directory",

I have also tried the following with no joy:

chown -R vagrant:www-data /home/vagrant/code/abc-backend/storage

chmod -R g+w /home/vagrant/code/abc-backend/storage

I have tried lots of things with php artisan to try to remedy these errors including running php artisan cache:clear commands with no joy - can anyone else give me an insight into why I am getting both the log file error and the cache/data error & the best methods to fix them both?

Let me know if you need any more info with my setup that will assist.

-- Update -- I am using Virtual Box 6.0 the Homestead box runs Ubuntu 18.04.3 LTS Homestead v10.9.1

Homestead.yaml file --

---
ip: "192.168.10.10"
memory: 2048
cpus: 2
provider: virtualbox
authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

folders:
    - map: ~/Sites
      to: /home/vagrant/code

sites:
    - map: laravel.local
      to: /home/vagrant/code/laravel/public
      type: "laravel"

    - map: abc.local
      to: /home/vagrant/code/abc-ui/app
      type: abc
      variables:
          -   key: abc_TIER
              value: homestead

    - map: dev.abc.local
      to: /home/vagrant/code/abc/public
      type: dev
      variables:
          -   key: abc_TIER
              value: homestead

    - map: api.abc.local
      to: /home/vagrant/code/abc-backend/public

databases:
    - homestead
    - dev

features:
    - mysql8: true
    - pm2: true
    - webdriver: true

variables:
    - key: ABC_TIER
      value: homestead

enter image description here

The framework/data/cache directory looks like this - the subdirectories look the same too (vagrant/vagrant and same drwxrwxrwx permissions).

drwxrwxrwx 1 vagrant vagrant 160 Jul 20 09:50 . drwxrwxrwx 1 vagrant vagrant 160 Jul 17 15:06 .. drwxrwxrwx 1 vagrant vagrant 96 Jul 20 09:50 1c -rwxrwxrwx 1 vagrant vagrant 6148 Apr 3 10:52 .DS_Store -rwxrwxrwx 1 vagrant vagrant 14 Jul 17 15:06 .gitignore

Zabs
  • 13,852
  • 45
  • 173
  • 297
  • 1
    Is the project shared with a shared folder? If so, what type is it? NFS, Samba, etc. What OS is your host? What is your VM provider? VBox, vmware, etc. – dbf Jul 23 '20 at 09:38
  • @dbf added some more info that may help :) – Zabs Jul 23 '20 at 09:56
  • I believe you need to set the ownership (chown) to `www-data` instead, since that's the user the web service runs on. Also, there's an [excellent answer here](https://stackoverflow.com/a/37266353/3209381) related to the same issue. – sykez Jul 29 '20 at 16:50
  • We have that error on our production server and we didn't find solution yet. Can you share your `.env` file or what cache driver you are using? I think that problem is with `file` cache driver, and if you swap it with i.e `redis` errors will gone – maki10 Jul 30 '20 at 10:06

10 Answers10

2

What are you using for web server? nginx or apache?

If you are using nginx, you need to change the group of storage folder as following.

chown -R vagrant:www-data storage

If you are using apache, you need to change the group of storage folder as following.

chown -R vagrant:apache storage

Or please do this.(not recommended)

chmod -R 777 storage
NeK
  • 846
  • 5
  • 13
1

Directories within the storage and the bootstrap/cache directories should be writable by your web server or Laravel will not run.

Have you tried:

chmod -R o+w /home/vagrant/code/abc-backend/storage

?

If that doesn't get it, you may need to examine the contents of /home/vagrant/code with ls -al to see who owns the files and use chown to modify them to the expected web user.

James Clark
  • 1,285
  • 2
  • 9
  • 14
  • added screenshot showing the `ls -al` which shows which user owns the directories etc.. and they are show vagrant which would be correct – Zabs Jul 23 '20 at 12:07
  • What do the permissions on subfolders look like, specifcally the ```/home/vagrant/code/abc-backend/storage/framework/cache/data``` folder which is causing the error? – James Clark Jul 23 '20 at 13:41
  • This is how my storage/framework/cache/data looks like when I do a `ls -al` drwxrwxrwx 1 vagrant vagrant 160 Jul 20 09:50 . drwxrwxrwx 1 vagrant vagrant 160 Jul 17 15:06 .. drwxrwxrwx 1 vagrant vagrant 96 Jul 20 09:50 1c -rwxrwxrwx 1 vagrant vagrant 6148 Apr 3 10:52 .DS_Store -rwxrwxrwx 1 vagrant vagrant 14 Jul 17 15:06 .gitignore – Zabs Jul 23 '20 at 15:07
1

What I usually do for permissions and ownership is something like:

sudo chown -R ubuntu:www-data /var/www/project
sudo chmod -R ug+rwx storage bootstrap/cache .git
sudo find . -type f -exec chmod 664 {} \;
sudo find . -type d -exec chmod 775 {} \;

This being said, it sounds like your web server is having trouble creating the log file.

To test this out:

  • have reliable reproduction steps (so you are getting the error)
  • do sudo chown -R www-data:www-data /home/vagrant/code/abc-backend/storage.
  • retest

If this works, you have some options:

  • leave it like this, job done. Just be aware that in the future something else might come up like below.
  • investigate further issues: If you decide to actually to sudo chown -R www-data:www-data /home/vagrant/code/abc-backend you will likely at some point come across access issues where running a command as one user (let's say vagrant) generates files which should be accessible by another (let's say www-data). These can be: git pull, composer install, cron jobs, likely others. But this is more likely in a collaborative environment as opposed to homestead.
Lpgfmk
  • 391
  • 1
  • 4
  • 17
1

I think the problem is that permissions are not persisted to subfolders after cache:clear (as that command is run by the vagrant user, not apache). Cache uses a lot of subfolders and they seem to be owned (both user and group) by vagrant, so apache (and php) can't write there.

Instead:

chown -R vagrant:www-data /home/vagrant/code/abc-backend/storage
chmod -R g+w /home/vagrant/code/abc-backend/storage

try this:

chown -R vagrant:www-data /home/vagrant/code/abc-backend/storage
chmod -R g+sw /home/vagrant/code/abc-backend/storage

The +s will set the sticky bit, so that any new folder or file created under that path will be automatically owned by that same group (www-data in this case).

ivanhoe
  • 4,593
  • 1
  • 23
  • 22
0

Once I was stuck in this kind of issue and got remedy with the following steps

  1. Go to bootstrap directory and delete config.php and packages.php.
  2. Then run php artisan config:cache in the terminal.
  3. Then run composer du in the terminal.

Try swapping the step 2 and 3 after deleting config.php and packages.php from bootstrap directory

x00
  • 13,643
  • 3
  • 16
  • 40
0

As I can see, vagrant use its own user to access for Laravel project. Please Try to set config.ssh.username to www-data in your vagrant config.ssh.

https://www.vagrantup.com/docs/vagrantfile/ssh_settings#config-ssh-username

Egretos
  • 1,155
  • 7
  • 23
0

Vagrand needs certain permissions over the Laravel directory. You need to first change ownership of the laravel directory to our web group.

sudo chown -R :www-data /home/vagrant/code/abc-backend/laravel

Now the web group owns the files instead of the root user. Next we need to give the web group write privileges over our storage directory so it can write to this folder. This is where you store log files, cache, and even file uploads.

sudo chmod -R 775 /home/vagrant/code/abc-backend/storage

As it says in the documentation: After installing Laravel, you may need to configure some permissions. Directories within the storage and the bootstrap/cache directories should be writable by your web server or Laravel will not run

Then you should use it too.

sudo chmod -R 775 /home/vagrant/code/abc-backend/bootstrap/cache
MrEduar
  • 1,784
  • 10
  • 22
0

This may help - Try to clear configuration cache by

php artisan config:clear 

and view cache too

php artisan view:clear 

[Refer : tecadmin.net/clear-cache-laravel-5/]

Permissions related issue : Consider If you have put any cron and log file created from there and also you try to wrtie text to same log file with another script execution... then it consider as different user and return as permission denied... So when creating log itself give permission to other users OR use different log files for cron and other script.

GQ.
  • 147
  • 7
0

Could you try

folders:
- map: ~/Sites
  to: /home/vagrant/code
  options:
    mount_options: ['dmode=777','fmode=777']
Zeppi
  • 1,175
  • 6
  • 11
0

You can try the following:

sudo nano /etc/php/7.x/fpm/pool.d/www.conf

Change the following paramaters:

user = www-data
group = www-data

To:

user = vagrant
group = vagrant

Then, remember to restart PHP FPM

sudo service php7.x-fpm restart
Boon
  • 687
  • 4
  • 12
  • 25