하나의 서버(하나의 IP)에 여러 개의 도메인이 접속시 각 해당 웹 서비스가 실행되게 하는 가상 호스트(Virtual Host)에 대해


하나의 웹 서버가 있을 경우(당연히 이 서버에 하나의 IP)가 할당되어 있을 것인데 이 웹 서버에 aaa.bbb.com, ccc.ddd.co.kr, eee.fff.org 등 여러 개의 도메인으로부터 접속해 들어올때 각각에 해당하는 웹 서비스가 실행되도록 할려면 가상 호스트(Virtual Host)를 적용해서 원하는 기능을 구현해 낼수 있다.


이 실험을 테스트한 상황(혹은 이를 위해 필요한 조건)

 -. Windows 10에 VirtualBox를 통해 Ubuntu 16.04 설치

 -. VirtualBox의 Ubuntu 서버에 apache2 설치

 -. VirtualBox의 Ubuntu 서버에 Php 7.0 설치

 -. 지금은 가상 호스트가 정상적으로 작동하는 것이 목적이므로 DB 연결까지 하지 않을 것이므로 MySQL은 설치하지 않음

 -. VirtualBox에 설치된 Ubuntu의 IP를 ipTIME 공유기에서 Static IP로 고정 할당

 -. ipTIME의 관리자 모드에서 '고급 설정' - 'NAT/라워 관리' - '포트포워드 설정'을 통해 외부의 80 포트로 들어오는 연결을 VirtualBox의 Ubuntu 16.04의 IP(위에서 Static으로 할당한 IP)로 포워딩 되도록 설정

 -. ipTIME의 DDNS 기능을 이용하여 2개의 도메인 네임을 생성하면서 각각의 해당 웹 서비스를 선택해서 실행

여기서는 ipTIME의 관리자 모드로 집입해서 '고급 설정' - '특수 기능' - 'DDNS 설정'을 이용해 아래와 같이 2개의 DDNS를 번갈아 설정하면서 Ubuntu에 설정한 2개의 가상 호스트로의 접근을 테스트 할 것이다.


fullstack.iptime.org  ⇒ VirtualBox Ubuntu 16.04에 있는 /home/fullstack/www/html/joe.php를 실행

joekog.iptime.org  ⇒  VirtualBox Ubuntu 16.04에 있는 /home/joekog/www/index.php를 실행


위와 같이 2개의 도메인이 동일한 서버(여기서는 VirtualBox Ubuntu 16.04)에 접속시 각각 서로 다른 웹 서비스가 실행되도록 구현하고자 한다. 


먼저 아래와 같이 사용자를 등록한다


root@gsoft:/home# adduser joekog

'joekog' 사용자를 추가 중...

새 그룹 'joekog' (1005) 추가 ...

새 사용자 'joekog' (1005) 을(를) 그룹 'joekog' (으)로 추가 ...

'/home/joekog' 홈 디렉터리를 생성하는 중...

'/etc/skel'에서 파일들을 복사하는 중...

새 UNIX 암호 입력: 

새 UNIX 암호 재입력: 

passwd: 암호를 성공적으로 업데이트했습니다

joekog의 사용자의 정보를 바꿉니다

새로운 값을 넣거나, 기본값을 원하시면 엔터를 치세요

이름 []: joekog

방 번호 []: 

직장 전화번호 []: 

집 전화번호 []: 

기타 []: 

정보가 올바릅니까? [Y/n] Y


이렇게 하면 /hoem/joekog가 생성되었을 것이다.

아래와 같이 joekog의 웹 서비스를 위한 joekog 사용자에 대한 웹 루트 경로를 생성한다.


root@gsoft:/home# cd joekog

root@gsoft:/home/joekog# ls -l

합계 32

drwxr-xr-x 2 joekog joekog 4096  5월 17 17:51 ./

drwxr-xr-x 8 root   root   4096  5월 17 17:51 ../

-rw-r--r-- 1 joekog joekog  220  5월 17 17:51 .bash_logout

-rw-r--r-- 1 joekog joekog 3771  5월 17 17:51 .bashrc

-rw-r--r-- 1 joekog joekog 8980  5월 17 17:51 examples.desktop

-rw-r--r-- 1 joekog joekog  655  5월 17 17:51 .profile

root@gsoft:/home/joekog# mkdir www

root@gsoft:/home/joekog# cd www

root@gsoft:/home/joekog/www# pwd

/home/joekog/www


이 위치에 php 파일을 하나 만들어 후에 http://joekog.iptime.org/hi.php와 같은 식으로 접속하고자 한다.

생성한 /home/joekog/www/ 위치에 vi hi.php로 다음과 같이 코딩한다.


<?php

        echo "<h1>Hi~~ This is joekog.iptime.org's web root.</h1>";

        echo "Web Root : /home/joekog/www/";

?>


# cd /etc/apache2/sites-available로 이동하면 000-default.conf라는 이름의 아파치 설정 파일이 있다.

이 파일 안에 보면 아래와 같으 항목이 있다. 


DocumentRoot /var/www/html


이 항목이 뜻하는 것은 아파치의 디폴트 웹 루트가 /var/www/html이라는 뜻이다. 

즉 http://joekog.iptime.org를 실행하면 /var/www/html/index.html이 실행되는 디폴트 웹 루트라는 것이다.

우리는 /home/joekog/www/을 웹 루트로 만들고 이 위치에 .html, .php 등을 위치시키고 이 웹 서비스를 실행시키고자 하는 것이다.

따라서 디폴트 웹 루트가 실행되지 않게 할려면 


# cd /etc/apache2/sites-enabled/로 이동해서 ls -l로 파일 목록을 표시해 보면 다음과 같은 심볼릭 링크 파일이 보일 것이다.


000-default.conf -> ../sites-available/000-default.conf


이 파일을 다음 명령을 이용해서 없애준다.


# a2dissite 000-default

Site 000-default disabled.

To activate the new configuration, you need to run:

  service apache2 reload


# service apache2 reload

# service apache2 restart


그러면 /etc/apache2/sites-enabled/에서 000-default.conf -> ../sites-available/000-default.conf가 없어 졌을 것이다.

/etc/apache2/sites-available/로 이동해서


# cp 000-default.conf joekog.iptime.org.conf


해당 경로에 joekog.iptime.org.conf 파일이 생겼을 것이다. 이제 /home/joekog/www/을 웹 루트로 만들도록 설정을 변경한다.


# vi joekog.iptime.org.conf 로 내용을 열면 아래와 같이 되어 있을 것이다.


<VirtualHost *:80>

        # The ServerName directive sets the request scheme, hostname and port that

        # the server uses to identify itself. This is used when creating

        # redirection URLs. In the context of virtual hosts, the ServerName

        # specifies what hostname must appear in the request's Host: header to

        # match this virtual host. For the default virtual host (this file) this

        # value is not decisive as it is used as a last resort host regardless.

        # However, you must set it for any further virtual host explicitly.

        #ServerName www.example.com


        ServerAdmin webmaster@localhost

        DocumentRoot /var/www/html


        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,

        # error, crit, alert, emerg.

        # It is also possible to configure the loglevel for particular

        # modules, e.g.

        #LogLevel info ssl:warn


        ErrorLog ${APACHE_LOG_DIR}/error.log

        CustomLog ${APACHE_LOG_DIR}/access.log combined


        # For most configuration files from conf-available/, which are

        # enabled or disabled at a global level, it is possible to

        # include a line for only one particular virtual host. For example the

        # following line enables the CGI configuration for this host only

        # after it has been globally disabled with "a2disconf".

        #Include conf-available/serve-cgi-bin.conf

</VirtualHost>


# vim: syntax=apache ts=4 sw=4 sts=4 sr noet


이 파일의 내용을 다음과 같이 변경해 준다.


<VirtualHost *:80>

        # The ServerName directive sets the request scheme, hostname and port that

        # the server uses to identify itself. This is used when creating

        # redirection URLs. In the context of virtual hosts, the ServerName

        # specifies what hostname must appear in the request's Host: header to

        # match this virtual host. For the default virtual host (this file) this

        # value is not decisive as it is used as a last resort host regardless.

        # However, you must set it for any further virtual host explicitly.

        ServerName joekog.iptime.org


        ServerAdmin webmaster@localhost

        DocumentRoot /home/joekog/www


        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,

        # error, crit, alert, emerg.

        # It is also possible to configure the loglevel for particular

        # modules, e.g.

        #LogLevel info ssl:warn


        ErrorLog ${APACHE_LOG_DIR}/joekog.iptime.org-error.log

        CustomLog ${APACHE_LOG_DIR}/joekog.iptime.org-access.log combined


        # For most configuration files from conf-available/, which are

        # enabled or disabled at a global level, it is possible to

        # include a line for only one particular virtual host. For example the

        # following line enables the CGI configuration for this host only

        # after it has been globally disabled with "a2disconf".

        #Include conf-available/serve-cgi-bin.conf

</VirtualHost>


# vim: syntax=apache ts=4 sw=4 sts=4 sr noet


수정한 파일의 내용을 저장하고 빠져나온다. 다음 명령을 통해서 joekog.iptime.org라는 도메인을 아파치에 등록한다.


# a2ensite joekog.iptime.org

Enabling site joekog.iptime.org.

To activate the new configuration, you need to run:

  service apache2 reload


이렇게하고서 /etc/apache2/sites-enabled/로 이동해 보면 다음과 같이 심볼릭 파일이 생성되었을 것이다.


joekog.iptime.org.conf -> ../sites-available/joekog.iptime.org.conf


그림과 같이 iptime 관리자 페이지로 접속해서 DDNS 설정 항목에서 '삭제'버튼을 눌러 기존 있던 DDNS를 삭제한다. 만일 처음부터 DDNS를 생성하지 않았다면 이 과정이 필요 없다.




아래 그림과 같이 우리가 원하는 도메인으로 joekog와 사용자 ID에 적당한 이메일 주소를 적고 'DDNS 등록' 버튼을 눌러서 생성한다.




아래 그림과 같이 "정상적으로 등록"이라는 메시지가 보이면 이 DDNS가 활성화 되기까지는 1-2분 정도의 시간이 소요되는 것 같다. 만일 "정상적으로 등록"이라는 항목에 이미 사용중인 DDNS라는 메시지 등이 보인다면 다른 이름의 DDNS를 등록해야 한다. 이럴 경우 위의 설정들을 모두 다른 이름의 DDNS에 맞게 고쳐야 한다.

따라서 테스트가 번거롭지 않을려면 ipTime에 먼저 원하는 DDNS를 등록해 보고 사용가능한지부터 확인하는 것이 팁이 될 것이다.




/etc/apache2/로 이동해서 apache2.conf파일의 내용을 편집한다. 여기서 /home/joekog/www에 대한 권한을 설정해 주지 않으면 웹 브라우저에서 http://joekog.iptime.org/hi.php로 접속했을 때 '웹 사이트 표시를 거부했다'는 식의 권한 관련 화면이 브라우저 뜨고 해당 웹 페이지가 실행되지 않는다.


# vi /etc/apache2/apache2.conf를 열어서 다음과 같은 정보가 보이는 곳의 아랫쪽에


<Directory />

        Options FollowSymLinks

        AllowOverride None

        Require all denied

</Directory>


<Directory /usr/share>

        AllowOverride None

        Require all granted

</Directory>


<Directory /var/www/>

        Options Indexes FollowSymLinks

        AllowOverride None

        Require all granted

</Directory>


다음과 같이 설정 값을 추가해 준다.


<Directory /home/joekog/www/>

        Options Indexes FollowSymLinks

        AllowOverride None

        Require all granted

</Directory>


이상의 모든 과정을 마친 후 아파치를 재시작해 주어야 한다.


# service apache2 restart


이제 웹 브라우저에서 


http://joekog.iptime.org로 접속하면 hi.php의 아래 내용이 실행된 웹 페이지를 브라우저에서 확인할수 있을 것이다.


<?php

echo "<h1>Hi~~ This is joekog.iptime.org's web root.</h1>";

echo "Web Root : /home/joekog/www/";

?>


이번에는 fullstack.iptime.org에 대해서 작업을 할텐데 joekog에 대해 했던 동일한 작업을 그대로 해 준 후 ipTIME의 DDNS 설정을 fullstack.iptime.or로 변경 한 후 웹 브라우저에서 http://fullstack.iptime.org를 실행하면 이번에는 /home/fullstack/www/html/joe.php의 내용이 실행된 웹 페이지가 뜨게 될것이다. 이때 서로 다른 웹 페이지임을 구분하기 위해서 joe.php의 내용을 hi.php와는 다른 내용으로 작성하면 가상 호스트의 작동이 정상적인지를 확인할수 있을 것이다.



+ Recent posts