如何在流浪客中连接到基于名称的虚拟主机



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

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

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

127.0.0.1 my.site1
127.0.0.1 my.site2

由于Vagrant的端口映射,访客只能作为my.site1:port访问,例如my.site:3000。这样一来,访客内部的Apache就把我带到了根目录(Apache的欢迎站点)。这对于两个vhost: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)

这是因为vhosts配置不正确,还是我在名称/端口或主机/来宾配置中遗漏了一些点?

我说得对吗?

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,所以我将192.168.23.46分配给了流浪客

一旦完成了这项工作,也许只需使用此shell脚本为自己创建虚拟主机就可以省去所有麻烦。我已经粘贴了输出,您可以在下面查看我已经用mysite1和mysite2名称设置了两个虚拟主机。

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

192.168.23.46 mysite1 192.168.23.46 mysite2

使用http://mysite1和http://mysite2.您可能想更改脚本放置在相应文档根下的index.php的内容,这样您就可以确保请求由正确的虚拟主机处理,因为此脚本只处理apache的默认index.php,它将在您的两个文档根下找到。

另一种选择是在公共网络上提供Vagrant盒子,然后使用公共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

如果你需要更多的澄清,请告诉我。

相关内容

最新更新