본문 바로가기

WebServer/Tomcat

apache + tomcat 설정

Windows에서 아파치 톰켓 연동을 해보는 중이다. 이런저런 이유 때문에 리눅스에서 해보고 싶지만 윈도우 환경만 가능하다. 어차피 테스트 환경을 구축하는 중이라 큰 문제는 없어보인다.


먼저 아파치는 아래 페이지를 따라하면 된다.

http://junemoon.tistory.com/150


아파치 설치가 끝났으니 이제 톰켓을 깔고 연동을 해보자. 난 톰켓 7으로 테스트 환경을 만들었다.


톰켓 다운로드

http://tomcat.apache.org/download-70.cgi


톰켓을 원하는 경로에 앞축을 풀면된 일딴 놔두자. 톰켓은 실행하면 기본 페이지가 있으므로 설정할 것이 없다. 이제 httpd와 연동해야하는데 컨넥터가 필요하다. 윈도우 컨넥터 파일을 다운로드 받자.


톰켓 컨넥터 다운로드

http://mirror.apache-kr.org/tomcat/tomcat-connectors/jk/binaries/windows/


압축파일 안에 mod_jk.so 파일이 있을 것이다. 이걸 아파치 폴더 안에 modules 이라는 폴더가 있는데 여기에 넣으면 된다. 넣었으면 아파치에서 설정을 해주어야 하는데 설정은 별도의 파일로 하고싶다. 나중에 복붙하기도 편하고 정리되있어야 이해하기도 쉬우니까...


httpd.conf 안에 Include conf/extra/httpd-default.conf 이 부분이 주석처리 되어있는데 주석을 풀어주자. 이 안에서 보안 관련 부분만 수정하려고 한다. ServerTokens 찾아서 수정한다.

ServerTokens Prod

## 추가: Trace 메소드 사용 금지

TraceEnable Off


ServerTokens는 웹 서버의 정보를 어디까지 알려주는지 설정할 수 있는데, 너무 많은 정보가 알려지면 해킹당하기도 그만큼 쉬워질태니 서버이름만 노출되도록 정의했다. 그리고 TraceEnable Off는 TRACE 메서드를 사용하지 못하게 하는 설정이다.


다음으로 Include conf/extra/httpd-vhosts.conf 의 주석을 해제한다. VirtualHost 설정을 사용하여 2개의 도메인을 이용해 톰켓과 아파치를 나눠서 설정해보려고 한다. test1, test2를 추가하고 test1에 톰켓을 연결할 것이다. 다음과 같이 설정해보았다.

#NameVirtualHost *:80 ## 2.4 이상은 이 설정이 필요없다.

Include conf/extra/httpd-vhosts-localhost1.conf

Include conf/extra/httpd-vhosts-localhost2.conf


설정은 아파치 2.4 이상부터 필요없다. 넣으면 httpd가 실행되지도 않는다. 2.4 도큐먼트에는 내용이 있던데.... 쩝.

각각 설정 내용은 다음과 같다.


httpd-vhosts-localhost1.conf

<VirtualHost *:80 >

    ServerAdmin 관리자 메일주소

    DocumentRoot "D:/dev/server/webContents/test1"

    ServerName test1.localhost.com

    Include conf/extra/security.conf


    <Directory "D:/dev/server/webContents/test1">

        Options FollowSymLinks

        AllowOverride None

        Require all granted

    </Directory>

<IfModule dir_module>

DirectoryIndex index.html

</IfModule>

    RewriteEngine On

    RewriteCond %{REQUEST_METHOD} (PUT|DELETE|TRACE|OPTIONS)

    RewriteRule (.*) - [F]


    <Location /jkmanager>

        jkmount jkstatus

        Order deny,allow

        Deny from all

        Allow from env=let_me_in

    </Location>


    JkMountFile D:/dev/server/httpd-2.4.17-win64-VC14/conf/extra/uriworkermap-test1.properties

</VirtualHost>


test1.localhost.com 이라는 도메인을 정의하고 여기에 아파치 설정을 연결했다. 2.4 부터는 Require all granted 옵션으로 바뀌었다. 이전에는 보통 [ Order allow,deny / Allow from all ] 함께 사용했었다.

Rewrite는 메서드사용을 제한하기 위해 사용했다. RewriteCond 뒷부분에 나열된 메서드는 404 오류가 나도록 정의했다. OPTIONS에서 사용가능한 메소드 목록이 나오는데 정의해주면 나오지 않을 것이다.


uriworkermap-test1.properties 파일도 마저 정의한다.


uriworkermap-test1.properties

/*=wlb


tomcat과 연동할 부분은 정의 하는 것이다. 모든 URI를 wlb라는 tomcat과 연동한다는 의미이다.


httpd-vhosts-localhost2.conf

<VirtualHost *:80 >

    ServerAdmin 관리자 메일주소

    DocumentRoot "D:/dev/server/webContents/test2"

    ServerName test2.localhost.com

    Include conf/extra/security.conf


    <Directory "D:/dev/server/webContents/test2">

        Options FollowSymLinks

        AllowOverride None

        Require all granted

    </Directory>

<IfModule dir_module>

DirectoryIndex index.html

</IfModule>

    RewriteEngine On

    RewriteCond %{REQUEST_METHOD} (PUT|DELETE|TRACE|OPTIONS)

    RewriteRule (.*) - [F]

</VirtualHost>


test2.localhost.com 이라는 도메인을 정의했다. test1.localhost.com, test2.localhost.com 각각 다른 페이지가 나올 것이다. test1.localhost.com, test2.localhost.com을 hosts파일에 다음과 같이 정의해서 테스트 가능하도록 하자.

127.0.0.1 test1.localhost.com

127.0.0.1 test2.localhost.com


이제 httpd.conf 안에 다음 설정을 추가한다.

# Apache tomcat connector

Include conf/extra/httpd-modjk.conf


conf 밑에 extra 밑에 httpd-modjk.conf 파일을 만들어 설정할 것이다. 이제 파일을 만들고 설정해보자.


httpd-modjk.conf

#

# HTTPD Web Server and Apache Tomcat(ajp) Connector

# the configuration of the server.

#

# Required modules: mod_jk (for the ajp handler),


# Load Module jk_module

<IfModule !mod_jk.c>

    LoadModule jk_module modules/mod_jk.so

</IfModule>


# jk_module Global configuration of the server

<IfModule mod_jk.c>

    JkWorkersFile conf/extra/workers.properties

    JkLogFile "|D:/dev/server/httpd-2.4.17-win64-VC14/bin/rotatelogs.exe D:/dev/server/httpd-2.4.17-win64-VC14/logs/mod-jk.log.%Y%m%d 86400"

    #JkLogLevel debug

    #JkLogLevel info

    JkLogLevel error

    JkLogStampFormat "[%Y %a %b %d %H:%M:%S]"

    JKRequestLogFormat "%w %R %V %T"

    JkOptions +ForwardKeySize +ForwardURICompatUnparsed -ForwardDirectories

</IfModule>


이제 workers.properties 파일을 정의해보자.


workers.properties

# HTTPD Web Server and Apache Tomcat(ajp) Connector

# the loadbalancer configuration of the Server

#

# Include workers.properties by conf/extra/httpd-modjk.conf

#

# Define loadbalancer 2 worker node using ajp13

worker.list=w01, w02, wlb

# admin Cluster Group 1 #############################################

#

# configuration template

worker.template.type=ajp13

worker.template.lbfactor=1

worker.template.socket_timeout=30

worker.template.socket_keepalive=true

worker.template.recovery_options=7

worker.template.ping_mode=A

worker.template.ping_timeout=10000

worker.template.connection_pool_size=25

worker.template.connection_pool_minsize=25

worker.template.connection_pool_timeout=60


worker.w01.reference=worker.template

worker.w01.port=8009

worker.w01.host=localhost


worker.w02.reference=worker.template

worker.w02.host=localhost

worker.w02.port=8009


worker.wlb.type=lb

worker.wlb.retries=2

worker.wlb.method=Session

worker.wlb.sticky_session=True

worker.wlb.balance_workers=w01,w02


로드발란싱을 위해 일딴 2개의 설정을 만들어두었다. 아직은 로드발란싱이 적용되지 않았다.


그리고 톰캣에서 conf 폴더의 server.xml 파일을 열어보자. Connector 태그 안에 port가 workers에서 정의한 포트가 같은지 확인하자. port가 같다면 이제 설정은 끝이다.


이제 httpd와 tomcat을 실행하고 잘 연동되는지 확인해보자.


test1.localhost.com


test2.localhost.com


좀 더 구체적인 설정도 필요해 보이나 여기까지하면 기본적인 것은 끝난 것같다. httpd 하나로 한 서버에서 여러 도메인을 운영할 수 있는 훌륭한 기능도 있다. 운영하다 보면 이렇게 사용할 경우가 종종 발생하는데, 유용하게 사용할 수 있겠다.



※ 참고

https://tomcat.apache.org/connectors-doc-archive/jk2/jk/aphowto.html

https://tomcat.apache.org/connectors-doc/reference/apache.html




'WebServer > Tomcat' 카테고리의 다른 글

Connection reset by peer: socket write error  (0) 2016.07.19
HTTP Method 제한하기  (0) 2015.06.26
CentOS6(x86_64)에서 Tomcat6 설치  (0) 2011.10.19
Linux 톰켓 설정  (0) 2011.08.03
Linux 톰켓 설치  (0) 2011.08.03