스프링 오픈소스 프로젝트 중, petclinic을 AWS로 구축하는 전체 과정 중,
apache - tomcat을 mod proxy로 연동하는 방법에 대해 소개하겠다.
1. WEB 설정
1. Apache 수동 설치
1. GCC 컴파일러 설치
yum install -y gcc gcc-c++ expat-devel pcre-devel
2. Apache 설치
# 신규 disk에 apache 설치
cd /data
# apr1.6, apr-util1.6, pcre8.4*, httpd2.4* 다운로드
wget http://mirror.apache-kr.org/apr/apr-1.6.5.tar.gz --no-check-certificate
wget http://mirror.apache-kr.org/apr/apr-util-1.6.3.tar.gz --no-check-certificate
wget https://sourceforge.net/projects/pcre/files/pcre/8.45/pcre-8.45.tar.gz --no-check-certificate
wget https://dlcdn.apache.org/httpd/httpd-2.4.57.tar.gz --no-check-certificate
# 압축파일 해제
tar zxvf apr-1.6.5.tar.gz
tar zxvf apr-util-1.6.3.tar.gz
tar zxvf pcre-8.45.tar.gz
tar zxvf httpd-2.4.57.tar.gz
# 다운받은 압축파일 삭제
rm -r *.gz
# apr 설치
cd /data/apr-1.6.5
./configure --prefix=/data/apr-1.6.5
make && make install
# apr-util 설치
cd /data/apr-util-1.6.3/
./configure --prefix=/data/apr-util-1.6.3 --with-apr=/data/apr-1.6.5
make && make install
# pcre 설치
cd /data/pcre-8.45
./configure --prefix=/data/apr-util-1.6.3 --with-apr=/data/apr-1.6.5
make && make install
# apache(httpd) 설치
cd /data/httpd-2.4.57/
./configure --prefix=/data/apache --enable-modules=most --enable-mods-shared=all --enable-so --with-apr=/data/apr-1.6.5 --with-apr-util=/data/apr-util-1.6.3
make && make install
# ServerName 변경
vi /data/apache/conf/httpd.conf
## file 206번째 줄 수정
ServerName 127.0.0.1:80
# apache 실행
/data/apache/bin/apachectl -k start
# apache 실행 확인
curl 127.0.0.1
## 출력결과 : <html><body><h1>It works!</h1></body></html>
# 80번 포트 동작여부 확인
netstat -antp | grep httpd
# index.html 변경
vi /data/apache/htdocs/index.html
- AutoScaling을 위해 index.html 변경
<!DOCTYPE html>
<html>
<head>
<title>현재 IP 주소</title>
</head>
<body>
<h1>현재 IP 주소: <span id="ip-address">Loading...</span></h1>
<script>
// 서버로부터 IP 주소를 가져오는 함수
function getIPAddress() {
fetch('https://api.ipify.org?format=json')
.then(response => response.json())
.then(data => {
const ipAddress = data.ip;
document.getElementById('ip-address').textContent = ipAddress;
})
.catch(error => {
console.error('IP 주소를 가져오는 도중 오류가 발생했습니다.', error);
document.getElementById('ip-address').textContent = 'IP 주소를 가져오지 못했습니다.';
});
}
// 페이지가 로드되면 IP 주소를 가져오도록 호출
window.addEventListener('load', getIPAddress);
</script>
</body>
</html>
3. Systemctl에 Apache 등록
vi /etc/systemd/system/apache.service
# apache.service 파일 내용
[Unit]
Description=The Apache HTTP Server
[Service]
Type=forking
# EnvironmentFile=/data/apache/bin/envvars
PIDFile=/data/apache/logs/httpd.pid
ExecStart=/data/apache/bin/apachectl start
ExecReload=/data/apache/bin/apachectl graceful
ExecStop=/data/apacher/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
# 데몬 재시작 후 적용
systemctl daemon-reload
systemctl enable --now apache
# apache 실행 확인
systemctl status apache.service
2. WAS 설정
1. JDK 설치
1. openJDK 다운로드
cd /data
# openJDK 18.0.2 다운로드
wget https://download.java.net/java/GA/jdk18.0.2/f6ad4b4450fd4d298113270ec84f30ee/9/GPL/openjdk-18.0.2_linux-x64_bin.tar.gz
# 파일 압축 해제
tar xvzf openjdk-18.0.2_linux-x64_bin.tar.gz
# 설치 다운로드 파일 삭제
rm -r *.gz
- 만약, 압축 파일 해제 후 폴더가 보이지 않는다면?
- Server를 reboot해볼 것!!
2. 환경 변수 설정
vi /etc/profile
## shift + G 로 마지막줄 이동
## java 관련 환경변수 추가
export JAVA_HOME=/data/jdk-18.0.2
export PATH=$PATH:$JAVA_HOME/bin
# 변경사항 적용
source /etc/profile
# 경로 확인
echo $JAVA_HOME
echo $PATH
2. pet-clinic 설치
1. pet-clinic 설치 및 빌드
cd /data
# petclinic 폴더 생성
mkdir petclinic
# petclinic 폴더 이동
cd petclinic
# git에서 petclinic 다운로드
git clone https://github.com/spring-projects/spring-petclinic.git
# 빌드를 위해 경로 이동
cd spring-petclinic
# petclinic 설치
./mvnw package
# petclinic 실행
java -jar target/*.jar
## 확인 후, crtl+c 실행 종료
- 만약 petclinic이 제대로 설치되지 않는다면
# 빌드 파일 cleaning
./mvnw clean
# 재설치시 test없이 빠르게 build
./mvnw package -DskipTests
2. pet-clinic 실행 확인
# 백그라운드로 실행
java -jar target/*.jar &
# 작동 포트 확인
netstat -antp | grep LISTEN
# curl로 접속 확인
curl http://127.0.0.1:8080
3. Systemctl에 petclinic 등록
추후 Pet clinic rebuild시 충돌 발생
>> 접속 확인이 잘 된다면 굳이 이 단계에서 등록하지 않아도 됨!
- 이미 pet clinic이 동작중이라면 error가 날 수 있음
# 현재 실행중인 프로세스 확인
ps
# java가 실행중이라면 PID를 kill 해야 함
kill PID
## 강제종료 옵션은 -9
vi /usr/lib/systemd/system/petclinic.service
# petclinic.service 파일 내용
[Unit]
Description=Service Description
After=syslog.target network.target postgresql.service
[Service]
# petclinic의 target 디렉터리 경로
WorkingDirectory=/data/petclinic/spring-petclinic/target
# 실행 경로
ExecStart=/data/jdk-18.0.2/bin/java -jar -Dspring.profiles.active="mysql" spring-petclinic-3.1.0-SNAPSHOT.jar &
Type=simple
User=root
Group=root
[Install]
WantedBy=multi-user.target
Alias=SpringBoot-Serv.service
# 데몬 재시작 후 적용
systemctl daemon-reload
systemctl enable --now petclinic
# apache 실행 확인
systemctl status apache.service
3. WEB-WAS-DB 연동
1. WEB server 설정 - mod_proxy
1. httpd.conf 수정
vi /data/apache/conf/httpd.conf
## file의 123, 126, 158번째 줄 주석 해제
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule negotiation_module modules/mod_negotiation.so
## file의 213 ~ 218번째 줄 변경
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
# <Directory> blocks below.
#
<Directory />
AllowOverride none
# Require all denied
Require all granted
</Directory>
## file의 490번째 줄 주석 해제
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
2. httpd-vhosts.conf 수정
vi /data/apache/conf/extra/httpd-vhosts.conf
## file에 추가
<VirtualHost *:80>
ServerName localhost
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://<<IN-LB-DNS>>:8080/
ProxyPassReverse / http://<<IN-LB-DNS>>:8080/
<Location />
Order allow,deny
Allow from all
</Location>
</VirtualHost>
3. 연동 확인
systemctl daemon-reload
systemctl enable --now apache
systemctl status apache
curl http://127.0.0.1
systemctl restart apache
- 해당 부분까지 진행 완료하면 EX-LB의 DNS로 petclinic에 접속 가능
WEB-WAS의 기본적인 연동이 완료되었으므로 IMG 생성 필수!
2. WAS Server 설정
1. application-mysql.properties 수정
vi /data/petclinic/spring-petclinic/src/main/resources/application-mysql.properties
# database init, supports mysql too
database=mysql
# jdbc:mysql://<RDS endpoint>>/<<DB 초기이름>>
spring.datasource.url=${MYSQL_URL:jdbc:mysql://******.ap-northeast-2.rds.amazonaws.com/petclinic}
# MYSQL_USER:<<
spring.datasource.username=${MYSQL_USER:******}
spring.datasource.password=${MYSQL_PASS:******}
# SQL is written to be idempotent so this is safe
spring.sql.init.mode=always
2. petclinic rebuild
cd /data/petclinic/spring-petclinic
# 기존 빌드파일 제거 후 재설
./mvnw clean
./mvnw package
## 빠른 빌드를 원한다면 test없이 package 설치
./mvnw package -DskipTests=true
- 만약 JAVA_HOME을 찾을 수 없다는 error가 발생한다면
echo $JAVA_HOME source /etc/profile ## 해당 명령어로 JAVA_HOME을 선언한 다시 clean 진행
3. DB 연동 후 petclinic 재실행
cd /data/petclinic/spring-petclinic
# 백그라운드에서 petclinic 실행
java -jar -Dspring.profiles.active="mysql" target/*.jar &
# 작동 포트 확인
netstat -antp | grep LISTEN
---------------------------------------------------
./mvnw spring-boot:run -Dspring-boot.run.profiles=mysql
4. 연동 확인
# DB 확인을 위해 mySQL 설치
yum install -y mysql
# db 접속
mysql -h <RDS 엔드포인트> -u <마스터 사용자이름> -p
MySQL [(none)]> show databases;
MySQL [(none)]> use petclinic;
MySQL [petclinic]> select * from owners;
이후의 과정들은 간략하게 설명하겠다. (대부분 콘솔 상에서 작업할 것이므로)
[참고] ASG 및 LB 생성 과정
- WAS의 대상 그룹 생성
- WAS 오토스케일링 그룹 생성
- WAS 로드밸런서 생성
- WEB의 httpd-vhosts.conf 파일의 IN-LB DNS 변경
- WEB의 IMG 신규 생성
- WEB 대상 그룹 생성
- WEB 오토스케일링 그룹 생성
- WEB 로드밸런서 생성
* 주의 사항
# WAS ASG의 User Data
#!/bin/bash
sudo su
source /etc/profile
cd /data/petclinic/spring-petclinic
java -jar -Dspring.profiles.active="mysql" target/*.jar &
모든 설정이 끝나고 route53에 도메인을 까지 등록해준다면?!
브라우저를 통해 접속이 가능해진다!!
728x90
'Study > AWS' 카테고리의 다른 글
[AWS] Lambda를 활용해 CI/CD trigger 만들기 (1) | 2024.04.22 |
---|---|
[AWS] CloudWatch 지표에 대한 레퍼런스 정리 (0) | 2024.04.01 |
[AWS] 데이터베이스 종류 및 용어 정리 (0) | 2024.04.01 |
[AWS] AWS 기본 개념 정리 (0) | 2024.04.01 |
[AWS] 클라우드 보안 (1) | 2024.01.22 |
댓글