Laravel无SSH/命令环境下手动创建Storage软链接的方法
Hey there! I’ve dealt with this exact storage link headache on cPanel before—super frustrating when you can’t run artisan commands via SSH. Let’s break down a few solid solutions that should get your storage link working:
Your initial approach was on the right track, but missing a couple key checks and proper imports. Here’s the corrected version:
First, add this import at the top of app/Providers/AppServiceProvider.php:
use Illuminate\Support\Facades\File;
Then update the boot() method to include existence checks and error handling:
public function boot() { // Only create the link if it doesn't already exist if (!File::exists(public_path('storage'))) { try { File::link(storage_path('app/public'), public_path('storage')); } catch (\Exception $e) { // Log the error for debugging (check storage/logs/laravel.log) \Log::error('Failed to create storage link: ' . $e->getMessage()); } } }
The main issues with your original code were likely:
- Not checking if the link already exists (which throws an error if it does)
- Using a more complex service resolution instead of the cleaner
Filefacade
Many cPanel instances let you create symlinks directly through the file manager—no code needed:
- Log into cPanel and open the File Manager
- Navigate to your
public_htmldirectory (this is where your Laravel public folder’s files live, or the public folder itself if using a subdomain) - Right-click in an empty space and select Create Symbolic Link (look for this option in the top bar dropdown if right-click doesn’t show it)
- For the Link Path, enter the full server path to your
storage/app/publicfolder. Example:home/yourcpanelusername/laravelproject/storage/app/public - For the Link Name, enter
storage - Click Create—if you get a permission error, double-check that your
storage/app/publicfolder has 755 permissions
If the above methods fail, create a quick temporary script to run the link command via your browser:
- Make a new file named
createstorage.phpin your public directory - Paste this code into it:
<?php require __DIR__.'/../vendor/autoload.php'; $app = require_once __DIR__.'/../bootstrap/app.php'; // Bootstrap the Laravel application $app->boot(); use Illuminate\Support\Facades\File; if (!File::exists(public_path('storage'))) { File::link(storage_path('app/public'), public_path('storage')); echo "Storage link created successfully! You can delete this script now."; } else { echo "Storage link already exists—no action needed. Delete this script!"; }
- Visit the script in your browser (e.g.,
https://yourdomain.com/createstorage.php) - Delete the script immediately after it runs—leaving it exposed is a security risk!
- If you’ve moved Laravel’s
publicfolder contents intopublic_html(standard for cPanel), ensure your paths are correct.public_path('storage')will point topublic_html/storage, andstorage_path('app/public')points to your Laravel root’sstorage/app/public. - Permissions matter! Your
storagedirectory and subfolders should be owned by your cPanel user (not root) with 755 permissions for folders and 644 for files.
内容的提问来源于stack exchange,提问作者Kabiru Wahab

