如何连接到 vagrant guest 中基于名称的虚拟主机?

How to connect to name-based virtualhosts in vagrant guest?

在盒子里面,我有两个虚拟主机:

<VirtualHost *:80>
    HostName my.site1
    ...
</Virtualhost *:80>

<VirtualHost *:80>
    HostName my.site2
    ...
</VirtualHost>

如何从主机连接到来宾内部的虚拟主机?在我的(主机)/etc/hosts 我有:

127.0.0.1 my.site1
127.0.0.1 my.site2

由于 Vagrant 的端口映射,来宾只能作为 my.site1:port 访问,例如 my.site:3000。这样,来宾中的 Apache 将我带到根目录(Apache 的欢迎站点)。两个虚拟主机都是一样的:my.site1:3000my.site2:3000.

apachectl -S 日志:

VirtualHost configuration:
*:80     is a NameVirtualHost
         default server stretch.localdomain (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost stretch.localdomain (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost my.site1 (/etc/apache2/sites-enabled/001-site1.conf:1)
         port 80 namevhost my.site2 (/etc/apache2/sites-enabled/002-site2.conf:1)

这是因为虚拟主机配置不当还是我遗漏了 name/port 或 host/guest 配置中的某些点?

我说得对吗?它应该这样工作吗?

my.site1:3000 -> contents of my.site1
my.site2:3000 -> contents of my.site2

好的,所以这里的问题是网络。我能够重现这个问题。 首先,请在Vagrant中禁用端口转发。只需评论 config.vm.network "forwarded_port", guest: 80, host: 8070 并执行 vagrant reload . 要使其正常工作,您需要检查主机的 IP 地址,然后转到您的 Vagrant 文件并编辑 config.vm.network "private_network", ip: "X.X.X.X" 以便这里的ip地址实际上和你的主机在同一个网络上。我所做的只是将最后一个八位字节增加 1。例如我的本地IP地址是192.168.23.45所以我给Vagrant guest分配了192.168.23.46

完成此操作后,也许您可​​以通过使用 this shell 脚本为自己创建虚拟主机来省去所有麻烦。我在下面粘贴了输出,您可以通过它来查看我已经使用 mysite1 和 mysite2 名称设置了我的两个虚拟主机。

然后将主机文件条目放在您的主机上,如下所示:

192.168.23.46 mysite1 192.168.23.46 mysite2

并使用 http://mysite1 and http://mysite2 访问网站。您可能想要更改脚本放置在相应文档根目录下的 index.php 的内容,以便您可以确保请求正在由正确的虚拟主机处理,因为该脚本只处理默认的 index.php 的 apache,将在您的两个文档根目录下找到。

另一种选择是让 Vagrant box 在 public 网络上可用,然后 使用 public IP 访问它,为此,您必须在 Vagrant 文件中启用 config.vm.network "public_network",其余创建虚拟主机的过程是相同的(使用此脚本)。

[root@localhost vagrant]# bash test.sh
Enter the server name your want (without www) : mysite1
Enter a CNAME (e.g. :www or dev for dev.website.com) : mysite1
Enter the path of directory you wanna use (e.g. : /var/www/, dont forget the /): /var/www/mysite1/
Enter the user you wanna use (e.g. : apache) : apache
Enter the listened IP for the server (e.g. : *): *
Web directory created with success !
/etc/httpd/conf.d/mysite1.conf
Virtual host created !
Would you like me to create ssl virtual host [y/n]?
n
Testing configuration
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Syntax OK
Would you like me to restart the server [y/n]?
y
Redirecting to /bin/systemctl restart httpd.service
======================================
All works done! You should be able to see your website at http://mysite1

Share the love! <3
======================================

Wanna contribute to improve this script? Found a bug? https://gist.github.com/mattmezza/2e326ba2f1352a4b42b8
[root@localhost vagrant]# bash test.sh
Enter the server name your want (without www) : mysite2
Enter a CNAME (e.g. :www or dev for dev.website.com) : mysite2
Enter the path of directory you wanna use (e.g. : /var/www/, dont forget the /): /var/www/mysite2/
Enter the user you wanna use (e.g. : apache) : apache
Enter the listened IP for the server (e.g. : *): *
Web directory created with success !
/etc/httpd/conf.d/mysite2.conf
Virtual host created !
Would you like me to create ssl virtual host [y/n]?
n
Testing configuration
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Syntax OK
Would you like me to restart the server [y/n]?
y
Redirecting to /bin/systemctl restart httpd.service
======================================
All works done! You should be able to see your website at http://mysite2

Share the love! <3
======================================

Wanna contribute to improve this script? Found a bug? https://gist.github.com/mattmezza/2e326ba2f1352a4b42b8

如果您需要更多说明,请告诉我。