How to Implement Symlink with Laravel Project in Shared Hosting (Alternate Storage: link)

Debrian R. Saragih
KuliKode
Published in
2 min readApr 29, 2022

Hi, this time I’ll show you how to create a symbolic link with laravel project, where our project was placed in shared hosting. Many of you may implement the default laravel function in the terminal like “php artisan storage:link” to create this symlink. But, what if our public_html location is different from our main project link?

Before we dive into the implementation, we have to know how important the symlink is in our laravel project, especially to make our project file and public file separate and secure. In the picture below, we can see that our public file for domain1.com is in public_html/domain1.com folder. As you can see, we save the image data in the root folder of public_html/images. We cannot access the file above of our main addon domain (domain1.com) because our scope is only in the files below the domain itself.

In laravel, we can read the documentation,

https://laravel.com/docs/9.x/filesystem,

using the artisan command php artisan storage:link, will create a symbolic link from public/storage to storage/app/public

This method is limited to creating a symlink with the default Laravel Project structure above, if we put our project file in a case like the first picture, we cannot use this function.

Many laravel programmers in a forum (link), suggest creating a php file with some code like this:

$targetFolder = $_SERVER['DOCUMENT_ROOT'].'/storage/app/public';
$linkFolder = $_SERVER['DOCUMENT_ROOT'].'/public/storage';
symlink($targetFolder,$linkFolder);
echo 'Symlink completed';

I’ve tried that method and give back an error message. So, is there any solution to overcome that issue? Back to the basic symlink idea, just write a line code and modify it with your own project's location.

In my case, as you see in the picture, just type this command in your terminal hosting (in any location you are):

ln -s /home/nctests/public_html/images /home/nctests/public_html/domain1.com/images

Done! whenever you call the images, for example, domain1.com/images/abc.jpg, it will automatically refer to the public_html/images folder.

Please read the more specific explanation in https://linuxize.com/post/how-to-create-symbolic-links-in-linux-using-the-ln-command/

Thank you.

--

--

Debrian R. Saragih
KuliKode

I write code // IT enthusiast // web developer. Learning by practicing & googling. https://www.linkedin.com/in/debrian-ruhut-saragih/