本地 apache 无法解析虚拟主机
Posted
技术标签:
【中文标题】本地 apache 无法解析虚拟主机【英文标题】:Local apache cannot resolve vhosts 【发布时间】:2018-11-27 03:34:09 【问题描述】:我已经在我的 Mac (macOS 10.13.5) 上使用 Homebrew 设置了本地 Apache (2.4.33)。服务器正在运行,配置文件的语法检查返回“语法正常”。 我在 httpd-vhosts.conf 文件 (/usr/local/etc/httpd/extra/httpd-vhosts.conf) 中为我的 DEV 环境定义了几个虚拟主机,并检查了该文件是否实际加载:
httpd -D DUMP_INCLUDES
Included configuration files:
(*) /usr/local/etc/httpd/httpd.conf
(542) /usr/local/etc/httpd/extra/httpd-vhosts.conf
我又做了一次检查以确保 Vhost 配置是否也正常:
httpd -S
VirtualHost configuration:
*:80 is a NameVirtualHost
default server localhost (/usr/local/etc/httpd/extra/httpd-vhosts.conf:40)
port 80 namevhost localhost (/usr/local/etc/httpd/extra/httpd-vhosts.conf:40)
port 80 namevhost spielwiese.local (/usr/local/etc/httpd/extra/httpd-vhosts.conf:45)
port 80 namevhost phpmyadmin.local (/usr/local/etc/httpd/extra/httpd-vhosts.conf:77)
port 80 namevhost relaunch.domain1.local (/usr/local/etc/httpd/extra/httpd-vhosts.conf:85)
port 80 namevhost dev.domain2.local (/usr/local/etc/httpd/extra/httpd-vhosts.conf:93)
port 80 namevhost test.domain2.local (/usr/local/etc/httpd/extra/httpd-vhosts.conf:101)
到目前为止,这看起来很有希望。奇怪的是,除了两个(dev.domain2.local 和 test.domain2.local)之外的所有 vhost 都可以解决,但我在配置中找不到错误:
<VirtualHost *:80>
DocumentRoot "/Users/username/Sites/phpmyadmin"
ServerName phpmyadmin.local
ErrorLog "/private/var/log/apache2/phpmyadmin.local-error_log"
CustomLog "/private/var/log/apache2/phpmyadmin.local-access_log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Users/username/owncloud/somefolder/www/relaunch.domain1.local/public_html"
ServerName relaunch.domain1.local
ErrorLog "/private/var/log/apache2/relaunch.domain1.local-error_log"
CustomLog "/private/var/log/apache2/relaunch.domain1.local-access_log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Users/username/Sites/dev.domain2.local/web"
ServerName dev.domain2.local
ErrorLog "/private/var/log/apache2/dev.domain2.local-error_log"
CustomLog "/private/var/log/apache2/dev.domain2.local-access_log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Users/username/Sites/test.domain2.local"
ServerName test.domain2.local
ErrorLog "/private/var/log/apache2/test.domain2.local-error_log"
CustomLog "/private/var/log/apache2/test.domain2.local-access_log" common
</VirtualHost>
我已经多次停止、启动和重新启动 apache,但总是得到相同的结果:
$ ping phpmyadmin.local
PING phpmyadmin.local (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.040 ms
$ ping test.domain2.local
ping: cannot resolve test.domain2.local: Unknown host
$ ping dev.domain2.local
ping: cannot resolve dev.domain2.local: Unknown host
任何想法或提示如何解决这个问题?
【问题讨论】:
您是否在/etc/hosts
文件中将这两个域指向了 127.0.0.1?
感谢您的提示,@dusan-bajic - 这解决了我的问题!我没有找到 hosts 文件,因为我在 usr/local/etc 中查找配置文件(httpd.conf、httpd-vhosts.conf)所在的文件夹。
【参考方案1】:
问题已解决 - 感谢@dusan-bajic!将这两个域添加到 /etc/hosts 文件后,一切都按预期工作。
【讨论】:
以上是关于本地 apache 无法解析虚拟主机的主要内容,如果未能解决你的问题,请参考以下文章
Apache vhost 解析为 localhost 文档根目录