0

In my Laravel app, I am using Nginx as the server. Ubuntu version is 21.10

I also want to use Laravel's console (Tinker) to log stuff. When I set www-data to be the owner of storage folder, which contains the logs, then I can view my app via localhost, but then I can't use Tinker to log information, for example Log::debug("test");, as I get:

UnexpectedValueException with message 'The stream or file 
"/var/www/laravel-app/storage/logs/laravel.log" could not be       
opened in append mode: Failed to open stream: Permission denied

so I set my user temporarily to own storage when I want to use Tinker:

sudo chown -R $USER:www-data storage

But then I can't view my app, as I get the same error as above when trying to access localhost.

How can I make both users have permissions to stoarge?

  • 1
    Are you using a supported Ubuntu release? Which? Please read https://askubuntu.com/help/how-to-ask and https://askubuntu.com/help/formatting – waltinator Apr 09 '22 at 19:28
  • Sorry, added tag for the version and in the question – Ligonsker Apr 09 '22 at 19:30
  • 1
  • @muru yes, it did help (I used solution #1, the one with sudo gpasswd -a "$USER" www-data). Why didn't the solution provided by the answer on this post help? You can see below, using usermod, I added both users to the group yet that didn't work, but using gpasswd and the other lines did help. Why? and do I need to undo the usermod commands I did? – Ligonsker Apr 09 '22 at 19:44
  • @Ligonsker I'd guess the paths in question didn't have r/x permissions for group, which were fixed by the chmod commands in that answer. Now the answer here also has a permission-fixing command, so it might work now. No, you don't need to undo the usermod command. – muru Apr 09 '22 at 20:13

1 Answers1

1

You can use groups. Simply add those users to the www-data group

sudo usermod -aG www-data user1
sudo usermod -aG www-data user2

To ensure that the group has right to read and write:

chmod -r ug+rw path_of_folder_containing data

The you could check with ls -la:

-rw-------  1 aUser aGroup         48  2. Apr 20:44  someFile #only user may read/write
-rw-r--r--  1 aUser aGroup         48  2. Apr 20:44  someFile #user r/w, group and others only read
-rw-rw-r--  1 aUser aGroup         48  2. Apr 20:44  someFile #user & group r/w, others only read

You should log out and in again so that the group change takes effect.

kanehekili
  • 6,402
  • I did that, and when using getent group www-data I see both users listed, but still, only the one who I did chown can operate at a time – Ligonsker Apr 09 '22 at 19:38
  • you need to allow those folders/files that goup members may read or write them. See my updated answer. I usually chmod the folder using the recursion flag "-r" – kanehekili Apr 09 '22 at 19:57