갈루아의 반서재

 

아파치 웹서버를 사용하는 경우, 하나 이상의 도메인을 호스팅하기 위해 버추얼 호스트(Nginx 의 server blocks 과 유사)를 설정할 수 있다.

도메인 설정과 관련해서 더욱 자세한 내용은 introduction to DigitalOcean DNS 을 참조하기 바란다.

1. antilibrary.org 라는 도메인 이름으로 디렉토리를 생성한다. mkdir -p 상위경로도 함께 생성하라는 의미이다.

(parabellum) founder@hilbert:~$ sudo mkdir -p /var/www/antilibrary.org/public_html

로그 파일이 저장될 디렉토리도 생성

(parabellum) founder@hilbert:~$ sudo mkdir /var/www/antilibrary.org/logs

해당 디렉토리에 권한 부여

(parabellum) founder@hilbert:~$ sudo chown -R founder:founder /var/www/antilibrary.org/public_html

누구나 접근해서 읽을 수 있도록 권한을 변경한다.

(parabellum) founder@hilbert:~$ sudo chmod -R 755 /var/www

샘플 index.html 파일을 다음의 내용으로 생성한다.

(parabellum) founder@hilbert:~$ sudo vi /var/www/antilibrary.org/public_html/index.html
<html>
        <body>
                <h1>Hellow, World!</h1>
                <h2>Here is example.com</h2>
        </body>
</html>

다음 경로에 새로운 가상 호스트 파일을 생성한다.

(parabellum) founder@hilbert:~$ sudo nano /etc/apache2/sites-available/antilibrary.org.conf

아래 사항을 수정한다. 정보 수정이 끝나면 Ctrl+X 로 빠져나온다.

  • ServerAdmin -  에러발생시 이메일 연락처
  • ServerName 에는 example.com 과 같이 도메인 입력
  • ServerAlias 에는 www.example.com 와 같은 별칭 입력
  • DocumentRoot 는 가상호스트로 사용될 주소입력
  • ErrorDocument 에는 404 에러시 보여줄 문서 지정
  • 로그가 작성될 위치 지정, 기본값은 /var/log/apache2 디렉토리 이하에 저장된다. 가상 호스트별로 따로 관리하고 싶다면 아래와 같이 별도 위치를 지정해준다.

다음으로 디렉토리 지시자를 설정한다. 기본값인 /var/www 를 사용할 가상호스트 디렉토리 주소로 변경한다. 

<VirtualHost *:80>
    ServerAdmin webmaster@antilibrary.org
    ServerName antilibrary.org
    ServerAlias www.antilibrary.org
    DocumentRoot /var/www/antilibrary.org/public_html
    ErrorDocument 404 /404.html
    ErrorLog /var/www/anilibrary.org/logs/error.log
    CustomLog /var/www/antilibrary.org/logs/access.log combined
</VirtualHost>

<Directory /var/www/antilibrary.org/public_html>
 Options FollowSymLinks
 AllowOverride All
 Order allow,deny
 allow from all
</Directory>

해당 설정 파일을 a2ensite 를 통해 활성화시킨다.

(parabellum) founder@hilbert:~$ sudo a2ensite antilibrary.org
Enabling site antilibrary.org.
To activate the new configuration, you need to run:
  systemctl reload apache2

000-default.conf  에 정의된 기본 사이트를 비활성화시킨다.

(parabellum) founder@hilbert:~$ sudo a2dissite 000-default.conf
Site 000-default disabled.
To activate the new configuration, you need to run:
  systemctl reload apache2

설정 파일에 에러가 없는지 확인해보자.

(parabellum) founder@hilbert:~$ sudo apache2ctl configtest
(2)No such file or directory: AH02291: Cannot access directory '/var/www/anilibrary.org/                                                     logs/' for error log of vhost defined at /etc/apache2/sites-enabled/antilibrary.org.conf                                                     :1
AH00014: Configuration check failed
Action 'configtest' failed.
The Apache error log may have more information.

에러가 있다. 로그 디렉토리 경로에 오타가 있다. 수정하고 다시 테스팅해보자.

(parabellum) founder@hilbert:/etc/apache2/sites-available$ sudo apache2ctl configtest
Syntax OK

변경사항을 반영하기 위해 아파치를 다시 실행하자.

(parabellum) founder@hilbert:/etc/apache2/sites-available$ sudo systemctl restart apache2


이제 아이피로 접속해보면, 앞서 작성한 내용이 표시됨을 알 수 있다. 이제 네임서버 설정을 통해 도메인 입력시 해당 내용이 표시되도록 해보자.