동일한 네트워크의 다른 컴퓨터에서이 로컬 호스트에 어떻게 연결합니까?
현재 프로젝트를 진행 중이며 한 랩톱은 다른 랩톱의 로컬 호스트에 연결되는 집에있는 두 개의 랩톱에서 테스트하고 싶습니다. XAMPP를 사용하고 있습니다. 어떻게해야합니까?
확실히 가능합니다. 여기서는 Apache와 관련된 일반적인 사례를 살펴 보겠습니다.
당신이 큰 Symfony2 팬이고 http://symfony.local/
4 개의 다른 컴퓨터 (웹 사이트를 호스팅하는 주요 컴퓨터는 물론 Mac, Windows 및 Linux 배포판 (무선 또는 무선)에 연결됨) 에서 symfony 웹 사이트에 액세스하려고한다고 가정 해 봅시다. 메인 컴퓨터.
일반 스케치 :
1 가상 호스트를 설정하십시오 .
먼저 아파치 httpd-vhosts.conf
파일 에서 가상 호스트를 설정해야 합니다. 에 XAMP , 여기에이 파일을 찾을 수 있습니다 C:\xampp\apache\conf\extra\httpd-vhosts.conf
. 에 MAMP , 여기에이 파일을 찾을 수 있습니다 Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
. 이 단계 는 symfony.local
요청 을 처리하기 위해 컴퓨터의 웹 서버를 준비 합니다. 웹 사이트의 루트 / 메인 폴더뿐만 아니라 가상 호스트의 이름도 제공해야합니다. 이렇게하려면 해당 파일 끝에 다음 줄을 추가하십시오. DocumentRoot
기본 폴더 의 위치 를 로 변경해야합니다 . 여기 /Applications/MAMP/htdocs/Symfony/
에서는 내 웹 사이트의 루트로 사용했습니다.
<VirtualHost *:80>
DocumentRoot "/Applications/MAMP/htdocs/Symfony/"
ServerName symfony.local
</VirtualHost>
2 호스트 파일을 구성하십시오 .
클라이언트 (이 경우 브라우저)가 symfony.local
실제로 의미 하는 바를 이해 하려면 컴퓨터에서 호스트 파일을 편집해야합니다. 브라우저에 URL을 입력 할 때마다 컴퓨터는 그 의미를 이해하려고 시도합니다! symfony.local
컴퓨터에는 아무 의미가 없습니다. 따라서 이름 symfony.local
을 IP 주소로 확인 하려고 시도 합니다. 먼저 컴퓨터의 호스트 파일을 조사하여 IP 주소를 주소 표시 줄에 입력 한 것과 일치하는지 확인할 수 있습니다. 그렇게 할 수 없으면 DNS 서버에 요청 합니다 . 여기서 트릭은 다음을 호스트 파일에 추가하는 것입니다.
- 에 MAC ,이 파일에
/private/etc/hosts
; - 에 LINUX ,이 파일에
/etc/hosts
; - 에 WINDOWS ,이 파일에
\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
;
Hosts file
##
# Host Database
# localhost is used to configure the loopback interface
##
#...
127.0.0.1 symfony.local
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 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?
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 Get the IP address of the computer hosting the website:
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 Edit the hosts file on the computer you are trying to access the website from.:
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 Finally enjoy the results in your browser
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.
You can also access your localhost from mobile devices
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).
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:
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$.
On Linux, you can adapt the Mac OS way above by using Squid as a proxy server.
On Windows, you can do that using Fiddler. The solution is described in the following post: Monitoring iPhone traffic with Fiddler
Edit 23/11/2017: Hey I don't want to modify my Hosts file
@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
This is an interesting question, and as it is related to the OP question, let me help.
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.
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.
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
Same as before, that machine pompei at 192.168.1.5
runs an HTTP Server which serves your website symfony.local
.
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.
3 simple steps.
Step 1: DNS Server
Set-up a DNS Server on your network. Let's have it on pompei for convenience and use something like dnsmasq.
Dnsmasq provides Domain Name System (DNS) forwarder, ....
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
.
Go ahead install dnsmasq, dnsmasq configuration file is typically in /etc/dnsmasq.conf
depending on your environment.
I personally use no-resolv
and google servers server=8.8.8.8 server=8.8.8.4
.
*Note:* ALWAYS restart DNSmasq if modifying /etc/hosts file as no changes will take effect otherwise.
Step 2: Firewall
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
Step 3: Router
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.
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
.
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!
For testing on both laptops on the same wireless and across the internet you could use a service like http://localhost.run/ or https://ngrok.com/
Provided both machines are in the same workgroup, open cmd.exe
on the machine you want to connect to, type ipconfig
and note the IP at the IPv4 Address
line.
Then, on the machine you want to connect with, use http:// + the IP of the target machine
.
That should do it.
This tool saved me a lot, since I have no Admin permission on my machine and already had nodejs installed. For some reason the configuration on my network does not give me access to other machines just pointing the IP on the browser.
# Using a local.dev vhost
$ browser-sync start --proxy
# Using a local.dev vhost with PORT
$ browser-sync start --proxy local.dev:8001
# Using a localhost address
$ browser-sync start --proxy localhost:8001
# Using a localhost address in a sub-dir
$ browser-sync start --proxy localhost:8080/site1
http://www.browsersync.io/docs/command-line/
On Mac OS X:
In terminal, run
ifconfig | grep 192.
to get the internal IP of your machine on it's current network. It may be something like192.168.1.140
Start a server. For example,
node server.js
may start a server on some port.On your phone, visit
192.168.1.140:3000
or whatever port your server is running on.
it may be that your firewalls are preventing you from accessing the localhost's webserver.
Put the IP addresses of both of your computers' internet security antivirus network security as safe IP addresses if required.
How to find the IP address of your windows PC: Start > (Run) type in: cmd (Enter)
(This opens the black box command prompt)
type in ipconfig (Enter)
Let's say your Apache or IIS webserver is installed on your PC: 192.168.0.3
and you want to access your webserver with your laptop. (laptop's IP is 192.168.0.5)
On your PC you type in: http://localhost/ inside your Firefox or Internet Eplorer browser to access your data on your webserver.
On your laptop you type in http://192.168.0.3/ to access your webserver on your PC.
For all these things to work you need have installed a webserver correctly (e.g. IIS, Apache, XAMP, WAMP etc).
If it does not work, try to ping your PC from your laptop:
Open up command propmt on your laptop: Start > cmd (Enter)
ping 192.168.1.3 (Enter)
If the pinging fails, then firewalls are blocking your connection or your network cabling is faulty. Restart your modem or network switch and your machines.
Close programs such as chat programs that are using your ports.
You can also try a diffrent port number: http:192.168.0.3:80 or http:192.168.0.3:81 or any random number at the end
위의 단계를 수행 한 후에도 여전히 다른 장치에서 로컬 호스트에 액세스 할 수없는 경우 참고 사항입니다. 이는 apache ports.conf가 외부가 아닌 로컬 (127.0.0.1)로 제공되도록 구성 되었기 때문일 수 있습니다.
다음을 확인하십시오 (ubuntu apache2의 경우).
$ cat /etc/apache2/ports.conf
다음이 설정되면
NameVirtualHost *:80
Listen 127.0.0.1:80
그런 다음 기본값으로 다시 변경하십시오.
NameVirtualHost *:80
Listen 80
Windows를 사용하는 경우 ipconfig 를 사용 하여 로컬 IPv4 주소를 가져온 다음 Apache 구성 파일 아래에 httpd.conf 를 지정하십시오 .
Listen: 10.20.30.40:80
Apache 서버를 다시 시작하고 네트워크의 다른 컴퓨터에서 테스트하십시오.
'IT' 카테고리의 다른 글
다양한 깊이의 중첩 된 사전 값 업데이트 (0) | 2020.06.13 |
---|---|
루프 나 조건문없이 1부터 1000까지 인쇄하는 C 코드는 어떻게 작동합니까? (0) | 2020.06.13 |
Windows : 명령 프롬프트에서 여러 줄 명령을 지정하는 방법? (0) | 2020.06.13 |
런타임시 유형의 기본값 (0) | 2020.06.13 |
vim에서 특정 캐릭터로 점프하는 방법은 무엇입니까? (0) | 2020.06.13 |