Magento 2本地安装成功后访问出现404 Error Not found问题求助
Magento 2 本地MAMP环境404错误排查方案
针对你遇到的访问http://localhost/M241出现404的问题,可按以下步骤逐一排查:
1. 启用Apache重写模块
- 打开MAMP的
httpd.conf文件(路径:MAMP → Preferences → Apache → Open Conf File) - 找到
#LoadModule rewrite_module modules/mod_rewrite.so,删除行首的#号以启用模块 - 保存文件后重启MAMP的Apache服务
2. 确认.htaccess文件完整性
- 检查Magento根目录和
pub目录下是否存在.htaccess文件(安装时默认生成) - 若
pub目录的.htaccess缺失,从Magento安装包的pub文件夹中复制一份补全
3. 修正数据库Base URL配置
- 登录数据库,确保
config_core_data表中以下两条记录的value配置正确(注意末尾斜杠):web/unsecure/base_url:http://localhost/M241/web/secure/base_url:http://localhost/M241/(本地环境可暂用HTTP)
- 修改后执行缓存清理:
sudo php bin/magento cache:flush
4. 配置MAMP虚拟主机(关键)
由于你将网站根目录指向pub文件夹,需通过虚拟主机避免路径冲突:
- 打开MAMP的
httpd-vhosts.conf文件(路径:MAMP → Preferences → Apache → Open Conf File → 找到Include conf/extra/httpd-vhosts.conf并打开该文件) - 添加以下配置(替换
<你的Magento安装路径>/pub为实际路径):<VirtualHost *:80> DocumentRoot "<你的Magento安装路径>/pub" ServerName localhost/M241 <Directory "<你的Magento安装路径>/pub"> AllowOverride All Require all granted </Directory> </VirtualHost> - 保存后重启Apache服务
5. 修正文件权限(避免安全风险)
不要使用777权限,改用Magento官方推荐的权限设置:
sudo chown -R $USER:_www . sudo find . -type f -exec chmod 644 {} \; sudo find . -type d -exec chmod 755 {} \; sudo chmod -R 777 var/ generated/ pub/static/ pub/media/
(MAMP的Web服务器用户通常为_www)
6. 验证静态文件访问
尝试访问http://localhost/M241/robots.txt:
- 若能访问,说明路径配置基本正常,问题可能出在Magento路由重写,可重新执行
setup:upgrade并清理缓存 - 若无法访问,说明MAMP根目录指向或虚拟主机配置有误,需重新核对
内容的提问来源于stack exchange,提问作者Olude david
相关产品推荐
相关产品推荐

