Search code examples
phplaravelnginxvpslaravel-9

Images shows 404 on production server(Laravel project)


I've deployed laravel project to vps ubuntu server with LEMP stack. Everything works fine, but images shows 404, even there is a symbolic link to storage/app/public folder. I think, this issue is about permissions and I tried few permission mods, but still the same.

This is my project with their permissions:

enter image description here

This is inside storage/app/public folder:

enter image description here

This is my public folder with their permissions and the symbolic link:

enter image description here

And in case if it is needed here are my ngnix server configuration(/etc/nginx/sites-available/default):

server {
    listen 80 default_server;
    #listen [::]:80 default_server;

    root /var/www/html/west-hospital-admin/public;
    #root /home/west/west-hospital-admin/public;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html index.php;
    server_name _;

    location / {
        try_files $uri $uri/ /index.php$query_string;
    }
    # pass PHP scripts to FastCGI server

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    }
}

I've helper function called _asset() :

function _asset($path = null, $data = null): string
    {
        $darkmode = Settings::select('darkmode')->firstOrFail()->darkmode;
        $placeholderImg = $darkmode ? 'mazer/img/no-img-dark.png' : 'mazer/img/no-img.png' ;

        return asset(
            $path === null
                ? $placeholderImg
                : ($data === null || $data == ''
                    ? (file_exists($path)
                        ? $path
                        : $placeholderImg)
                    : (file_exists('uploads/' . $path . '/' . $data)
                        ? 'uploads/' . $path . '/' . $data
                        : $placeholderImg))
        );
    }

And I'm loading image like this:

<img src="{{ _asset('images/vacancies', $vacancy->image) }}" height="60px" width="80px">

Images are loading in the html like this:

http://109.74.199.165/uploads/images/vacancies/164967-1659941875.webp

And there is image in that directory but it shows 404 when visiting the link.


Solution

  • I've figured it out. It's permissions issue. chmod -R 755 storage will do the work. But if a new folder is created, it is necessary to give permission again for that folder.