Laravel存储符号链接出现Access Denied: 403 Error问题求助
Laravel 存储文件访问403 Forbidden 问题排查方案
问题详情
错误信息:
Forbidden
You don't have permission to access this resource.Server unable to read htaccess file, denying access to be safe
- 主目录:
/home/admin/domains/{mydomain.com} - Laravel源目录:
/home/admin/domains/{mydomain.com}/source - 网站根目录存储符号链接:
/home/admin/domains/{mydomain.com}/public_html/storage - 实际存储文件路径:
/home/admin/domains/{mydomain.com}/source/storage/app/public - 异常表现:Laravel后台写入文件正常,前端访问文件返回403权限错误
排查修复步骤
1. 优先解决.htaccess读取问题
错误提示核心是Web服务器无法读取.htaccess,先处理这个:
- 确认网站根目录(
public_html)下存在.htaccess,若缺失,从Laravel源目录的public子目录复制一份 - 设置
.htaccess权限为644(Web服务器可读,所有者可编辑):chmod 644 /home/admin/domains/{mydomain.com}/public_html/.htaccess - 确保文件所属用户组与Web服务器一致(常见为
www-data或apache,根据服务器环境调整):chown admin:www-data /home/admin/domains/{mydomain.com}/public_html/.htaccess
2. 验证存储符号链接有效性
检查符号链接是否正确指向目标路径,且权限正常:
- 查看符号链接状态:
输出应明确显示指向ls -l /home/admin/domains/{mydomain.com}/public_html/storage/home/admin/domains/{mydomain.com}/source/storage/app/public - 若链接无效,重新创建:
rm /home/admin/domains/{mydomain.com}/public_html/storage ln -s /home/admin/domains/{mydomain.com}/source/storage/app/public /home/admin/domains/{mydomain.com}/public_html/storage - 给存储目录递归设置
755权限(确保Web服务器可读取目录内文件):chmod -R 755 /home/admin/domains/{mydomain.com}/source/storage - 同步目录所属用户组:
chown -R admin:www-data /home/admin/domains/{mydomain.com}/source/storage
3. 调整Web服务器配置(支持符号链接)
- Apache环境:确保网站配置中启用
FollowSymLinks和AllowOverride All:
在虚拟主机配置文件中添加或确认:
重启Apache生效:<Directory /home/admin/domains/{mydomain.com}/public_html> AllowOverride All Options FollowSymLinks Require all granted </Directory>service apache2 restart - Nginx环境:在server块中明确配置存储路径的访问规则:
重启Nginx生效:location /storage { alias /home/admin/domains/{mydomain.com}/source/storage/app/public; autoindex off; allow all; }service nginx restart
4. 验证Laravel存储配置
确保config/filesystems.php中public磁盘配置正确:
'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'url' => env('APP_URL').'/storage', 'visibility' => 'public', ],
重新生成存储链接(在Laravel源目录执行):
cd /home/admin/domains/{mydomain.com}/source php artisan storage:link
内容的提问来源于stack exchange,提问作者D .MOTAHARI
相关产品推荐
相关产品推荐

