Laravel生产环境(虚拟主机)中Storage软链接失效问题求助
解决Laravel 10在Siteground主机上Storage图片无法访问的问题
1. 确认软链接路径正确性
你的目录结构大致如下:
public_html/ ├── laravel-core/ # 存放Laravel除public外的核心文件 │ └── storage/ │ └── app/ │ └── public/ # 上传的图片存储目录 ├── storage/ # 你创建的软链接 └── [原Laravel public目录下的文件,如index.php等]
Siteground环境下必须用绝对路径创建软链接,相对路径可能无法被正确解析。执行以下SSH命令:
# 删除已存在的无效软链接 rm public_html/storage # 用绝对路径重新创建软链接(替换你的用户名) ln -s /home/你的用户名/public_html/laravel-core/storage/app/public /home/你的用户名/public_html/storage
可以通过
pwd命令查看当前用户的根目录路径,替换命令中的你的用户名。
2. 修复Storage目录权限
Siteground对文件权限有严格要求,执行以下命令确保权限合规:
# 递归设置storage目录权限为755 chmod -R 755 /home/你的用户名/public_html/laravel-core/storage # 递归设置图片文件权限为644 chmod -R 644 /home/你的用户名/public_html/laravel-core/storage/app/public/*
同时确认storage目录的所有者与网站运行用户一致(通常和FTP用户名相同):
# 查看当前目录所有者 ls -l /home/你的用户名/public_html/laravel-core/storage # 不一致则修改所有者 chown -R 你的用户名:你的用户名 /home/你的用户名/public_html/laravel-core/storage
3. 修正index.php的路径引用
由于Laravel核心放在public_html的子文件夹中,需要修改public_html/index.php的路径指向:
打开文件后找到以下两行:
require __DIR__.'/../vendor/autoload.php'; $app = require_once __DIR__.'/../bootstrap/app.php';
修改为:
require __DIR__.'/laravel-core/vendor/autoload.php'; $app = require_once __DIR__.'/laravel-core/bootstrap/app.php';
4. 重置配置并清除缓存
保持.env配置简洁正确,无需额外设置APP_STORAGE_PATH:
APP_URL=https://你的域名.com FILESYSTEM_DISK=public
进入Laravel核心目录执行缓存清理命令:
cd /home/你的用户名/public_html/laravel-core php artisan cache:clear php artisan config:clear php artisan storage:link
执行
php artisan storage:link会自动生成符合Laravel规范的软链接,比手动创建更可靠。
5. 检查Siteground的安全与缓存设置
- 登录Siteground后台,进入
Security > ModSecurity,临时关闭规则测试是否能访问图片; - 进入
Speed > Caching,清除网站缓存和CDN缓存,避免旧链接被缓存。
内容的提问来源于stack exchange,提问作者naddiel sanchez
相关产品推荐
相关产品推荐

