You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 20.04 Apache环境下Laravel站点子目录访问不存在问题

问题原因

你遇到的非首页路径404错误是Laravel路由重写规则未生效导致的,根源是Apache未启用rewrite模块,且虚拟主机未配置允许.htaccess覆盖规则。

解决步骤
  • 第一步:启用Apache rewrite模块
    执行命令:sudo a2enmod rewrite
  • 第二步:修改虚拟主机配置文件
    编辑/etc/apache2/sites-available/test.loc.conf,添加Directory配置段,完整配置如下:
<VirtualHost *:80>
ServerName test.loc
ServerAlias www.test.loc
ServerAdmin webmaster@test.loc
DocumentRoot /var/www/html/test.loc/public

<Directory /var/www/html/test.loc/public>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined 
</VirtualHost>
  • 第三步:确认public目录下.htaccess文件存在且权限正常
    不要删除Laravel项目自带的/var/www/html/test.loc/public/.htaccess文件,确保项目目录权限配置正确,可执行命令调整:
    sudo chown -R www-data:www-data /var/www/html/test.loc
    sudo chmod -R 755 /var/www/html/test.loc/storage /var/www/html/test.loc/bootstrap/cache
  • 第四步:重启Apache使配置生效
    执行命令:sudo systemctl restart apache2

报错参考截图

404报错截图

内容的提问来源于stack exchange,提问作者Abo Hovhannisyan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 05:00:03