본문 바로가기

리눅스

Apache 웹 서버와 Tomcat 애플리케이션 서버를 다중 인스턴스로 실행하는 방법

반응형

Apache 웹 서버와 Tomcat 애플리케이션 서버를 다중 인스턴스로 실행하는 방법

Apache의 APR 라이브러리, Tomcat 커넥터, 그리고 JDK를 사용합니다.

JDK 설치(java 설치)

tar xvfz jdk-7u65-linux-x64.tar.gz -C /app/
ln -s /app/jdk1.7.0_65 /app/java

APR 및 APR-Util 컴파일 및 설치

  • apr-1.5.1 컴파일 및 설치
tar xvfz apr-1.5.1.tar.gz
./configure --prefix=/usr/local/apr
make && sudo make install
  • apr-util-1.5.4 컴파일 및 설치
tar xvfz apr-util-1.5.4.tar.gz
./configure --with-apr=/usr/local/apr
make && sudo make install

Apache HTTP Server 컴파일 및 설치

tar xvfz httpd-2.4.9.tar.gz
./configure --prefix=/app/apache2 --with-mpm=worker --enable-mods-shared=all --enable-module=so --enable-module=rewrite --with-apr=/usr/local/apr
make && sudo make install

Tomcat Native 컴파일 및 설치

tar xvfz tomcat-native-1.1.31-src.tar.gz
cd tomcat-native-1.1.31-src/jni/native/
./configure --prefix=/app/tomcat/instance1 --with-apr=/usr/local/apr --with-java-home=/app/java --with-ssl
make && sudo make install

Tomcat Connector 컴파일 및 설치

tar xvfz tomcat-connectors-1.2.40-src.tar.gz
cd tomcat-connectors-1.2.40-src/native
./buildconf.sh
./configure --prefix=/app/apache2 --with-apxs=/app/apache2/bin/apxs
make && sudo make install

Tomcat 설정

tar xvfz apache-tomcat-7.0.56.tar.gz -C /app/tomcat/instance1

tuser 계정 생성

useradd tuser
passwd tuser
vim /home/tuser/.bash_profile
...
export JAVA_HOME="/app/java"
export PATH=$JAVA_HOME/bin/:$PATH
export TERM=xterm
export CLASSPATH=$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar

Apache와 Tomcat 연동

[root@Test12 app]$ pwd
/app
[root@Test12 app]$ tree -L 2
.
├── apache2
├── java
└── tomcat
    ├── instance1
    ├── instance2
    └── instance3

25 directories, 7 files
  • httpd.conf 파일
vim /app/apache2/conf/httpd.conf
...
# Mod Jk setting
Include conf/extra/httpd-modjk.conf
...
  • httpd-vhosts.conf 파일
vim /app/apache2/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "/app/apache2/htdocs/t.scbyun.com"
    ServerName t.scbyun.com
    ServerAlias www.t.scbyun.com
    ErrorLog "logs/t.scbyun.com-error_log"
    CustomLog "logs/t.scbyun.com-access_log" common

    JkMount  /*.jsp  loadbalancer

</VirtualHost>
  • httpd-modjk.conf 파일
vim /app/apache2/conf/extra/httpd-modjk.conf
#
# HTTPD Web Server and Apache Tomcat(ajp) Connector
# the configuration of the server.
#
# Required modules: mod_jk (for the ajp handler),

# 1.
# Load Module jk_module
<IfModule !mod_jk.c>
        LoadModule jk_module modules/mod_jk.so
</IfModule>

# 2.
# jk_module Global configuration of the server
<IfModule mod_jk.c>
        JkWorkersFile conf/extra/workers.properties

        JkLogFile "|/app/apache2/bin/rotatelogs -l /app/apache2/logs/modjk_log.%Y%m%d%H 3600"
        JkLogLevel error
        JkLogStampFormat "[%Y %a %b %d %H:%M:%S]"
        JKRequestLogFormat " %R %w %V %T %U"

        JkOptions +ForwardKeySize +ForwardURICompatUnparsed -ForwardDirectories
        #JkMountFile conf/extra/uriworkermap.properties
        JkShmFile /app/apache2/logs/mod-jk.shm
</IfModule>

# 3.
# Allow jk Connector server status reports generated by jkstatus,
# with the URL of http://servername/jkstatus
# Change the "127.0.0.1" to match your domain to enable.
<Location /jkstatus>
    JkMount jkstatus
    Order deny,allow
    Deny from all
    Allow from 127.0.0.1
</Location>
  • workers.properties 파일
vim /app/apache2/conf/extra/workers.properties
worker.list = loadbalancer, status

worker.instance1.port=8009
worker.instance1.maintain=60
worker.instance1.host=localhost
worker.instance1.type=ajp13
worker.instance1.lbfactor=1
worker.instance1.fail_on_status=-404,-500,503,504

worker.instance2.port=8109
worker.instance2.maintain=60
worker.instance2.host=localhost
worker.instance2.type=ajp13
worker.instance2.lbfactor=1
worker.instance2.fail_on_status=-404,-500,503,504

worker.instance3.port=8209
worker.instance3.maintain=60
worker.instance3.host=localhost
worker.instance3.type=ajp13
worker.instance3.lbfactor=1
worker.instance3.fail_on_status=-404,-500,503,504

# Load-balancing behaviour
worker.loadbalancer.type=lb
worker.loadbalancer.max_reply_timeouts=5000
#<--5초 이상의 요청은 서버의 부하를 고려해 에러(504) 처리한다.
worker.loadbalancer.balance_workers=instance1,instance2,instance3
worker.loadbalancer.method=Session
worker.loadbalancer.sticky_session=1

worker.status.type=status
#<-- jkstatus 모니터링용

Tomcat Connector와 Apache 연결 확인

  • startup 스크립트
vim /app/tomcat/instance1/bin/startup-instance1.sh
#!/usr/bin/env bash

# Start user check
if [ "$USER" != "tuser" ]; then
    echo "You must start the script with the 'tuser' account. Another user is denied."
    exit 1
fi

# Options
SERVER_NAME=instance1
export CATALINA_BASE="/app/tomcat/$SERVER_NAME"
export CATALINA_HOME="/app/tomcat/$SERVER_NAME"
export JAVA_HOME="/app/java"

# Java options
export JAVA_OPTS="-Djava.awt.headless=true -server -Xms2048m -Xmx2048m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m -XX:MaxPermSize=512m -XX:+DisableExplicitGC -Dspring.profiles.active=real $JAVA_OPTS"
export JAVA_OPTS="$JAVA_OPTS -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$CATALINA_BASE/logs/heapdump/instance1/"
export JAVA_OPTS="$JAVA_OPTS -verbose:gc -Xloggc:$CATALINA_BASE/logs/gc/$SERVER_NAME_gc-$(date +%Y-%m-%d).log -XX:+PrintGCDetails -XX:+PrintGCDateStamps"

# Catalina options
export CATALINA_OPTS="-Denv=product -Denv.servername=$SERVER_NAME $CATALINA_OPTS"

# Run check
PP=$(ps -ef | grep java | grep "$SERVER_NAME" | wc -l)
Ppid=$(ps -ef | grep java | grep "$SERVER_NAME" | grep -v 'grep' | awk '{print $2}')

if [ "$PP" -eq 1 ]; then
    echo "###########################################################################"
    echo "###########################################################################"
    echo "### This Apache Tomcat is already running!! PID - $Ppid -, please shutdown first.###"
    echo "###########################################################################"
    echo "###########################################################################"
else
    echo "########################################"
    echo "########################################"
    echo "### Start $SERVER_NAME Apache Tomcat ###"
    $CATALINA_BASE/bin/startup.sh
    echo "### Please wait.. Apache Tomcat is running now. ###"
    echo "########################################"
    echo "########################################"
    sleep 10
    echo "--------------tail----------------------"
    tail "$CATALINA_BASE/logs/catalina.out"
    echo "--------------tail end------------------"
    echo "###########################################################################"
    echo "###########################################################################"
    echo "### Start script is finished. ###"
    echo "###########################################################################"
    echo "###########################################################################"
fi
  • shutdown 스크립트
vim /app/tomcat/instance1/bin/shutdown-instance1.sh
#!/usr/bin/env bash

# Start user check
if [ "$USER" != "tuser" ]; then
    echo "You must start the script with the 'tuser' account. Another user is denied."
    exit 1
fi

# Options
SERVER_NAME=instance1
export CATALINA_BASE=/app/tomcat/$SERVER_NAME
export CATALINA_HOME=/app/tomcat/$SERVER_NAME
export JAVA_HOME=/app/java

# Shutdown
cd "$CATALINA_HOME/bin"
./shutdown.sh
sleep 10

# Run check
PP="$(ps -ef | grep java | grep "$SERVER_NAME" | wc -l)"
Ppid=$(ps -ef | grep java | grep "$SERVER_NAME" | grep -v 'grep' | awk '{print $2}')

if [ "$PP" -eq 1 ]; then
    echo "########################################################################"
    echo "########################################################################"
    echo "### This Apache Tomcat is still running!! PID - $Ppid -, forcing kill. ###"
    echo "########################################################################"
    echo "########################################################################"
    
    kill -9 "$Ppid"
    
    BB="$(ps -ef | grep java | grep "$SERVER_NAME" | wc -l)"
    Bpid=$(ps -ef | grep java | grep "$SERVER_NAME" | grep -v 'grep' | awk '{print $2}')

    if [ "$BB" -eq 1 ]; then
        echo "##############################################################"
        echo "##############################################################"
        echo "### Sorry, Apache Tomcat does not shut down!! PID - $Bpid - ###"
        echo "##############################################################"
        echo "##############################################################"
    fi
else
    echo "--------------tail----------------------"
    tail "$CATALINA_BASE/logs/catalina.out"
    echo "--------------tail end------------------"
    
    echo "#################################################################"
    echo "#################################################################"
    echo "### $SERVER_NAME Apache Tomcat has been successfully shut down.###"
    echo "#################################################################"
    echo "#################################################################"
fi
  • 인스턴스 Port 할당
grep port= /app/tomcat/instance[1-3]/conf/server.xml
$ grep port= /app/tomcat/instance[1-3]/conf/server.xml
/app/tomcat/instance1/conf/server.xml:<Server port="8005" shutdown="SHUTDOWN">
/app/tomcat/instance1/conf/server.xml:    <Connector port="8080" protocol="HTTP/1.1"
/app/tomcat/instance1/conf/server.xml:    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
/app/tomcat/instance1/conf/server.xml:    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="EUC-KR" />
/app/tomcat/instance2/conf/server.xml:<Server port="8105" shutdown="SHUTDOWN">
/app/tomcat/instance2/conf/server.xml:    <Connector port="8180" protocol="HTTP/1.1"
/app/tomcat/instance2/conf/server.xml:    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
/app/tomcat/instance2/conf/server.xml:    <Connector port="8109" protocol="AJP/1.3" redirectPort="8443" URIEncoding="EUC-KR" />
/app/tomcat/instance3/conf/server.xml:<Server port="8205" shutdown="SHUTDOWN">
/app/tomcat/instance3/conf/server.xml:    <Connector port="8280" protocol="HTTP/1.1"
/app/tomcat/instance3/conf/server.xml:    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
/app/tomcat/instance3/conf/server.xml:    <Connector port="8209" protocol="AJP/1.3" redirectPort="8443" URIEncoding="EUC-KR" />

JSP 테스트 페이지

vim /app/tomcat/instance1/webapps/ROOT/test2.jsp
<%@ page
        language="java"
        contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"
%><?xml version="1.0" encoding="utf-8"?>
<% request.setCharacterEncoding("utf-8"); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head><title>JSP TEST</title></head>
<body>
<pre>
클래스경로: <%=System.getProperty("java.class.path",".")%>
라이브러리경로: <%=System.getProperty("java.library.path", ".")%>
파라메터: <%=request.getParameter("param")%> ('한글'이라고 표기되어야 함)
파일인코딩: <%=System.getProperty("file.encoding")%>
시스템캐릭터셋: <%=java.nio.charset.Charset.defaultCharset().name()%>
페이지캐릭터셋: <%=response.getCharacterEncoding()%>
서블릿 경로: <%=application.getRealPath("")%>
</pre>
</body>
</html>

 

startup-instance1.sh.txt.txt
0.00MB
shutdown-instance1.sh.txt.txt
0.00MB
test.jsp.txt.txt
0.00MB

 

728x90
반응형