phplinuxdockernginx

After migrating the Docker container to Linux, images are not loading


After migrating the Docker container to Linux, images are not loading; in the browser, I receive:

GET /storage/image/WBRNZ0c6tnOfED2APNuYRcSjqIfzvJWXYnBlNRkB.jpg HTTP/1.1" 403 The docker-compose.yml file looks like this:

services:
      nginx:
        image: nginx:latest
        volumes:
          - ./:/var/www/
          - ./_docker/nginx/conf.d/:/etc/nginx/conf.d/
        ports:
          - "8876:80"
        depends_on:
          - app
        container_name: project_nginx
      app:
        build:
            context: .
            dockerfile: _docker/app/Dockerfile
        volumes:
          - ./:/var/www/
        depends_on:
            - db
        container_name: project_app

      db:
          image: mysql:8.0
          restart: always
          volumes:
              - ./tmp/db:/var/lib/mysql
          environment:
              MYSQL_DATABASE: blogdb
              MYSQL_ROOT_PASSWORD: root
          ports:
              - "8101:3306"
          command: mysqld --character-set-server=utf8 --collation-server=utf8_unicode_ci
          container_name: project_db
      phpmyadmin:
          image: phpmyadmin/phpmyadmin
          container_name: phpmyadmin
          environment:
              PMA_HOST: db
              PMA_USER: root
              PMA_PASSWORD: root
          ports:
              - "8080:80"
          depends_on:
              - db

The nginx.conf file looks like this:

    server {
        listen 80;
        index index.php index.html;
        error_log  /var/log/nginx/error.log;
        access_log /var/log/nginx/access.log;
        root /var/www/public;
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass app:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }
        location / {
            try_files $uri $uri/ /index.php?$query_string;
            gzip_static on;
        }
    }


At first, I thought it was a permissions issue. I found information on the forum stating that Nginx does not have permissions to a certain folder, and this can be fixed with the command:

chmod 777 -R storage

However, in this case, I get: chmod: changing permissions of 'storage': Operation not permitted. Then I found that if I run it with sudo, I can grant permissions as the superuser. The command works, but the images still do not load. I dug further and checked the permissions of the folders by running the ls -l command:

drwxrwxrwx 5 www-data www-data 4096 Nov 5 22:58 storage

The storage folder seems to have permissions for www-data (as I understand, this is typically the server's username).

If I try to directly access http://localhost:8876/storage/image/8CvZHzr5BJnZseIkh8Ne7sTMyVFHcGBUoXeAmNXS.jpg, I also get an error.

Thank you in advance; please don't be harsh, I'm just learning!


Solution

  • Enter the Laravel container

    First check if the storage folder link is in your public folder. If you can't find it, run

    php artisan storage:link
    

    https://laravel.com/docs/11.x/filesystem#the-public-disk