jasonomarion's avatar

Storage new folder default owner is ROOT?

I'm using "dedicated folder for each user" structure. When somebody uploads file to my app, it will be stored in special folder, then it will be converted by FFMpeg library to required format and will be returned to user.

For example:

// validations ...
$path = Storage::disk('tmp')->putFileAs('toConvert/'.auth()->id(),$request->file,$name);
// convert to required format with FFMpeg and return to user

Everything works well, but not for new users.

For example: New registered user has id 5000. When he will try to upload something, Laravel fails in attempt to put file in new folder 5000: ./storage/temp/toConvert/5000/EXAMPLE.ogg with error .ERROR: fopen(/var/www/myCoolApp/storage/tmp/toConvert/5000/EXAMPLE.ogg): failed to open stream: Permission denied.

This because a new generated folder has owner and group: root. Only after chown nginx. /var/www/myCoolApp/storage/tmp/toConvert/5000 command Laravel put files without any errors.

I can't always do command above after a new user registration! Why Laravel creates new folder with user:group root:root?

chown nginx. /var/www/myCoolApp/storage /var/www/myCoolApp/bootstrap/cache -fR and chmod 775 /var/www/myCoolApp/storage /var/www/myCoolApp/bootstrap/cache -fR doesn't help. New folders will be created as root by Laravel.

php -i | grep USER output:

USER => root
$_SERVER['USER'] => root

But /etc/php-fpm.d/www.conf has:

user = nginx
group = nginx
listen.owner = nginx
listen.group = nginx
listen.mode = 0660

php artisan:cache clear doesn't help.

ps aux | grep php output:

root      1261  0.0  0.3 404040 14088 ?        Ss   18:42   0:00 php-fpm: master process (/etc/php-fpm.conf)
nginx     1262  0.0  0.7 419192 29712 ?        S    18:42   0:00 php-fpm: pool www
nginx     1263  0.0  0.6 419028 26856 ?        S    18:42   0:00 php-fpm: pool www
nginx     1264  0.0  0.7 417108 27960 ?        S    18:42   0:00 php-fpm: pool www
nginx     1265  0.0  0.7 419156 30192 ?        S    18:42   0:00 php-fpm: pool www
nginx     1266  0.0  0.6 419052 26848 ?        S    18:42   0:00 php-fpm: pool www

config/filesystems.php:

        'tmp' => [
            'driver' => 'local',
            'root' => storage_path('tmp'),
        ],

Laravel version 5.8. Php version 7.2.34.

I have an another app with similar folders structure for avatars (special folder for each user), but it works well. php -i | grep USER command shows root too.

I will be grateful if you point me in the right direction.

0 likes
2 replies
lakazai's avatar

Look in your php-fpm config file (eg: /etc/php7.2/fpm/pool.d/www.conf). There you will find options user and group.

jasonomarion's avatar

After playing with setfacl, chmod g+s and chmod u+s the problem solved. Now new directories and files has nginx:nginx.

Please or to participate in this conversation.