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

Apache虚拟主机配置失效求助:本地多站点无法访问

问题排查请求

背景

之前用Apache单站点运行正常,最近要在本地新增两个站点,参考相关指南和Stack Overflow内容配置后,访问/etc/hosts和httpd-vhosts.conf里配置的三个主机时,浏览器提示its-site.test refused to connect.,自行排查后未解决,本人是Apache新手,可能遗漏了明显问题。

预期配置

开发时通过命名虚拟主机访问三个本地站点:

  • its-site.test
  • its-cab.test
  • its-gallery.test

Hosts文件内容

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost

127.0.0.1       its-site.test
127.0.0.1       its-cab.test
127.0.0.1       its-gallery.test

httpd配置内容

/opt/homebrew/etc/httpd/httpd.conf:

Listen 80

Include /opt/homebrew/etc/httpd/extra/httpd-vhosts.conf
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
ServerName localhost:80

# Also enabled PHP but not including the config here as it may not be relevant

虚拟主机配置内容

/opt/homebrew/etc/httpd/extra/httpd-vhosts.conf:

<VirtualHost *:80>
    ServerName its-site.test
    ServerAlias *.its-site.test
    DocumentRoot "/Users/ciesinsg/Documents/Repositories/its-site/web"

    <directory "/Users/ciesinsg/Documents/Repositories/its-site/web">
        Require all granted
        AllowOverride All
    </directory>
</VirtualHost>


<VirtualHost *:80>
    ServerName its-cab.test
    ServerAlias *.its-cab.test
    DocumentRoot "/Users/ciesinsg/Documents/Repositories/its-cab/web"

    <directory "/Users/ciesinsg/Documents/Repositories/its-cab/web">
        Require all granted
        AllowOverride All
    </directory>
</VirtualHost>


<VirtualHost *:80>
    ServerName its-gallery.test
    ServerAlias *.its-gallery.test
    DocumentRoot "/Users/ciesinsg/Documents/Repositories/its-gallery/web"

    <directory "/Users/ciesinsg/Documents/Repositories/its-gallery/web">
        Require all granted
        AllowOverride All
    </directory>
</VirtualHost>

补充信息

  • M1 Macbook Air
  • 通过Homebrew安装Apache和PHP
  • 站点指向Drupal项目的web目录

此前正常的单站点配置

之前仅配置httpd.conf,未修改/etc/hosts和httpd-vhosts.conf,配置内容:

Listen 80
DocumentRoot "/Users/ciesinsg/Documents/Repositories/its-gallery/web"
<Directory "/Users/ciesinsg/Documents/Repositories/its-gallery/web">

# Also enabled PHP

配置虚拟主机时已移除上述DocumentRoot和<Directory>配置。


排查方案与解决办法

1. 检查Apache服务状态与端口占用

  • 确认Apache是否正常启动:执行brew services list,查看httpd状态是否为started。未启动则执行brew services start httpd;已启动则重启服务:brew services restart httpd。
  • 检查80端口是否被占用:执行sudo lsof -i :80,若有其他进程占用,要么停止该进程,要么修改Apache监听端口——把httpd.conf里的Listen 80改成Listen 8080,同时虚拟主机里的<VirtualHost *:80>改成<VirtualHost *:8080>,访问时需加端口号(如its-site.test:8080)。

2. 验证虚拟主机配置语法

执行httpd -t检查配置文件语法,若有错误会给出具体提示(比如路径错误、标签未闭合等)。

3. 启用虚拟主机模块

在httpd.conf里检查是否存在LoadModule vhost_alias_module lib/httpd/modules/mod_vhost_alias.so,如果没有,添加该行后重启Apache。Homebrew安装的Apache默认可能未启用该模块,这是虚拟主机生效的关键。

4. 修复目录权限

Drupal的web目录需要Apache具备读取和执行权限,执行以下命令:

sudo chmod -R 755 /Users/ciesinsg/Documents/Repositories/
sudo chown -R _www:_www /Users/ciesinsg/Documents/Repositories/

(Homebrew安装的Apache默认运行用户为_www,需确保目录归属正确)

5. 确认本地解析生效

执行ping its-site.test,查看是否返回127.0.0.1。若未返回,刷新DNS缓存:sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder(适用于M1 Mac)。

6. 清除浏览器缓存并强制访问

浏览器可能缓存了旧连接信息,尝试用隐私窗口访问,或按Ctrl+Shift+R强制刷新。另外,确保访问时使用http://its-site.test而非https://,因为未配置SSL,HTTPS会导致连接失败。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 10:48:13