Apache | Basic Apache Configuration | Host Access Settings (hosts File)

This article explains how to configure host access by using the hosts file.

Access settings using the hosts file

When Apache is installed in a local environment and accessed from a browser, localhost, which represents the local machine, is used. Access is also possible through 127.0.0.1.

127.0.0.1 is a special IP address called the local loopback address, which points to the machine itself. Even when an IP address is configured on the local host, 127.0.0.1 always represents the local machine.

Therefore, when Apache or a similar service runs on the same computer as the client, accessing 127.0.0.1 means accessing your own PC, and as a result you can access Apache running on the same computer.

So what is localhost? It is the host name configured for the IP address 127.0.0.1. Therefore, when you access localhost, you access 127.0.0.1, and ultimately access your own machine.

The place where host names and IP addresses are defined is the hosts file. DNS servers can also be used to define host names and IP addresses, but by writing them in the local hosts file, you can register host names for IP addresses without referring to a DNS server.

Location of the hosts file

On Windows, the hosts file is located in C:\Windows\System32\drivers\etc.

C:\Windows\System32\drivers\etc>dir
 C 드라이브의 볼륨에는 이름이 없습니다.
 볼륨 일련 번호: XXXX-XXXX

 C:\Windows\System32\drivers\etc 디렉터리

2019-03-19  오후 01:53    <DIR>          .
2019-03-19  오후 01:53    <DIR>          ..
2019-03-19  오후 01:49               824 hosts <<<<<<<<<<<<<<<<< 여기 존재한다.
2019-03-19  오후 01:49             3,683 lmhosts.sam
2019-03-19  오후 01:49               407 networks
2019-03-19  오후 01:49             1,358 protocol
2019-03-19  오후 01:49            17,635 services
               5개 파일              23,907 바이트
               2개 디렉터리  458,572,058,624 바이트 남음

C:\Windows\System32\drivers\etc>

hosts is a text file, so it can be opened with a text editor.

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost

The hosts file can register host names for IP addresses. The format is as follows.

IP address		host name

In the default settings, localhost is registered as the host name for 127.0.0.1. Because it is registered here, entering localhost lets the system know that the IP address for that host name is 127.0.0.1, and as a result it can access 127.0.0.1.

Here, 127.0.0.1 is an IPv4 address, and ::1 is an IPv6 address.

In the Windows environment checked here, both entries were commented out. Leaving them as-is may cause inconvenience, so remove # as follows.

# localhost name resolution is handled within DNS itself.
127.0.0.1           localhost
#	::1             localhost

On Windows, note that you may not be able to edit the file unless the text editor is run with administrator privileges.