如何从同一网络上的另一台计算机连接到该本地主机? [英] How do I connect to this localhost from another computer on the same network?

查看:186
本文介绍了如何从同一网络上的另一台计算机连接到该本地主机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究一个项目,我想在家里的两台笔记本电脑上对其进行测试,其中一台笔记本电脑连接到另一台笔记本电脑上的本地主机.我正在使用XAMPP.我该怎么做?

I'm currently working on a project and I would like to test it out on two laptops at home where one laptop connects to the localhost on the other. I am using XAMPP. How do I do this?

推荐答案

那肯定是可能的.在这里,我们将以Apache为例.

That's definitely possible. We'll take a general case with Apache here.

假设您是Symfony2的忠实拥护者,并且您希望通过4台不同的计算机(位于托管您网站的主要主机,以及连接了Mac,Windows和Linux发行版的计算机)访问http://symfony.local/上的symfony网站(无论是否无线)到主计算机.

Let's say you're a big Symfony2 fan and you would like to access your symfony website at http://symfony.local/ from 4 different computers (the main one hosting your website, as well as a Mac, a Windows and a Linux distro connected (wireless or not) to the main computer.

1 设置虚拟主机:

您首先需要在apache httpd-vhosts.conf文件中设置虚拟主机.在 XAMP 上,您可以在以下位置找到此文件:C:\xampp\apache\conf\extra\httpd-vhosts.conf.在 MAMP 上,您可以在以下位置找到此文件:Applications/MAMP/conf/apache/extra/httpd-vhosts.conf. 此步骤在计算机上准备Web服务器以处理symfony.local请求.您需要提供虚拟主机的名称以及网站的根/主文件夹.为此,请在该文件的末尾添加以下行.您需要将DocumentRoot更改为您的主文件夹所在的任何位置.在这里,我已将/Applications/MAMP/htdocs/Symfony/作为我网站的根.

You first need to set up a virtual host in your apache httpd-vhosts.conf file. On XAMP, you can find this file here: C:\xampp\apache\conf\extra\httpd-vhosts.conf. On MAMP, you can find this file here: Applications/MAMP/conf/apache/extra/httpd-vhosts.conf. This step prepares the Web server on your computer for handling symfony.local requests. You need to provide the name of the Virtual Host as well as the root/main folder of your website. To do this, add the following line at the end of that file. You need to change the DocumentRoot to wherever your main folder is. Here I have taken /Applications/MAMP/htdocs/Symfony/ as the root of my website.

<VirtualHost *:80>
    DocumentRoot "/Applications/MAMP/htdocs/Symfony/"
    ServerName symfony.local
</VirtualHost>

2 配置主机文件:

为使客户端(在这种情况下,您是浏览器)理解symfony.local的真正含义,您需要在计算机上编辑主机文件.每次在浏览器中键入URL时,计算机都会尝试理解它的含义! symfony.local对计算机并不意味着任何东西.因此它将尝试将名称symfony.local解析为IP地址.为此,首先检查计算机上的主机文件,看他是否可以将IP地址与您在地址栏中键入的内容相匹配.如果不能,它将询问 DNS服务器.这里的技巧是将以下内容附加到您的hosts文件.

For the client (your browser in that case) to understand what symfony.local really means, you need to edit the hosts file on your computer. Everytime you type an URL in your browser, your computer tries to understand what it means! symfony.local doesn't mean anything for a computer. So it will try to resolve the name symfony.local to an IP address. It will do this by first looking into the hosts file on your computer to see if he can match an IP address to what you typed in the address bar. If it can't, then it will ask DNS servers. The trick here is to append the following to your hosts file.

  • MAC 上,此文件位于/private/etc/hosts中;
  • LINUX 上,此文件位于/etc/hosts中;
  • WINDOWS 上,此文件位于\Windows\system32\private\etc\hosts;
  • WINDOWS 7 上,此文件位于\Windows\system32\drivers\etc\hosts;
  • WINDOWS 10 上,此文件位于\Windows\system32\drivers\etc\hosts;
  • On MAC, this file is in /private/etc/hosts;
  • On LINUX, this file is in /etc/hosts;
  • On WINDOWS, this file is in \Windows\system32\private\etc\hosts;
  • On WINDOWS 7, this file is in \Windows\system32\drivers\etc\hosts;
  • On WINDOWS 10, this file is in \Windows\system32\drivers\etc\hosts;

主机文件

##
# Host Database
# localhost is used to configure the loopback interface
##
#...
127.0.0.1           symfony.local

从现在开始,每次在此计算机上键入symfony.local时,您的计算机将使用环回接口连接到symfony.local.它会理解您要在localhost(127.0.0.1)上工作.

From now on, everytime you type symfony.local on this computer, your computer will use the loopback interface to connect to symfony.local. It will understand that you want to work on localhost (127.0.0.1).

3 从另一台计算机访问symfony.local :

3 Access symfony.local from an other computer:

我们终于到达了您的主要问题:

We finally arrive to your main question which is:

我现在如何通过另一台计算机访问我的网站?

How can I now access my website through an other computer?

现在这很容易!我们只需要告诉其他计算机如何找到symfony.local!我们如何做到这一点?

Well this is now easy! We just need to tell the other computers how they could find symfony.local! How do we do this?

3a 获取托管网站的计算机的IP地址:

我们首先需要知道托管网站的计算机上的IP地址(从一开始就一直在使用的IP地址).在终端中,在 MAC LINUX 上,键入ifconfig |grep inet,在WINDOWS上键入ipconfig.假设此计算机的IP地址为 192.168.1.5 .

We first need to know the IP address on the computer that hosts the website (the one we've been working on since the very beginning). In the terminal, on MAC and LINUX type ifconfig |grep inet, on WINDOWS type ipconfig. Let's assume the IP address of this computer is 192.168.1.5.

3b 在您试图访问网站的计算机上编辑主机文件.:

同样,在 MAC 上,此文件位于/private/etc/hosts中;在/etc/hosts中的 LINUX 上;并在\Windows\system32\private\etc\hosts中的 WINDOWS 上(如果您使用的是 WINDOWS 7 ,则此文件位于\Windows\system32\drivers\etc\hosts)..现在的诀窍是使用我们尝试访问/交谈的计算机的IP地址:

Again, on MAC, this file is in /private/etc/hosts; on LINUX, in /etc/hosts; and on WINDOWS, in \Windows\system32\private\etc\hosts (if you're using WINDOWS 7, this file is in \Windows\system32\drivers\etc\hosts).. The trick is now to use the IP address of the computer we are trying to access/talk to:

##
# Host Database
# localhost is used to configure the loopback interface
##
#...
192.168.1.5         symfony.local

4 最后在浏览器中享受搜索结果

现在,您可以进入浏览器并输入http://symfony.local,以在不同的计算机上精美地查看您的网站!请注意,如果您是OSX用户,则可以通过Virtual Box在Internet Explorer上测试您的网站(如果您不想使用Windows计算机),则可以应用相同的策略. 在OSX上制作Windows/IE测试环境.

You can now go into your browser and type http://symfony.local to beautifully see your website on different computers! Note that you can apply the same strategy if you are a OSX user to test your website on Internet Explorer via Virtual Box (if you don't want to use a Windows computer). This is beautifully explained in Crafting Your Windows / IE Test Environment on OSX.

您可能想知道如何从移动设备访问本地主机网站.在某些情况下,您将无法修改设备上的主机文件(iPhone,iPad ...)(不包括越狱).

You might wonder how to access your localhost website from a mobile device. In some cases, you won't be able to modify the hosts file (iPhone, iPad...) on your device (jailbreaking excluded).

那么,解决方案就是在托管网站的计算机上安装代理服务器,然后从您的iPhone连接到该代理.实际上,在以下帖子中对此进行了很好的解释,并且设置的时间并不长:

Well, the solution then is to install a proxy server on the machine hosting the website and connect to that proxy from your iphone. It's actually very well explained in the following posts and is not that long to set up:

在Mac上,我建议: SquidMan 作为代理.这是100%免费的解决方案.某些人还可以使用 Charles 作为代理服务器,但它是50美元.

On a Mac, I would recommend: Testing a Mac OS X web site using a local hostname on a mobile device: Using SquidMan as a proxy. It's a 100% free solution. Some people can also use Charles as a proxy server but it's 50$.

在Linux上,您可以通过使用 Squid 作为代理服务器来适应上面的Mac OS.

On Linux, you can adapt the Mac OS way above by using Squid as a proxy server.

在Windows上,您可以使用 Fiddler 进行操作.以下帖子中介绍了该解决方案:监视iPhone Fiddler的流量

On Windows, you can do that using Fiddler. The solution is described in the following post: Monitoring iPhone traffic with Fiddler

@Dre.任何可能的方式都可以通过另一台计算机从另一台计算机访问该网站 手动编辑主机文件?假设我要100台计算机 访问网站

@Dre. Any possible way to access the website from another computer by not editing the host file manually? let's say I have 100 computers wanted to access the website

这是一个有趣的问题,它与OP问题有关,请允许我提供帮助.

This is an interesting question, and as it is related to the OP question, let me help.

您必须在网络上进行更改,以便每台计算机都知道您的网站托管在哪里. 大多数日常路由器都不会这样做,因此您必须运行自己的 DNS服务器 >在您的网络上.

You would have to do a change on your network so that every machine knows where your website is hosted. Most everyday routers don't do that so you would have to run your own DNS Server on your network.

假设您有一个路由器(192.168.1.1).该路由器具有DHCP服务器,并为网络上的100台计算机分配IP地址.

Let's pretend you have a router (192.168.1.1). This router has a DHCP server and allocates IP addresses to 100 machines on the network.

现在,假设您与上述相同,在同一网络上的192.168.1.5上有一台拥有您的网站的计算机.我们将把该机器称为 pompei .

Now, let's say you have, same as above, on the same network, a machine at 192.168.1.5 which has your website. We will call that machine pompei.

$ echo $HOSTNAME
pompei

与以前一样,位于192.168.1.5的计算机 pompei 运行一个 HTTP服务器,该服务器为您的网站symfony.local提供服务.

Same as before, that machine pompei at 192.168.1.5 runs an HTTP Server which serves your website symfony.local.

为了让每台计算机都知道symfony.local托管在 pompei 上,我们现在需要网络上的自定义DNS服务器,该服务器知道symfony.local的托管位置.然后,网络上的设备将能够在内部解析 pompei 服务的域名.

For every machine to know that symfony.local is hosted on pompei we will now need a custom DNS Server on the network which knows where symfony.local is hosted. Devices on the network will then be able to resolve domain names served by pompei internally.

步骤1:DNS服务器

在网络上设置DNS服务器.为了方便起见,请在 pompei 上使用它,并使用 dnsmasq .

Set-up a DNS Server on your network. Let's have it on pompei for convenience and use something like dnsmasq.

Dnsmasq提供域名系统(DNS)转发器 ... .

我们希望 pompei 运行DNSmasq来处理DNS请求Hey, pompei, where is symfony.local并响应Hey, sure think, it is on 192.168.1.5 but don't take my word for it.

We want pompei to run DNSmasq to handle DNS requests Hey, pompei, where is symfony.local and respond Hey, sure think, it is on 192.168.1.5 but don't take my word for it.

继续安装dnsmasq,根据环境,dnsmasq配置文件通常位于/etc/dnsmasq.conf中.

Go ahead install dnsmasq, dnsmasq configuration file is typically in /etc/dnsmasq.conf depending on your environment.

我个人使用no-resolv和Google服务器server=8.8.8.8 server=8.8.8.4.

I personally use no-resolv and google servers server=8.8.8.8 server=8.8.8.4.

*注意:* 如果修改/etc/hosts文件,请务必重新启动DNSmasq,否则更改不会生效.

*Note:* ALWAYS restart DNSmasq if modifying /etc/hosts file as no changes will take effect otherwise.

第2步:防火墙

要工作, pompei 需要允许传入和传出域"数据包,这些数据包往返于端口53.当然!这些是DNS数据包,如果 pompei 不允许它们,则根本无法访问您的DNS服务器.继续并打开该端口53.在linux上,您通常会使用 iptables 这个.

To work, pompei needs to allow incoming and outgoing 'domain' packets, which are going from and to port 53. Of course! These are DNS packets and if pompei does not allow them, there is no way for your DNS server to be reached at all. Go ahead and open that port 53. On linux, you would classically use iptables for this.

分享我的想法,但是您很可能必须深入防火墙并充分了解所有内容.

Sharing what I came up with but you will very likely have to dive into your firewall and understand everything well.

#
# Allow outbound DNS port 53
#
 iptables -A INPUT -p tcp --dport 53 -j ACCEPT
 iptables -A INPUT -p udp --dport 53 -j ACCEPT

 iptables -A OUTPUT -p tcp --dport 53 -j ACCEPT
 iptables -A OUTPUT -p udp --dport 53 -j ACCEPT

 iptables -A INPUT -p udp --sport 53 -j ACCEPT
 iptables -A INPUT -p tcp --sport 53 -j ACCEPT

 iptables -A OUTPUT -p tcp --sport 53 -j ACCEPT
 iptables -A OUTPUT -p udp --sport 53 -j ACCEPT

步骤3:路由器

告诉路由器您的dns服务器现在在192.168.1.5上.在大多数情况下,您只需登录路由器并轻松手动进行更改即可.

Tell your router that your dns server is on 192.168.1.5 now. Most of the time, you can just login into your router and change this manually very easily.

就是这样,当您在计算机上询问symfony.local时,它将询问您的 DNS服务器托管symfony.local的位置,并在收到来自symfony.local的答复后立即进行询问. DNS服务器随后将向192.168.1.5上的 pompei 发送正确的 HTTP请求.

That's it, When you are on a machine and ask for symfony.local, it will ask your DNS Server where symfony.local is hosted, and as soon as it has received its answer from the DNS server, will then send the proper HTTP request to pompei on 192.168.1.5.

我让你玩这个,享受旅程.这两个步骤是主要准则,因此,如果您是第一次这样做,则必须调试并花费几个小时.假设这是一个更高级的网络,有主DNS服务器,辅助DNS服务器,

I let you play with this and enjoy the ride. These 2 steps are the main guidelines, so you will have to debug and spend a few hours if this is the first time you do it. Let's say this is a bit more advanced networking, there are primary DNS Server, secondary DNS Servers, etc.. Good luck!

这篇关于如何从同一网络上的另一台计算机连接到该本地主机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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