프로그램 자료/Cent OS
[CentOS7] 사용자 계정으로 톰캣 서비스 등록하기(80, 443)
motolies
2019. 8. 6. 16:43
리눅스(CentOS7)에 설치할 웹 솔루션을 만들다보니 사용자 계정으로 서비스를 구동하고 싶다는 고객사가 있었다.
서비스로 등록할 서비스 파일 만들기
서비스 파일을 만들 때 가장 중요한거는 유저와 그룹, 환경변수를 동적으로 잡아주는 것 같다.
다음은 서비스파일을 만들기 전의 목업 파일이다.
motolies.org
#!/bin/bash
[Unit]
Description=motolies web service
After=network.target syslog.target
[Service]
Type=forking
Environment=JAVA_HOME=##motolies_path/openjdk1.8_zulu
User=##whoami
Group=##groups
ExecStart=##motolies_path/tomcat9/bin/startup.sh
ExecStop=##motolies_path/tomcat9/bin/shutdown.sh
Umask=0007
RestartSec=5
Restart=always
[Install]
WantedBy=multi-user.target
다음은 위의 목업파일(motolies.org)을 문자열치환하여 정상적인 서비스 파일로 만드는 배치파일이다.
motolies_setting
#!/bin/bash
# create service file
pwd=`pwd`
pwds=`echo $pwd | sed "s/\//925DF2DA/g"`
tuser=`whoami`
tgroup=`groups`
sed -e "s/##motolies_path/$pwds/g ; s/##whoami/$tuser/g ; s/##groups/$tgroup/g" motolies.org | sed "s/925DF2DA/\//g" > motolies.service
같은 폴더에 위 두 개의 파일을 넣어두고 ./motolies_setting 파일을 실행하면 motolies.service 라는 파일이 생성되게 된다.
서비스로 등록하는 배치파일 만들기
서비스 등록과 실행은 아래와 같은 배쉬를 만들고 실행하면 등록과 실행이 된다.
다만, 사용자 계정은 1000번 이하의 포트는 점유할 수 없게 되어있으므로 80포트로 들어오는 건 8080으로, 443으로 들어오는 건 8443으로 포워딩을 시켜주도록 했다.
motolies_install
#!/bin/bash
# service 등록
firewall-cmd --zone=public --add-forward-port=port=80:proto=tcp:toport=8080 --permanent
firewall-cmd --zone=public --add-forward-port=port=443:proto=tcp:toport=8443 --permanent
firewall-cmd --reload
chmod -R 775 *
cp -f motolies.service /etc/systemd/system/motolies.service
systemctl enable /etc/systemd/system/motolies.service
systemctl start motolies.service
톰캣 설정방법
톰캣의 경우도 설정이 필요했는데, 다음과 같이 proxyPort라는 attr를 추가해주면 된다.
server.xml
<Connector port="8080" proxyPort="80"
<Connector port="8443" proxyPort="443"