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

MacOS Ventura下Apache VHost配置正常但无法访问问题

问题解决:Apache虚拟主机404及默认主机日志异常

一、解决访问localhost日志写入虚拟主机文件的问题

Apache会将第一个定义的VirtualHost作为默认主机,当访问localhost没有匹配到对应虚拟主机时,会自动使用第一个虚拟主机配置,导致日志写入parenting的日志文件。解决方法是添加localhost对应的默认虚拟主机,并将其放在所有虚拟主机配置的最前面:

编辑/usr/local/etc/httpd/extra/httpd-vhosts.conf,在现有配置前添加:

<VirtualHost *:80>
    ServerName localhost
    # 替换为你原来的默认网站根目录,比如/usr/local/var/www或你的个人Sites目录
    DocumentRoot "/usr/local/var/www"
    <Directory "/usr/local/var/www">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    ErrorLog "/usr/local/var/log/httpd/localhost-error_log"
    CustomLog "/usr/local/var/log/httpd/localhost-access_log" combined
</VirtualHost>

二、解决parenting.localhost访问404错误

按以下步骤逐一排查:

1. 验证文档根目录及文件存在性

确认目标目录和index.php文件存在:

ls -l /Users/joshteam/PhpstormProjects/parenting/public/index.php

如果文件不存在,检查项目文件是否正确部署到该目录。

2. 配置DirectoryIndex优先加载index.php

在httpd.conf或当前VirtualHost的<Directory>块中添加:

DirectoryIndex index.php index.html

确保Apache查找目录首页时优先尝试index.php。

3. 修复目录权限

Apache以_www用户运行,需要赋予该用户访问项目目录的权限:

# 递归设置目录读写权限
sudo chmod -R 755 /Users/joshteam/PhpstormProjects/parenting
# 将_www用户加入你的用户组(替换joshteam为你的实际用户组)
sudo dseditgroup -o edit -a _www -t user joshteam

4. 确认PHP模块已启用

检查httpd.conf中是否存在PHP模块加载配置:

LoadModule php_module /usr/local/opt/php/lib/httpd/modules/libphp.so
AddHandler application/x-httpd-php .php

如果没有,添加上述配置(路径根据你安装的PHP版本调整)。

5. 重启Apache并验证配置

# 验证配置语法
httpd -t
# 重启服务
brew services restart httpd
# 查看虚拟主机加载情况
httpd -t -D DUMP_VHOSTS

输出应显示localhost和parenting.localhost两个虚拟主机,其中localhost为默认主机。

完成以上步骤后,重新访问http://parenting.localhost即可正常加载index.php,访问http://localhost的日志也会写入对应默认主机的日志文件。

内容的提问来源于stack exchange,提问作者Josh Team

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 22:45:38