7

I am using the default laravel folder structure and the filesystem public:

'public' => [
    'driver'     => 'local',
    'root'       => storage_path('app/public'),
    'url'        => env('APP_URL') . '/storage',
    'visibility' => 'public',
],

Everything runs on docker and the complete laravel folder is mounted into /var/www/html:

#... Laravel PHP-FPM service definition
  volumes:
    - "./:/var/www/html"
#...

When I run php artisan storage:link and then cd /var/www/html/public and ls -la I see that the symlink exists:

lrwxr-xr-x 1 root root 32 May 5 11:19 storage -> /var/www/html/storage/app/public

If i then check to see if the linked folder also exists in the container cd /var/www/html/storage/app/public everything is there as expected.

Also, when checking ls -la /var/www/html/public/storage/ directly it shows me the content of the linked folder. So everything is working from symlink perspective

# ls -la /var/www/html/public/storage/
total 512
drwxr-xr-x  4 www-data www-data    128 May  5 11:21 .
drwxr-xr-x 11 www-data www-data    352 Apr 19 14:03 ..
-rw-r--r--  1 www-data www-data     14 Feb 16 16:58 .gitignore
-rw-r--r--  1 www-data www-data 519648 May  5 10:58 sample.png

However, when opening the url /storage/sample.png the server returns 404.

The remaining contents of /var/www/html/public do work fine, so for example /var/www/html/public/test.png is visible under localhost/test.png.

On production, everything works fine though. Any ideas why the symbolic link is not working on the local system while the link is actually correct?

I already tried removing the storage link and setting the symlink again.

7
  • Is your local web server configured to follow symlinks? Commented May 5, 2021 at 13:32
  • I added disable_symlinks off; to nginx.conf http directive if that is what you are referring to. Still does not work though.
    – marks
    Commented May 5, 2021 at 14:49
  • What Operating System are you using? (If the answer is Windows, I'm afraid that's your problem - symlinks don't map correctly. If that's the case I will write up an answer with alternative options, citations, etc.)
    – Abulafia
    Commented May 10, 2021 at 10:38
  • MacOS Catalina 10.15.7 :)
    – marks
    Commented May 10, 2021 at 14:12
  • 1
    Tried, storage link is created relative storage -> ../storage/app/public/ but still 404 unfortunately. thanks mysterious tom
    – marks
    Commented May 11, 2021 at 15:18

4 Answers 4

5
+100

You mentioned that you are using docker. I think the reason that it doesn't work locally, but in production, could be that there is a configuration shift between the two deployments.

For it to work the Nginx container must have access to the storage folder, since it is supposed to serve assets that are located there. I'm guessing that is currently not the case and only the public folder is mounted.

Check if the entire project or both ./public and ./storage are mounted into the Nginx container.

Something like this:

services:
  #...
  nginx:
   #...
   volumes:
    - "./public:/var/www/html/public:ro"
    - "./storage/app:/var/www/html/storage/app:ro"
    #...
5

For anyone running Laravel Sail and Docker, you need to go into the Docker container through the command line or in my case inside Docker Desktop's prebuilt terminal and run the following:

su sail (switches to sail user)

php artisan storage:link (adds the sym link into the docker container.

1
  • 1
    One of the best answers on WWW.
    – Melih
    Commented Aug 5, 2023 at 8:43
1

Add these lines on file configuration of nginx:

location ^~ /storage/ {
    root "/var/www/html/public/storage/"
}

Every time when the request start with storage, will access directly the shared path.

1

It's important to make sure your symlink is created from inside the docker container.

If you run storage:link from outside of your container, the symlink will be relative to your local machine.

Get into the container by running:

docker exec -it name_of_your_php_container bash

Once you are inside, you should be able to run php artisan

2
  • 4
    You can set the symlink relatively to fix this problem as well: php artisan storage:link --relative
    – Tom
    Commented May 24, 2021 at 8:21
  • 1
    @Tom You should move that to a separate answer (also worth to mention that it requires symfony/filesystem package to work)
    – apokryfos
    Commented Mar 8, 2023 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.