如何在无业游民的访客中连接到基于名称的虚拟主机? [英] How to connect to name-based virtualhosts in vagrant guest?

查看:88
本文介绍了如何在无业游民的访客中连接到基于名称的虚拟主机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Inside the box, I have two virtual hosts:

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

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

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

How can I connect to the vhosts inside the guest from the host? In my (host) /etc/hosts I have:

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.

Because of the Vagrant's port mapping, the guest is accessible only as my.site1:port, e.g., my.site:3000. With that, the Apache inside the guest takes me just to the root (the Apache's welcome site). It is the same for both vhosts: my.site1:3000 and my.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)

是因为虚拟主机配置不正确还是我在名称/端口或主机/来宾配置中缺少某些地方?

Is that because of improper configuration of vhosts or am I missing some point in the name/port or host/guest configuration?

我对吗,应该这样吗?

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分配给了Vagrant客人.

Okay, so the problem here is the networking. I was able to reproduce this problem. First of all, please disable the port forwarding in Vagrant. Just comment config.vm.network "forwarded_port", guest: 80, host: 8070 and do a vagrant reload .To get this working, you need to check your host's IP address and then go to your Vagrant file and edit config.vm.network "private_network", ip: "X.X.X.X" so that the ip address here is actually on the same network as your host. What I did is just incremented the last octet by 1. e.g. My local IP address is 192.168.23.45 so I assigned 192.168.23.46 to the Vagrant guest.

完成此操作后,也许只需使用 Shell脚本为您自己创建虚拟主机.我已将输出粘贴到下面,您可以查看下面的内容,以设置我的两个虚拟主机的名称分别为mysite1和mysite2.

Once this is done, perhaps, you can save yourself all the trouble by just using this shell script to create Virtual hosts for yourself. I have pasted the output below which you can go through to see that I have setup my two virtual hosts with mysite1 and mysite2 names.

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

Then just put the host file entries on your host like below:

192.168.23.46 mysite1 192.168.23.46 mysite2

192.168.23.46 mysite1 192.168.23.46 mysite2

并使用 http://mysite1

And accessing the website using http://mysite1 and http://mysite2. You might want to change the content of the index.php placed by script under the respective document roots so that you can be sure that the requests are being handled by correct virtual hosts since this scripts just deals with the default index.php of apache which will be found under both your document roots.

另一个选择是在公共网络上使无业游民"框可用,然后 使用公共IP对其进行访问,为此,您将必须在Vagrant文​​件中启用config.vm.network "public_network",并且创建虚拟主机的其余过程是相同的(使用此脚本).

The other option is to make the Vagrant box available on public network and then access it using the public IP and for that, you will have to enable config.vm.network "public_network" in your Vagrant file and the rest of the process of creating the Virtual host is the same (Using this script).

[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

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

Please let me know in case you need more clarification.

这篇关于如何在无业游民的访客中连接到基于名称的虚拟主机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆