laravel symlink is changed when code is pushed to live site - laravel-5

I am using laravel 5.5's storage symlink to store images. When I push code to the git and pull from live site then symlink path on live site becomes same as is on my local code .
My local has this symlink
storage -> /home/path/to/project/app/public/
But live site expects this symlink path
storage -> /var/www/html/website.com/public_html/project_code/storage/app/public/
Every time I have to delete symlink and create it again on live site.
I have do these steps on live site
cd public
rm storage
cd ..
php artisan storage:link
after doing these steps my symlink becomes according to live site and then it starts working.
Live site should have this symlink
storage -> /var/www/html/website.com/public_html/project_code/storage/app/public/
project_code/.gitignore :
public/storage
project_code/storage/app/.gitignore
*
!public/
!.gitignore
How I can get rid of this issue.
Thanks in advance!

As your symlink points to another location within the same project, you can safely use relative paths while linking, instead of having absolute one (which is what artisan is setting up). That would require manual linking though:
cd app/public
ln -s ../storage/app/public storage
Remove current storage symlink first if you have it already.

You might consider setting up a post-receive hook in the target Git repo (the one you are pushing to). See this one for instance.
In that hook, you can add any step you need (like fixing the symlink).

Related

Symlink in Laravel current release folder from external folder

I am using zero downtime deployment for Laravel and I have a folder which is updated regularly with weather files.
The folder structure is like:
releases/1 - where the current release is.
current/ - linked to /releases/1 and this is where Apache server root is.
public/weather - this is the folder where weather files are stored (via ftp process).
Can I use symlink on public/weather -> /current/public/weather?
Command I try to use:
ln -nfs --relative /var/www/html/public/weather /var/www/html/current/public/weather
the command appears to work, however I can not see it linked with ls -al.
If I try:
ln /var/www/html/public/weather /var/www/html/current/public/weather
I get this error.
ln: /var/www/html/public/wdisplay: hard link not allowed for directory
Thank you

laravel symlinks on a window machine

Using a windows server, I have a laravel installation.
I have a storage folder that I have created on 'root/storage'
When I save images from the website, the images are stored in this folder.
I use this to save the files:
Storage::disk('local')->put('course_badges/'.$pre_string . $request->file('badge')->getClientOriginalName(), File::get($request->file('badge')));
When I try to reference the file like so 'https://app.com/storage/file_name.jpg', laravel gives me a 'Sorry, the page you are looking for could not be found.' error.
However is I copy the files to '/public/storage/'
The image is then referenced correctly.
I have run the: php artisan symlink function.
I have tried to run ln -s ../storage/ command in powershell, but windows tells me that ln is not recognized.
What I am trying to achieve is to have my files saved in the root/storage folder (and sub folders)
and to reference them using https:app.nam/storage/file
I had simmilar issue
as u mentioned file is stored in root/storage means problem with symlink between root/storage to public/storage to fix this
remove storage folder from public dir
Note:- keep backup first
then run php artisan storage:link
it should work

Laravel deployment - avoid overwriting storage directory when deploying from git

I have a laravel app where users can upload images. The images are uploaded to the storage/app/public/images folder which has a symlink to public/storage
The first time i set up the project on digital ocean I use the following command to deply from github:
cd /var/www/html/quickstart
git clone https://github.com/laravel/quickstart-basic
Development and testing of the app is done locally and then pushed to github. After any patches I run the above each time to redeploy the updated application but that overwrites the storage directory.
How do I push updates to my live site from github without overwriting the storage directory?
Please note i'm not an expert on git so bear with me.
* UPDATE *
Note currently i have a gitignore file in the storage\app\ directory which has:
*
!public/
!.gitignore
There is also one inside storage\app\public\ directory which has:
*
!.gitignore
you can add the directory to .gitignore file.
like this:
/storage/app/public/images/

Laravel - How to revert local storage symlink and refresh

When I save images to storage they show in the storage/app directory, but they don't show in public/storage. I also noticed that a storage/app/public directory seems to have been created which also contains everything within storage/app.
I can't work out how I managed to cause this mess, and I think it might make sense to revert these directories to how it should have been to begin with (with a new laravel project), remove any existing symlinks, and start again - does this sound like the best approach? And how would I go about doing this if I have set the symlink up incorrectly?
You can always delete the link:
cd public
rm storage
And create a new one without using the storage:link command if you need to link different locations:
\File::link(storage_path('dir1'), public_path('dir2'));
Or create a symlink manually:
ln -s /full/path/to/storage/dir1 /full/path/to/public/dir2
I had this problem and I fixed it by adding this to my deployment script:
cd public
rm storage
cd ..
php artisan storage:link
Before you do that, you can SSH into your deployment server and navigate to the /public folder. You might see a storage folder in there, and you should notice it's a symlink. You should leave it there and add my above commands to your deployment script.
If you remove storage link manually, you'll need to skip these on the first deployment:
cd public
rm storage
cd ..
Then every deployment after, you can do:
cd public
rm storage
cd ..
php artisan storage:link
If you don't do that, your deployment will fail probably because if rm storage throws an error, it will fail the build. Pay close attention to what I'm saying. You must be very specific when doing CLI commands, unless you can add some bash script that handles the 2 cases where ./public/storage exists (1) and when it doesn't (2).
You might gloss over my mention about deleting the storage symlink reference manually, but don't forget that if you deployment script does it and then fails for another reason, the symlink will be gone.
Someone should post an answer that uses a bash script to ensure both cases can be handled. I don't have the skill for that.
NOTE: php artisan storage:link only works while you are in the root directory of your project. That's why my above script starts with cd public and why it does cd ... If you aren't in the root at the time, the php artisan command will fail.
That is actually how it should be! Laravel has a command to symlink your public directory to the storage directory:
php artisan storage:link
The idea is to keep a persistent storage between deployments.
This convention will keep your publicly accessible files in one
directory that can be easily shared across deployments when using zero
down-time deployment systems like Envoyer.
https://laravel.com/docs/5.5/filesystem
Simple Way
Go to public folder Just delete/remove the storage link inside public/
Then Run the command
php artisan storage:link
the above command will link storage again
For windows users, I have tested this with Windows 10 and Laravel 7
Steps:
cd to your <project_root>/public directory and run rmdir storage - it will remove the link
cd back to project root directory and run php artisan storage:link to link again
The links should now be refreshed and viewable without issues.
php artisan storage:link --force

Why does symlink create an unnecessary "folder"?

Say the root folder on my server is /home/bong/www/, so mydomain.com will serve up index.php from /home/bong/www/.
Then I cloned an hg repository and placed it at /home/bong/hg/hgrepo/.
When I am on /home/bong/ I created a symlink as follow so that going to mydomain.com will serve up content from the hg/hgrepo directory:
ln -s hg/hgrepo www
When I go into www, I see hgrepo -> hg/hgrepo
Problem is then when i go to mydomain.com, it still doesn't work ... but mydomain.com/hgrepo does.
What did I do wrong?
I'm betting that www already existed, so when you created the link, it placed it inside the directory that was already there. Solution: delete www then create the link again.

Resources