##############################################
######### RockeyOS 9.5 Spring Boot 설치 ######
##############################################
# 스크립트는 반드시 관리자 권한에서 실행해야 한다.
# openssl 버전 확인 => TLS 1.3 지원
openssl version
dnf -y install expat-devel
# 시스템 업데이트
sudo dnf -y update
sudo dnf -y install wget unzip mc git nmap curl
# SSL 관련 패키지 설치 확인
sudo dnf -y install openssl openssl-devel
####################################################################################
# 방화벽 데몬 시작
systemctl start firewalld
# 서버 부팅 시 firewalld 데몬 자동 시작 설정
systemctl enable firewalld
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --permanent --add-service=mysql
firewall-cmd --permanent --zone=public --add-port=3306/tcp
firewall-cmd --permanent --zone=public --add-port=8080/tcp
firewall-cmd --permanent --zone=public --add-port=3000/tcp
firewall-cmd --reload
firewall-cmd --list-all
####################################################################################
# tomcat 배포 사전 조건
# 1. JAVA가 설치되어 있어야 한다. (톰캣 9는 자바 11 이상이 필요)
# jar 파일은 내부 톰캣이 있기에 따로 톰캣을 설치하지 않아도 된다.
####################################################################################
# 1. 기본 패키지 업데이트
sudo dnf update -y
# 2. Java 17 버전 설치
sudo dnf install -y java-17-openjdk java-17-openjdk-devel
java -version
# 3. Java 환경 변수 설정
echo "export JAVA_HOME=/usr/lib/jvm/java-17-openjdk" | sudo tee -a /etc/profile.d/java.sh
echo "export PATH=\$JAVA_HOME/bin:\$PATH" | sudo tee -a /etc/profile.d/java.sh
source /etc/profile.d/java.sh
# 여러개 설치되어 있는 자바 버전 선택하기
sudo alternatives --config java
# 4. Spring Boot 실행
# 아래 명령어를 수행하여 실행되는 메시지를 직접 육안으로 확인한다.
java -jar <jar파일명>
java -Xshare:off -jar /home/spring/SpringJWT/SpringJWT-0.0.1-SNAPSHOT.jar
# 5. Spring Boot 애플리케이션 실행
# 애플리케이션 실행 (백그라운드)
# 노트북에서 빌드(build)한 파일을 리눅스 서버로 업로드하고 아래와 같이 실행한다.
nohup java -Xshare:off -jar /home/spring/SpringJWT/SpringJWT-0.0.1-SNAPSHOT.jar --server.port=8080 > /var/log/springjwt.log 2>&1 &
# 필요한 Apache 모듈 활성화
sudo systemctl restart httpd
####################################################################################
# 프로세스 확인
ps aux | grep SpringJWT
# 프로세스 종료 (필요 시)
kill $(ps aux | grep 'SpringJWT' | awk '{print $2}')
# 종료 프로세스 작성
vi /usr/local/bin/kill_8080.sh
#!/bin/bash
# 8080 포트 사용 중인 프로세스 확인
PID=$(lsof -i :8080 -t)
if [ -n "$PID" ]; then
echo "8080 포트에서 실행 중인 프로세스(PID: $PID) 종료 중..."
kill $PID
sleep 2
# 프로세스가 종료되지 않았으면 강제 종료
if ps -p $PID > /dev/null; then
echo "⚠️ 정상 종료되지 않아 강제 종료 진행..."
kill -9 $PID
fi
echo "8080 포트 프로세스 종료 완료."
else
echo "ℹ️ 8080 포트에서 실행 중인 프로세스가 없습니다."
fi
# 저장(wq)하고 빠져나온다.
# 실행권한 부여
sudo chmod +x /usr/local/bin/kill_8080.sh
# 스크립트 실행
sudo /usr/local/bin/kill_8080.sh
####################################################################################
# systemd 서비스 등록 (서버 부팅 후 자동 실행)
vi /etc/systemd/system/springjwt.service
[Unit]
Description=Spring Boot Application - SpringJWT
After=network.target
[Service]
User=apache
Group=apache
ExecStart=/usr/bin/java -Xshare:off -jar /home/spring/SpringJWT/SpringJWT-0.0.1-SNAPSHOT.jar
SuccessExitStatus=143
Restart=always
RestartSec=5
StandardOutput=file:/var/log/springjwt.log
StandardError=file:/var/log/springjwt.log
[Install]
WantedBy=multi-user.target
#저장(wq)하고 빠져나온다.
# systemd 데몬 리로드
sudo systemctl daemon-reload
# 서비스 등록 및 실행
sudo systemctl enable springjwt
sudo systemctl start springjwt
# 실행 상태 확인
sudo systemctl status springjwt
# 서비스 중지 (필요 시)
sudo systemctl stop springjwt
# 실행 로그 확인
cat /var/log/springjwt.log
####################################################################################