Laravel Statamic线上环境图片加载失败求助(本地正常)
DigitalOcean应用平台上Laravel Statamic图片无法加载的排查方案
问题背景
部署在DigitalOcean应用平台的Laravel Statamic应用出现图片无法加载,但alt文本正常显示的问题,本地环境下图片完全正常。已尝试多种常见修复方案但均无效,怀疑问题出在符号链接配置上,寻求技术建议。
已尝试的修复操作
- 确认所有资源磁盘已配置URL并设为公开
- 执行过
storage:link命令 - 通过
user.ini提升PHP内存限制并验证生效 - 在DigitalOcean中配置了URL、APP_KEY等环境变量
- 尚未尝试删除public storage文件夹(不清楚线上控制台操作方法)
当前Filesystems.php配置
return [ /* |-------------------------------------------------------------------------- | Default Filesystem Disk |-------------------------------------------------------------------------- | | Here you may specify the default filesystem disk that should be used | by the framework. The "local" disk, as well as a variety of cloud | based disks are available to your application. Just store away! | */ 'default' => env('FILESYSTEM_DISK', 'images'), 'cloud' => env('FILESYSTEM_CLOUD', 's3'), /* |-------------------------------------------------------------------------- | Filesystem Disks |-------------------------------------------------------------------------- | | Here you may configure as many filesystem "disks" as you wish, and you | may even configure multiple disks of the same driver. Defaults have | been set up for each driver as an example of the required values. | | Supported Drivers: "local", "ftp", "sftp", "s3" | */ 'disks' => [ 'local' => [ 'driver' => 'local', 'root' => storage_path('app'), 'throw' => false, 'url' => '/images', 'visibility' => 'public', ], 'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'url' => env('APP_URL') . '/storage', 'visibility' => 'public', 'throw' => false, ], 's3' => [ 'driver' => 's3', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION'), 'bucket' => env('AWS_BUCKET'), 'url' => env('AWS_URL'), 'endpoint' => env('AWS_ENDPOINT'), 'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false), 'throw' => false, ], 'images' => [ 'driver' => 'local', 'root' => public_path('images'), 'url' => '/images', 'visibility' => 'public', ], 'favicons' => [ 'driver' => 'local', 'root' => public_path('favicons'), 'url' => '/favicons', 'visibility' => 'public', ], 'files' => [ 'driver' => 'local', 'root' => public_path('files'), 'url' => '/files', 'visibility' => 'public', ], 'social_images' => [ 'driver' => 'local', 'root' => public_path('social_images'), 'url' => '/social_images', 'visibility' => 'public', ], ], /* |-------------------------------------------------------------------------- | Symbolic Links |-------------------------------------------------------------------------- | | Here you may configure the symbolic links that will be created when the | `storage:link` Artisan command is executed. The array keys should be | the locations of the links and the values should be their targets. | */ 'links' => [ public_path('storage') => storage_path('app/public'), ], ];
针对性排查与修复建议
1. 确认DigitalOcean部署时符号链接是否正确生成
DigitalOcean应用平台的容器化部署环境中,storage:link命令可能不会自动执行。可以通过两种方式确保符号链接生成:
- 在项目的
package.json中添加postinstall脚本:"scripts": { "postinstall": "php artisan storage:link" } - 在DigitalOcean应用平台的部署配置中,添加构建命令:
php artisan storage:link
2. 检查public/images目录的存在与权限
你的默认磁盘是images,其根目录指向public_path('images'),需确认线上环境中该目录存在且权限正确:
- 执行命令查看目录状态:
ls -la public/ - 修正权限(若需要):
chown -R www-data:www-data public/images
3. 修正local磁盘的URL配置冲突
当前local磁盘的url设为/images,但它的根目录是storage_path('app'),这会和images磁盘的路径产生混淆。建议移除或修正该配置:
'local' => [ 'driver' => 'local', 'root' => storage_path('app'), 'throw' => false, // 移除错误的url配置,避免路径冲突 // 'url' => '/images', 'visibility' => 'public', ],
4. 验证图片实际存储路径与磁盘配置匹配
如果图片实际存储在storage/app/public目录下,应该使用public磁盘而非images磁盘;若图片确实在public/images下,需确认文件是否已正确部署到线上环境。
5. 清除应用缓存确保配置生效
线上环境执行以下命令刷新配置:
php artisan config:clear php artisan cache:clear
内容的提问来源于stack exchange,提问作者Luke
相关产品推荐
相关产品推荐

