D
D
Dmitry2018-09-26 18:53:59
Yii
Dmitry, 2018-09-26 18:53:59

Console controller access denied, how to solve?

Good afternoon.
It is necessary to process uploaded images via cron.
Created such a console controller.

class ImagesController extends Controller
{
    public function actionResize()
    {
        Yii::setAlias('@web', dirname(__DIR__) . '/web');
        $path = Yii::getAlias('@web') . '/uploads';

        $images = $this->scanDir($path); // рекурсивный обход директорий
        try{
            foreach ($images as $image){
                $img = Image::getImagine()->open($path . '/' . $image);
                //echo $path . '/' . $image . PHP_EOL;
                if($img->getSize()->getWidth() > 1000){
                    Image::resize($path . '/' . $image, 1000, null)->save($path. '/' . $image, ['jpeg_quality' => 80]);
                    echo 'Done!' . PHP_EOL;
                }
            }
        }
        catch (Exception $exception){
            Yii::getLogger()->log($exception, Logger::LEVEL_WARNING);
            Yii::getLogger()->log($exception, Logger::LEVEL_ERROR);
            echo 'Error' . PHP_EOL;
        }
    }
}

Error when trying to run console command

ImagickException: unable to open image `/home/slonik/localhost/www/memory/web/uploads/country_1/region_5/city_5/JPJvU8GlmulD-KCHdlwBKSHHcVHhBWpk.jpg': Permission denied @ error/blob.c/OpenBlob/2712 in /home /slonik/localhost/www/memory/vendor/imagine/imagine/lib/Imagine/Imagick/Image.php:287

If we transfer this action to the web controller or change the permissions on directories 0777, then everything works fine. The images are being processed.
I understand that the problem is in access rights, but I can not figure out how to solve this problem.
Loaded the console controller on the working server - works without errors. That's just the problem on localhost.
The uploads directory contains several directories that are created when uploading images. Directories are created using mkdir(), but the mode parameter does not affect the permissions in any way, I cannot change them. They just do not change, without any errors.
if(!file_exists($p)){
     mkdir($p, 0777, true);
};

Directory permissions
drwxrwxrwx 1 slonik slonik  18 Сен 26 18:05 uploads
drwxr-xr-x 1 www-data www-data 16 Сен 26 18:05 country_1
drwxr-xr-x 1 www-data www-data 12 Сен 26 18:05 region_5
drwxr-xr-x 1 www-data www-data 20 Сен 26 18:05 city_5

Permissions for uploaded files
-rw-r--r-- 1 www-data www-data 845155 Сен 26 18:05 1g62QFwjgt67kwUcMEV3DcAViJHlAmUN.jpg
-rw-r--r-- 1 www-data www-data 878004 Сен 26 18:05 4HSV4UdL3gWFC5UKNmcYSgDJmYQODj6V.jpg
-rw-r--r-- 1 www-data www-data 824929 Сен 26 18:05 C9asvh5KiEvpOvbkh8SXXyVLzzCQyenr.jpg

Compared these access rights with the rights on the server - everything is the same.
How to solve this problem for localhost?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2018-09-27
@slo_nik

The issue was resolved. They told me how to do it right.
The apache2 mpm-itk module is required .
This module allows you to run virtual hosts as a user. Among other things, this allows you not to think about setting additional rights to certain folders and files on your sites.
I have Ubuntu 16.04., apache2 server.
1) Check if apache2 is built with mpm-itk support
in the console
, as a result, look for the line mpm_itk_module (static)
2) If not, then install the module.
3) Edit the virtual host configuration, enter the following instruction there

<IfModule mpm_itk_module>
      AssignUserId your_user_name your_user_groupe
</IfModule>

4) Restart apache2.
Everything you can use.

D
Denis, 2018-09-26
@sidni

The problem I think is that the web application is launched from the user under which apache or php-fpm is running, and the console application from the one who runs the cron, either you or yourself.
And of course, if these files are created from a web application, then by default they are not available for changes to other users.
from the advice, either save images initially with rights 0777 or add a user who runs cron to the apache group, for example, if it is ubuntu, then usually it is www-data
Somehow I came across that the command
mkdir ($ p, 0777, true);
Doesn't set the right permissions below added chmod
You can
try sudo usermod -aG www-data YOU_USER

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question