将public改为public_html后,如何正确配置Laravel存储软链接?
解决Laravel
storage:link 指向错误public目录的问题 1. 清除配置缓存
Laravel会缓存配置文件,旧的public路径可能仍在缓存中,先执行以下命令清除缓存:
php artisan config:clear php artisan cache:clear
2. 验证public_path()是否正确
通过Artisan Tinker确认路径配置是否生效:
php artisan tinker
在Tinker环境中执行:
echo public_path();
如果输出不是项目根目录下的public_html,请排查以下配置:
- 检查
AppServiceProvider.php的register方法,确认路径绑定无拼写错误:public function register(): void { $this->app->bind('path.public', function () { return base_path() . '/public_html'; }); } - 修改
bootstrap/app.php中的usePublicPath配置,去掉realpath避免路径解析失败:->registered(function ($app) { $app->usePublicPath(base_path('public_html')); })
3. 手动创建软链接
若上述配置生效后,php artisan storage:link仍无法正确创建链接,可手动创建软链接:
Linux/macOS
ln -s 项目根目录路径/storage/app/public_html 项目根目录路径/public_html/storage
Windows(命令提示符)
mklink /D "项目根目录路径\public_html\storage" "项目根目录路径\storage\app\public_html"
Windows(PowerShell)
New-Item -ItemType SymbolicLink -Path "项目根目录路径\public_html\storage" -Target "项目根目录路径\storage\app\public_html"
4. 额外检查项
- 确认
config/filesystems.php中links配置项的public_path('storage')已指向public_html/storage - 确保服务器用户对
public_html和storage目录拥有读写权限
内容的提问来源于stack exchange,提问作者Puka
相关产品推荐
相关产品推荐

