Linux/Linux 실습

[Linux] 조건에 맞는 이름 기반의 가상 호스트 생성

GGkeeper 2021. 11. 9. 22:08

실습> 아래 조건에 맞게 이름 기반의 가상호스트를 생성하시오.

-- 조건 --
웹서버 설정 정보
도메인     사용자      웹페이지 경로                  디폴트 웹 문서   IP 주소
ebs.com  ebsuser  /home/ebsuser/public_html  index.html  192.168.8.3
ytn.com  ytnuser  /home/ytnuser/public_html  index.html  192.168.8.3
tvn.com  tvnuser  /home/tvnuser/public_html  index.html  192.168.8.3

-- 실습 순서 --
1. 도메인 설정
2. 사용자 생성
3. 기본 문서 생성
4. SELinux 설정
5. 가상호스트 설정
6. 웹서버 재시작
7. 도메인 접근
-- 실습 순서 --

1. 도메인 설정
DNS(Domain Name System)로 하지 않고 가짜로 설정한다.
DNS : 도메인주소 -> IP주소 변환
/etc/hosts 파일이 DNS보다 우선순위를 먼저한다.

# vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.108.3 firewall.linuxmaster.net
192.168.108.3 www.sbs.com sbs.com
192.168.108.3 www.kbs.com kbs.com
192.168.108.3 www.mbc.com mbc.com
192.168.108.3 www.ebs.com ebs.com
192.168.108.3 www.ytn.com ytn.com
192.168.108.3 www.tvn.com tvn.com

설정된 도메인으로 통신이 잘 되는지 확인한다.
# ping www.ebs.com
# ping www.ytn.com
# ping www.tvn.com

2. 사용자 생성
사용자 3명을 생성한다.
# useradd -g users ebsuser
# passwd ebsuser
# useradd -g users ytnuser
# passwd ytnuser
# useradd -g users tvnuser
# passwd tvnuser

3. 기본 문서 생성
각 사용자의 홈디렉터리의 권한을 701로 설정한다.
# chmod -c o+x /home/{ebs,ytn,tvn}user

ebsuser 사용자 로그인
login as: ebsuser
ebsuser@192.168.108.3's password:

$ cd public_html
$ vi index.html
<html>
 <head>
   <meta charset="utf8">
   <title> ::: ebs.com ::: </title>
 </head>

<body>

<center> ebs.com 에 오신 것을 환영합니다. </center>

</body>
</html>

ytnuser 사용자 로그인
login as: ytnuser
ytnuser@192.168.108.3's password:
$ cd public_html
$ vi index.html
<html>
 <head>
   <meta charset="utf8">
   <title> ::: ytn.com ::: </title>
 </head>

<body>

<center> ytn.com 에 오신 것을 환영합니다. </center>

</body>
</html>

tvnuser 사용자 로그인
login as: tvnuser
tvnuser@192.168.108.3's password:

$ cd public_html
$ vi index.html
<html>
 <head>
   <meta charset="utf8">
   <title> ::: tvn.com ::: </title>
 </head>

<body>

<center> tvn.com 에 오신 것을 환영합니다. </center>

</body>
</html>

4. SELinux 설정
# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      31

SELinux를 변경한다.
# chcon -R -t httpd_sys_content_t ~ebsuser/public_html
# chcon -R -t httpd_sys_content_t ~ytnuser/public_html
# chcon -R -t httpd_sys_content_t ~tvnuser/public_html

5. 가상호스트 설정
가상호스트 : 하나의 서버에서 여러 도메인을 운영할 수 있는 기능이다.

가상 도메인 설정 형식
<VirtualHost *:80>
    ServerAdmin   관리자 이메일 주소
    DocumentRoot   (절대경로)
    ServerName    도메인 웹페이지가 제공되는 경로주소
    ServerAlias   도메인 주소
    ErrorLog      웹페이지 접근 시에 에러 로그가 기록되는 경로
    CustomLog     웹페이지 접근 시에 정상/에러 로그가 기록되는 경로
</VirtualHost>


# vi /etc/httpd/conf/httpd.conf
 : 
 : (생략)
###########
# ebs.com #
###########
<VirtualHost *:80>
    ServerAdmin   webmaster@ebs.com
    DocumentRoot  /home/ebsuser/public_html
    ServerName    ebs.com
    ServerAlias   www.ebs.com
    ErrorLog      logs/ebs.com-error_log
    CustomLog     logs/ebs.com-access_log common
</VirtualHost>

###########
# ytn.com #
###########
<VirtualHost *:80>
    ServerAdmin   webmaster@ytn.com
    DocumentRoot  /home/ytnuser/public_html
    ServerName    ytn.com
    ServerAlias   www.ytn.com
    ErrorLog      logs/ytn.com-error_log
    CustomLog     logs/ytn.com-access_log common
</VirtualHost>

###########
# tvn.com #
###########
<VirtualHost *:80>
    ServerAdmin   webmaster@tvn.com
    DocumentRoot  /home/tvnuser/public_html
    ServerName    tvn.com
    ServerAlias   www.tvn.com
    ErrorLog      logs/tvn.com-error_log
    CustomLog     logs/tvn.com-access_log common
</VirtualHost>

6. 웹서버 재시작
설정이 완료되면 설정파일의 문법을 체크해서 Syntax OK 메세지가 나오면 
설정파일에 정상적으로 설정이 완료된 것이다. 이때 아파치 웹서버를 재시작한다.

/usr/sbin/httpd : 아파치 웹서버 데몬
httpd -t : 설정파일 문법 검사

# httpd -t
Syntax OK

# systemctl restart httpd

httpd -S : 현재 설정된 가상호스트와 여러 정보를 출력한다.
[root@firewall ~]# httpd -S
VirtualHost configuration:
*:80                   is a NameVirtualHost
         default server 192.168.108.3 (/etc/httpd/conf/httpd.conf:358)
         port 80 namevhost 192.168.108.3 (/etc/httpd/conf/httpd.conf:358)
         port 80 namevhost sbs.com (/etc/httpd/conf/httpd.conf:366)
                 alias www.sbs.com
         port 80 namevhost kbs.com (/etc/httpd/conf/httpd.conf:378)
                 alias www.kbs.com
         port 80 namevhost mbc.com (/etc/httpd/conf/httpd.conf:390)
                 alias www.mbc.com
         port 80 namevhost ebs.com (/etc/httpd/conf/httpd.conf:402)
                 alias www.ebs.com
         port 80 namevhost ytn.com (/etc/httpd/conf/httpd.conf:414)
                 alias www.ytn.com
         port 80 namevhost tvn.com (/etc/httpd/conf/httpd.conf:426)
                 alias www.tvn.com
ServerRoot: "/etc/httpd"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/etc/httpd/logs/error_log"
Mutex default: dir="/run/httpd/" mechanism=default
Mutex mpm-accept: using_defaults
Mutex authdigest-opaque: using_defaults
Mutex proxy-balancer-shm: using_defaults
Mutex rewrite-map: using_defaults
Mutex authdigest-client: using_defaults
Mutex proxy: using_defaults
Mutex authn-socache: using_defaults
PidFile: "/run/httpd/httpd.pid"
Define: _RH_HAS_HTTPPROTOCOLOPTIONS
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="apache" id=48
Group: name="apache" id=48

7. 도메인 접근
lynx : 텍스트용 웹브라우저
# yum -y install mc

# lynx --dump ebs.com
                 ebs.com 에 오신 것을 환영합니다.


# lynx --dump ytn.com
                 ytn.com 에 오신 것을 환영합니다.


# lynx --dump tvn.com
                 tvn.com 에 오신 것을 환영합니다.


8. 윈도우 hosts 파일 수정
실제 운영을 한다면 이 부분은 필요없고 DNS에서 설정하면 된다.
여기서는 가짜로 운영하는 것이므로 설정해준다.
cmd를 관리자 권한으로 실행하고 notepad를 이용해서 hosts 파일을 수정한다.
C:\Windows\system32>notepad %systemroot%\system32\drivers\etc\hosts
  :
  :(생략)
192.168.108.3 www.sbs.com sbs.com
192.168.108.3 www.kbs.com kbs.com
192.168.108.3 www.mbc.com mbc.com
192.168.108.3 www.ebs.com ebs.com
192.168.108.3 www.ytn.com ytn.com
192.168.108.3 www.tvn.com tvn.com

웹 브라우저에서 확인한다.
http://ebs.com
ebs.com 에 오신 것을 환영합니다.

http://ytn.com
ytn.com 에 오신 것을 환영합니다.

http://tvn.com
tvn.com 에 오신 것을 환영합니다.