infra/기타

WEB/WAS 분리 셋팅

jjuni_96 2024. 1. 30. 16:35
728x90

 

작업 환경

- java8, tomcat8, springboot, thymleaf

 

 

Apache 설치

1. gcc, c++ 설치

$ yum install -y gcc gcc-c++ mod_ssl

 

2. tomcat-connector.tar 업로드 및 압축 해제

tomcat-connectors-1.2.49-src.tar.gz
1.62MB

 

 

3. Apache 설치

$ yum insatll -y httpd
$ yum install -y httpd-devl
$ yum install perl

 

4. mod_jk.so 파일 생성을 위해 apxs 조회

$ which apxs
=> /usr/bin/apxs

 

5. mod_jk.so 파일 생성

# tomcat-connector > native 폴더로 이동 후 작업 진행

$./configure --with-apxs=/usr/bin/apxs
$ ake
$ ake install

 

6. httpd.conf 수정

$ vi /etc/httpd/conf/httpd.conf

#61번줄 - 아래와 같이 추가
LoadModule vhost_alias_module modules/mod_vhost_alias.so
LoadModule jk_module modules/mod_jk.so

#103번 - 주석 및 아래와같이 수정
# 도메인에 www는 무조건 추가를 해줘야함!!! 
ServerName www.testtest.slowtuttle.co.kr(http://www.testtest.slowtuttle.co.kr/)

#112번 - 접근 권한 수정
<Directory />
AllowOverride none
Require all granted
</Directory>

#가장 마지맥 - 아래와같이 추가
Include conf/extra/httpd-vhosts.conf
Include conf/extra/httpd-modjk.conf

 

7. uriworkermap.properties 생성

$ vi /etc/httpd/conf/uriworkermap.properties

# /docs 경로가 들어오면 workers.properties에 정의한
# worker1 톰캣인스턴스로 매핑
# 설치된 톰캣의 도큐먼트 페이지로 이동
/*=tomcat1

 

8. workers.properties 생성

$ vi /etc/httpd/conf/workers.properties

worker.list=tomcat1

worker.tomcat1.type=ajp13
worker.tomcat1.host=192.168.56.112 (tomcat 서버 IP)
worker.tomcat1.port=8009
#worker.tomcat1.lbfactor=1

 

9. 기타 설정파일 폴더 생성

$ mkdir /etc/httpd/conf/extra

 

10. httpd-modjk.conf 파일 생성

$ vi /etc/httpd/conf/extra/httpd-modjk.conf

<IfModule jk_module>
   JkWorkersFile /etc/httpd/conf/workers.properties
   JkLogFile /etc/httpd/mod_jk.log
   JkLogLevel info
   JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
   JkShmFile /etc/httpd/logs/mod_jk.shm

   JkMountFile /etc/httpd/conf/uriworkermap.properties
   JkMount /* tomcat1
</IfModule>

 

11. httpd-vhosts.conf 파일 생성

$ vi /etc/httpd/conf/extra/httpd-vhosts.conf
	
# Virtual Hosts
#
# Required modules: mod_log_config
	
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.4/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
    ServerName www.testtest.slowtuttle.co.kr

    RewriteEngine on
#    RewriteCond ${HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

</VirtualHost>

<VirtualHost *:443>
    ServerName testtest.slowtuttle.co.kr

    SSLEngine on
    SSLCertificateFile /etc/httpd/conf/SSL/인증서.pem
    SSLCertificateKeyFile /etc/httpd/conf/SSL/Private.key
    JkMount /* tomcat1
</VirtualHost>

 

12. 인증서파일 업로드

업로드 경로 : /etc/httpd/conf/SSL

 

13. 방화벽 open 및 reload

$ firewall-cmd --permanent --zone=public --add-port=8080/tcp --add-port=80/tcp --add-port=443/tcp
$ firewall-cmd --reload

 

 

14. selinux 해제

$ setenforce 0

 

15. 재기동

$ systemctl restart httpd

 

 

 

Tomcat 설정 파일 수정

$ vi <톰캣설치경로>/conf/server.xml

...
   <Connector port="8009" protocol="AJP/1.3" address="0.0.0.0" redirectPort="8443" URIEncoding="UTF-8" secretRequired="false" />
...

 

 

 

여기까지 작업하면 아래와 같은 구성으로 셋팅되어있음

 

 

 

static 경로 분리

1. httpd-vhosts.conf 설정파일 수정

$ vi /etc/httpd/conf/extra/httpd-vhosts.conf

...
<VirtualHost *:443>
    ServerName testtest.hiqri.ai

    SSLEngine on
    SSLCertificateFile /etc/httpd/conf/SSL/인증서.pem
    SSLCertificateKeyFile /etc/httpd/conf/SSL/Private.key

    # 아래 경로로 들어오면 apache 서버의 하위 경로로 바꾸겠다~
    Alias "/css/" "/home/apache/static/css/" 
    Alias "/font/" "/home/apache/static/font/"
    Alias "/js/" "/home/apache/static/js/"
    Alias "/image/" "/home/apache/static/image/"

    # 모든 url에 대하여 tomcat으로 넘기겠다
    JkMount /* tomcat1
    # 다만 아래의 경로만 제외하고
    JkUnmount /css/* tomcat1
    JkUnmount /font/* tomcat1
    JkUnmount /js/* tomcat1
    JkUnmount /image/* tomcat1

</VirtualHost>
...

 

2. 재기동

$ systemctl restart httpd

 

3. tomcat build.gradle 수정

...
war {
    archiveBaseName = "seperateTest"
    archiveFileName = "ROOT.war"
    archiveVersion = "0.0.0"

    exclude ('**/static/**') // static 디렉토리 제외
}


sourceSets {
    main {
        resources {
            srcDirs = ['src/main/resources']
            exclude '**/static/**' // static 디렉토리 제외
        }
    }
}
...

=> ROOT.war  파일에 static 항목이 없어야 제대로 동작하는지 확인이 가능함!

      - ROOT/WEB-INF/classes/ 폴더 확인

 

 

여기까지 셋팅되면 아래처럼 정적파일 및 동적 파일 분리가 완성됩니다.

 

Trouble Shooting

1. 파일 내부 경로 설정

Client에서 Apache로 요청할때 httpd 설정파일에서 로컬 경로로 어떻게 변경해야하는지가 어려웠습니다.

거색 끝에 Alias를 사용해서 변경하는 방식으로 적용하였습니다.

Alias "/css/" "/home/apache/static/css/"

 

2. 파일 권한 문제

Apache 설정을 root 계정에서 진행했는데 /etc/httpd/conf/httpd.conf 파일을 확인해보면 아래와같이 설정되어있습니다.

 

User apache

Group apache

 

그러다보니 root 권한으로 정적파일을 올리면 permition denied 에러가 발생하게 됩니다.

Apache를 root 권한으로 실행시키면 해결이 될 것 같아서 root로 변경을 해보았는데 정상적으로 기동이 되지 않았습니다.

 

그래서  어쩔 수 없이 정적 파일의 권한을 아래와같이 apache로 변경하였습니다.

$ chown -R apache:apache <정적파일>

 

 

 

기타 파일 위치

1. 에러 및 로그 확인

에러 로그 위치

   - /etc/httpd/logs/error_log

 

접근 로그

   - /etc/httpd/logs/access_log

 

ssl 관련 로그

   - /etc/httpd/logs/ssl_access_log

   - /etc/httpd/logs/ ssl_error_log

   - /etc/httpd/logs/ ssl_request_log

 

 

2. 에러 확인 방법

1. access 로그를 확인하여 url이 어떻게 들어오는지 확인

2. error 로그를 확인하여 어떤 문제가 있는지 자세히 디버깅

 

 

 

TODO

현재 구조가 thymleaf로 되어있기때문에 *.html 파일은 분리시키지 못하였습니다...

다음에 기회가 된다면 다른 프레임워크로 확인해봐야할 것 같습니다.

728x90
반응형
LIST