我在 macOS Ventura 13.7 上设置了多个虚拟主机。设置这些虚拟主机已经有几年了,现在我需要启动并开发一个新网站。要么是我的系统配置不正确,要么是我忘记了步骤。我希望能够在本地机器上打开并运行我的任何网站(html 或 php)。

配置

/etc/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   mysite1.com
127.0.0.1   mysite2.com
127.0.0.1   mysite3.com
127.0.0.1   mysite4.com
127.0.0.1   mysite5.cc
127.0.0.1   mysite6.com
127.0.0.1   mysite7.com
127.0.0.1   kubernetes.docker.internal
# kubernetes added by Docker Desktop
# To allow the same kube context to work on the host and the container:
# End of section

/etc/apache2/httpd.conf

# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf
<IfDefine SERVER_APP_HAS_DEFAULT_PORTS>
    Listen 8080
</IfDefine>
<IfDefine !SERVER_APP_HAS_DEFAULT_PORTS>
    Listen 80
</IfDefine>
ServerName localhost
<Directory />
    AllowOverride all
    Require all denied
</Directory>
DocumentRoot "/Volumes/HD/Web/websites"
<Directory "/Volumes/HD/Web/websites">
    Options Indexes FollowSymLinks Multiviews
    MultiviewsMatch Any
    AllowOverride All
    Require all granted
</Directory>

/etc/apache2/extra/httpd-vhosts.conf

<VirtualHost *:80>
    ServerAdmin webmaster@mysite1.com
    DocumentRoot "/Volumes/HD/Web/websites/mysite1"
    ServerName localmysite1.com
    ServerAlias www.localmysite1.com
    ErrorLog "/Volumes/HD/Web/websites/apache_logs/localmysite1/logs/error_log"
    CustomLog "/Volumes/HD/Web/websites/apache_logs/localmysite1/logs/access_log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@mysite2.com
    DocumentRoot "/Volumes/HD/Web/websites/mysite2"
    ServerName localmysite2.com
    ServerAlias www.localmysite2.com
    ErrorLog "/Volumes/HD/Web/websites/apache_logs/localmysite2/logs/error_log"
    CustomLog "/Volumes/HD/Web/websites/apache_logs/localmysite2/logs/access_log" common
</VirtualHost>
...

问题

mysite5.cc显示位于 的 html 页面/Volumes/HD/Web/websites。其运行方式与我将浏览器指向127.0.0.1或 时一样localhost:80。我不明白为什么此网站的行为有所不同。

我能够在 Chrome 中打开其他每个网站一次。关闭选项卡后,即使重新启动 Apache,我也无法再访问任何网站。我看到此错误

This site can’t be reached
mysite1.com refused to connect.
Try:
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_REFUSED

当我将浏览器指向 时,也会遇到同样的错误localhost:8080

故障排除

重新启动 Apache 没有帮助。重新启动计算机也没有帮助。

apachectl configtest返回Syntax OK

我尝试使用以下方法刷新 DNS 缓存
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

我尝试更改Listen 80Listen 8080,反之亦然。有趣的是,这并没有改变我在浏览器中看到的内容。localhost:80 始终显示 DocumentRoot 中的页面。我可以在访问日志中看到每个站点的初始视图。没有记录任何错误。

与此处的类似帖子相比,我尝试了这个:

↪ curl -I -L 127.0.0.1 
HTTP/1.1 200 OK
Date: Fri, 25 Oct 2024 06:46:39 GMT
Server: Apache/2.4.59 (Unix) PHP/8.3.4
Last-Modified: Sat, 10 Feb 2024 21:51:14 GMT
ETag: "22-6110e0b1e1880"
Accept-Ranges: bytes
Content-Length: 34

nslookup mysite1.com

** server can't find mysite1.com: NXDOMAIN

httpd -v

Server version: Apache/2.4.59 (Unix)
Server built:   Apr  3 2024 12:22:45

which httpd

/usr/local/bin/httpd

php -v

PHP 8.3.4 (cli) (built: Mar 12 2024 23:42:26) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.4, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.4, Copyright (c), by Zend Technologies

which php

/usr/local/opt/php/bin/php

我用来sudo apachectl start启动 apache。

以上内容让我认为我安装了另一个 apache 实例。事实证明确实如此。我一定是在某个时候从 Homebrew 安装了 Apache。在 中我找到了/usr/local/etc/httpd的副本。我没有看到该文件的 brew 版本。httpd.conf/extra/httpd-vhosts.confhosts

文件比较显示 Homebrew 版本包含httpd.confListen 127.0.0.1:80我将其更改为Listen *:80。现在,当我访问 时mysite5.cc,php 文件会下载到我的桌​​面。至少这比显示错误的页面要好。我需要记住如何启动 php…

奇怪的是,当我访问其他网站时,我仍然看到连接错误。所以我仍然有一些配置问题。

2

  • Hosts 条目不是 DNS 记录,因此使用 nslookup 和 dig 等工具不是调试问题的正确方法。此外,网站/域可能设置了强制安全访问的安全标头,例如 HSTS 和/或永久重定向到 https ,这两者都由您的浏览器缓存。然后,您的浏览器将拒绝通过明文 http 进行连接,您应该设置 TLS 证书或从新的匿名窗口进行测试


    – 


  • 感谢您的建议。我创建了一个用于本地开发的 SSL 证书(按照以下步骤)。不幸的是,我仍然在 Chrome 中看到 ERR_CONNECTION_REFUSED 错误消息。遗憾的是,在隐身窗口中打开页面也无济于事。顺便问一下,您知道我是否需要为每个开发站点创建单独的证书吗?我想我可能只需将每个 VirtualHosts 指向同一个证书。


    – 

0