본문 바로가기

WAS

5. Apache & Apache Tomcat 연동하기

http://localhost/ 혹은 http://127.0.0.1/으로 접속하면 Apache를 통해 페이지를 보여주고
http://localhost:8080/ 혹은 http://127.0.0.1:8080/으로 접속하면 Apache Tomcat를 통해 페이지를 보여줌

이제부터 Apache와 Apache Tomcat을 연동할 것입니다. 연동하는 이유는
html과 php를 사용한다면 Apache를 통해 http://localhost/ 혹은 http://127.0.0.1/로 접속하면 되지만 jsp를 사용해야 한다면 Apache Tomcat을 통해 http://localhost:8080/ 혹은 http://127.0.0.1:8080/로 포트를 설정해서 접속해야 할것이다.

하지만 서로 연동함으로써 jsp페이지라도 http://localhost/ 혹은 http://127.0.0.1/로 접속해서 php와 jsp 두가지 서비스를 제공할 수 있기 때문입니다. 이외에도 jsp를 좀더 빠르게 연결할 수 있습니다.

요즘엔 tomcat의 성능이 좋아져서 이렇게까지 할 필요가 없다고는 하지만 연동해 보는것도 좋은 공부가 될것 이라고 생각됩니다.
 
앞으로 만들 흐름입니다. php와 html은 Apache로 바로 해석하고 jsp는 Apache를 거쳐 Apache Tomcat을 통해 해석을 하게될 것 입니다.

맨 위에 있는 URL주소를 통해 톰캣 커넥터를 다운받음과 동시에 C:/WebService/Apache2/modules/폴더에 mod_jk로 이름을 바꿔서 저장합니다.

위와 같이 C:/WebService/Apache2/conf/폴더에 위와 같이 새로 만들기를 통해
workers.properties 파일을 생성합니다.

workers.properties 파일 안에 위와 같은 텍스트를 넣습니다.

#=====================================
# Define 1 real worker using ajp13
worker.list=worker1
# Set properties for worker1 (ajp13)
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009
#========================================


다음은 C:/WebService/Apache2/conf/폴더에 있는 httpd.conf파일을 열고 위와같이 'documentroot'를 검색합니다.
기존 Apache의 홈디렉토리를 Apache Tomcat의 홈데렉토리로 변경합니다.
여기서 홈디렉토리는 http://localhost/를 입력했을때 Apache가 홈디렉토리에 있는 파일을 보여 줌니다.
기존 C:/WebService/Apache2/htdocs를 C:/WebService/Tomcat 6.0/webapps/ROOT로 변경

다음은 directoryindex를 검색해서 위와 같이 index.jsp를 추가합니다.

위에서 http://localhost/를 입력하면 홈디렉토리에 있는 파일을 보여준다고 했습니다. 가장 처음으로 보여주는 파일이 index.html파일 입니다. 만약 존재하지 않는다면 index.html.var 이것도 존재하지않으면 index.jsp를 읽게 됩니다.

마지막으로 설정하는 부분입니다. httpd.conf파일의 맨아래에 위와 같이 빨간 네모안에 있는 파일을 추가하시면 됩니다.

#================================================
# Load mod_jk module
# Update this path to match your modules location
LoadModule jk_module modules/mod_jk.so
# Declare the module for <IfModule directive> (remove this line on Apache 2.x)
# AddModule mod_jk.c
# Where to find workers.properties
# Update this path to match your conf directory location (put workers.properties next to httpd.conf)
JkWorkersFile conf/workers.properties
# Where to put jk shared memory
# Update this path to match your local state directory or logs directory
JkShmFile logs/mod_jk.shm
# Where to put jk logs
# Update this path to match your logs directory location (put mod_jk.log next to access_log)
JkLogFile logs/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel info
# Select the timestamp log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
# Send everything to worker named worker1 (ajp13)
JkMount /*.jsp worker1
JkMount /*/servlet/* worker1
# JkOptions +ForwardURIEscaped
#==============================================



위와 같이 Apache Tomcat TryIcon을 더블클릭합니다.

다음과 같이 stop을 누른후 start를 눌러 재부팅합니다.

다음은 Apache TryIcon을 더블클릭 합니다.

위와 같이 stop을 누른후 start를 눌러 재부팅합니다.
그러면 오른쪽 아래와 같이 Tomcat 커넥터 모듈이 연동된걸 확인할수 있습니다.

위와 같이 주소를 입력하면  http://localhost/ 혹은 http://127.0.0.1/으로 접속하든  http://localhost:8080/ 혹은 http://127.0.0.1:8080/ 으로 접속하든 같은 페이지가 보이는 것을 확인할 수 있습니다.
위에서 documentRoot를 톰캣의 홈디렉토리로 변경했기 때문에 Apache를 통해 Apache Tomcat의 홈디렉토리로 연동 되는걸 확인할 수 있습니다.


연동이 됬는지 정확한 확인을 위해 위와 같이 
jsp_test.jsp파일과 php_test.php파일을 생성합니다. 

jsp_test.jsp

<%@ page contentType = "text/html; charset=euc-kr" language="java"%>
<html>
 <head>
  <title> JSP Test </title>
 </head>
 <body>
 <%
 String os_name = System.getProperty("os.name");
 String os_version = System.getProperty("os.version");
 String java_version = System.getProperty("java.version");
 String text = "JSP 한글 테스트";
 %>

 <b>OS Name</b> : <%=os_name%><br/>
 <b>OS version</b> : <%=os_version%><br/>
 <b>Java Version</b> : <%=java_version%><br/>
 <b>한글 테스트</b> : <%=text%><br/>

 </body>
</html>



php_test.php


<html>
 <head>
  <title> PHP Test </title>
 </head>
 <body>
 <?php
  echo "PHP한글 테스트";
  phpinfo();
 ?>
 </body>
</html>




위에서 생성한 jsp_test.jsp파일과 php_test.php파일을 홈디렉토리인 
C:/WebService/Tomcat 6.0/webapps/ROOT폴더에 붙여넣습니다.

http://localhost/jsp_test.jsp에서 jsp가 구동되는걸 확인할 수 있습니다.
http://localhost/php_test.php에서 php가 구동되는걸 확인할 수 있습니다.