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

Ubuntu20.04安装Magento2.4访问出现404 NOT FOUND问题求助

Magento 2.4 Ubuntu 20.04 访问404问题排查方案

1. 修正Apache虚拟主机配置

Magento 2.4强制要求网站根目录指向pub文件夹,否则会出现路由失效或404问题。打开你的配置文件/etc/apache2/sites-available/magento2.conf,确保配置符合以下要求:

<VirtualHost *:80>
    ServerName 127.0.0.1
    DocumentRoot /var/www/html/magento2/pub  # 必须指向pub目录

    <Directory /var/www/html/magento2/pub>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All  # 开启.htaccess重写支持
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/magento2_error.log
    CustomLog ${APACHE_LOG_DIR}/magento2_access.log combined
</VirtualHost>

2. 启用Apache重写模块

Magento依赖URL重写规则实现路由,必须启用rewrite模块:

sudo a2enmod rewrite
sudo systemctl restart apache2

3. 修复文件权限

确保web服务器用户(www-data)拥有Magento目录的正确权限:

# 设置目录属主
sudo chown -R www-data:www-data /var/www/html/magento2
# 设置文件和目录权限
sudo find /var/www/html/magento2 -type f -exec chmod 644 {} \;
sudo find /var/www/html/magento2 -type d -exec chmod 755 {} \;
# 赋予关键目录写权限
sudo chmod -R g+w /var/www/html/magento2/{pub,var,generated,app/etc}

4. 验证虚拟主机配置

  • 启用你的Magento站点配置:
    sudo a2ensite magento2.conf
    
  • 禁用默认站点避免冲突:
    sudo a2dissite 000-default.conf
    
  • 检查Apache配置语法:
    sudo apache2ctl configtest
    
  • 重启Apache生效:
    sudo systemctl restart apache2
    

5. 确认Magento Base URL配置

检查当前Base URL是否与访问地址一致:

bin/magento config:show web/unsecure/base_url

如果输出不是http://127.0.0.1/,执行以下命令修改:

bin/magento config:set web/unsecure/base_url http://127.0.0.1/
bin/magento cache:flush

6. 检查pub目录下的.htaccess文件

确保/var/www/html/magento2/pub/.htaccess存在,若丢失可通过部署静态内容生成:

bin/magento setup:static-content:deploy -f

同时设置该文件权限:

sudo chmod 644 /var/www/html/magento2/pub/.htaccess
sudo chown www-data:www-data /var/www/html/magento2/pub/.htaccess

内容的提问来源于stack exchange,提问作者adnen manssouri

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 22:27:30