[DevOps] Ubuntu 22.04 LTS Docker Container Tomcat Web Application Server ์„ค์น˜

2022. 9. 5. 18:01ใ†System ์ž‘์—…์‹ค/DevOps

728x90
๋ฐ˜์‘ํ˜•

 

 



 

 

๐Ÿš€ Tomcat ์„ค์น˜ํ•˜๊ธฐ

    ๐Ÿ”ฝ  ๊ฐœ์š”

        ๐Ÿ“ฆ ์†Œ๊ฐœ

ํ˜„์žฌ Tomcat์€ 10 Version๊นŒ์ง€ ๋‚˜์˜จ ์ƒํƒœ์—์š”.

ํ•˜์ง€๋งŒ, ์ฃผ๋‹ˆํ•˜๋ž‘์€ Tomcat 9 Version์„ ์ด์šฉํ•  ๊ฒƒ์ธ๋ฐ, 10 Version ๋ถ€ํ„ฐ Java EE Package ์ƒ๋‹น ๋‚ด์šฉ์ด Jakarta EE๋กœ ๋ณ€๊ฒฝ๋˜๋ฉด์„œ ์„ค์ • ๋“ฑ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ๋งŽ์ด ๋ถ€์กฑํ•œ ์ƒํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์ด์—์š”.

๋˜ํ•œ ์ฃผ๋‹ˆํ•˜๋ž‘์€ Docker์— Ubuntu 22.04 LTS Container๋ฅผ ์˜ฌ๋ ค Tomcat์„ ์„ค์น˜ํ•ด ๋ณด๋ ค ํ•ฉ๋‹ˆ๋‹ค.

 

 

 

    ๐Ÿ”ฝ  JAVA ์„ค์น˜

        ๐Ÿ“ฆ JAVA SE ์„ค์น˜

Tomcat์„ ์„ค์น˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” JAVA SE 8 ์ด์ƒ์ด ์„ค์น˜ ๋˜์–ด ์žˆ์–ด์•ผ ํ•ด์š”.

๋จผ์ € OpenJDK 11์„ ์„ค์น˜ํ•ด ๋ณด๋„๋ก ํ• ๊ฒŒ์š”.


apt-get update && apt-get upgrade -y

 

 

apt-get install -y openjdk-11-jdk
๋ฐ˜์‘ํ˜•


์œ„์˜ ๋ช…๋ น์–ด๋กœ ์„ค์น˜๋ฅผ ํ•ด ์ฃผ์„ธ์š”.

๊ทธ๋ฆฌ๊ณ , ์ข€ ๋” ์˜ˆ์œ ํ„ฐ๋ฏธ๋„ ์ฐฝ์—์„œ ์ž‘์—…ํ•˜๊ธฐ ์œ„ํ•ด docker์— CLI๋กœ ๋ถ™์–ด์„œ ์ž‘์—…์„ ํ•ด ๋ณผ๊ฒŒ์š”.


sudo docker ps


๋จผ์ € Docker๊ฐ€ ๊ธฐ๋™์ค‘์ธ Host Server์—์„œ ์œ„์˜ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ๊ธฐ๋™์ค‘์ธ Container๋ฅผ ํ™•์ธํ•ด ์ค๋‹ˆ๋‹ค.


sudo docker exec -it [Container ID or Names] bash


bash Shell์„ ์ด์šฉํ•ด์„œ ํ•ด๋‹น Container๋ฅผ Handlingํ•˜๊ธฐ ์œ„ํ•ด ์œ„์™€ ๊ฐ™์ด ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ด์„œ ํ•ด๋‹น Container์— ์ ‘์†ํ•ฉ๋‹ˆ๋‹ค.



java -version


JAVA๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์„ค์น˜๊ฐ€ ๋˜์—ˆ์–ด์š”.

 

 

 

 

    ๐Ÿ”ฝ  ์ƒˆ๋กœ์šด ์‚ฌ์šฉ์ž ์ƒ์„ฑ

        ๐Ÿ“ฆ tomcat User ์ƒ์„ฑ

root ์‚ฌ์šฉ์ž๋กœ tomcat์„ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์€ ๋งค์šฐ ์œ„ํ—˜ํ•ฉ๋‹ˆ๋‹ค.

๋•Œ๋ฌธ์— ๋ณด์•ˆ์„ ์œ„ํ•ด tomcat์„ ์ „์šฉ User๋ฅผ ๋งŒ๋“ค์–ด ์ค„ ๊ฒƒ์ด์—์š”.

 

sudo useradd -m -U -d /opt/tomcat -s /bin/false tomcat
728x90


/bin/false๋Š” ํ•ด๋‹น ๊ณ„์ •์œผ๋กœ ์™ธ๋ถ€์—์„œ Login์ด ๋˜์ง€ ์•Š๋„๋ก ํ•˜๊ธฐ ์œ„ํ•œ ์„ค์ •์ด์—์š”.



ํ˜„์žฌ ์ฃผ๋‹ˆํ•˜๋ž‘์€ root ๊ณ„์ •์œผ๋กœ Login ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— sudu ๋ช…๋ น์–ด๋Š” ์ œ์™ธํ•˜์˜€์Šต๋‹ˆ๋‹ค.








    ๐Ÿ”ฝ  Tomcat ์„ค์น˜

        ๐Ÿ“ฆ tomcat Download

Tomcat Binary ๋ฐฐํฌํŒ์€ ์ด ๊ณณ์—์„œ ๋‚ด๋ ค ๋ฐ›์„ ์ˆ˜ ์žˆ์–ด์š”.


Tomcat 9. Version์—์„œ ์ตœ์‹  Version์€ 9.0.65๋„ค์š”. ์ด๊ฒƒ์„ ์„ค์น˜ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.65/bin/apache-tomcat-9.0.65.tar.gz -P /tmp



apt-get install -y wget

 

wget ๋ช…๋ น์–ด๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†๋‹ค๊ณ  ๋‚˜์˜ค๋ฉด ์œ„์™€ ๊ฐ™์ด wget์„ ์„ค์น˜ํ•ด ์ฃผ์„ธ์š”.





์œ„์™€ ๊ฐ™์ด ๋‚ด๋ ค๋ฐ›๊ธฐ๊ฐ€ ์™„๋ฃŒ ๋˜๋ฉด tar ์••์ถ•์„ ํ’€์–ด์ฃผ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.


sudo tar -xf /tmp/apache-tomcat-${VERSION}.tar.gz -C /opt/tomcat/

 


tmp์— ๋‚ด๋ ค๋ฐ›์€ tar File์„ ์••์ถ•์„ ํ•ด์ œํ•˜๋ฉด์„œ /opt/tomcat์œผ๋กœ ์ด๋™๋˜๊ฒŒ ํ•ด ์ฃผ์—ˆ์–ด์š”.



์ด๋ ‡๊ฒŒ ์••์ถ•์ด ํ’€๋ฆฐ File์ด ์กด์žฌํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.


๋‚˜์ค‘์— Tomcat์„ Upgradeํ•  ๋•Œ, ์ตœ์‹  Version ์••์ถ•์„ ์ถœ๊ณ  ํ•ด๋‹น Version์„ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ํ•˜๋„๋ก ์‹ฌ๋ณผ๋ฆญ ๋งํฌ๋ฅผ ๋ณ€๊ฒฝํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


sudo ln -s /opt/tomcat/apache-tomcat-${VERSION} /opt/tomcat/latest


์‹ฌ๋ณผ๋ฆญ ๋งํฌ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ๊ฑธ๋ฆฐ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.


์œ„์—์„œ ์ƒ์„ฑํ•œ tomcat User๋Š” tomcat ์„ค์น˜ Directory์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ๋‚ด์šฉ์„ ์„ค์ •ํ•ด ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

sudo chown -R tomcat: /opt/tomcat





ํ•ด๋‹น Directory์˜ Owner ์‚ฌ์šฉ์ž๊ฐ€ root์—์„œ tomcat์œผ๋กœ ๋ณ€๊ฒฝ๋œ ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.


์ด์ œ Tomcat ๋‚ด๋ถ€์˜ Shell Script bin ์‹คํ–‰์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ์„ค์ •ํ•ด ์ค„๊ฒŒ์š”.

sudo sh -c 'chmod +x /opt/tomcat/latest/bin/*.sh'





Shell Script๋ฅผ ์ด์šฉํ•˜์—ฌ Tomcat Server๋ฅผ ๊ธฐ๋™ํ•˜๊ณ , ์ค‘์ง€ํ•˜๋Š” ๋Œ€์‹  Service๋ฅผ ํ†ตํ•ด ์ž‘์—…ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด ์ค„๊ฒŒ์š”.

vim์„ ๋จผ์ € ์„ค์น˜ํ•˜๊ณ ,

apt-get install -y vim



apt-get install -y vim


tomcat.service๋ฅผ ๋งŒ๋“ค์–ด ์•„๋ž˜ ๋‚ด์šฉ์„ ๋„ฃ์–ด์ฃผ์„ธ์š”.

[Unit]
Description=Tomcat 9 servlet container
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom -Djava.awt.headless=true"
Environment="CATALINA_BASE=/opt/tomcat/latest"
Environment="CATALINA_HOME=/opt/tomcat/latest"
Environment="CATALINA_PID=/opt/tomcat/latest/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
ExecStart=/opt/tomcat/latest/bin/startup.sh
ExecStop=/opt/tomcat/latest/bin/shutdown.sh
[Install]
WantedBy=multi-user.target



/etc/systemd/system/tomcat.service

 

 

apt-get install -y systemctl

 

Deamon ๋“ฑ์„ ์žฌ ๊ธฐ๋™ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” systemctl์„ ์„ค์น˜ํ•ด ์ค๋‹ˆ๋‹ค.



Tomcat Server๋ฅผ ๊ธฐ๋™ํ•ด ์ฃผ์—ˆ์–ด์š”.



์ฃผ๋‹ˆํ•˜๋ž‘์€ ํ•ด๋‹น Container์— 8080 Port๊ฐ€ 1004๋กœ Port Mapping์„ ํ•ด ์ฃผ์—ˆ์–ด์š”.



๋งŒ์•ฝ ํ•ด๋‹น Server์— ๋ฐฉํ™”๋ฒฝ์ด ์„ค์น˜ ๋˜์–ด ์žˆ๋‹ค๋ฉด ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด Tomcat Port๊ฐ€ ์ ‘๊ทผ ํ—ˆ์šฉ๋˜๋„๋ก ํ•ด ์ฃผ์„ธ์š”.

sudo ufw allow [Tomcat Port Number]{8080}/tcp



/opt/tomcat/latest/webapps/manager/META-INF/context.xml


์œ„์˜ File์—์„œ ์ ‘์†ํ•  ์ˆ˜ ์žˆ๋Š” ์ •๋ณด๋ฅผ ๋ณ€๊ฒฝํ•ด ์ค„ ๊ฒƒ์ด์—์š”.


/opt/tomcat/latest/webapps/manager/META-INF/context.xml


ํ˜„์žฌ๋Š” 127.0.0.1 ์ฆ‰ Localhost์—์„œ๋งŒ ์ ‘์† ๊ฐ€๋Šฅํ•˜๋„๋ก ์ œ์•ฝ์ด ๊ฑธ๋ ค ์žˆ๋Š” ์ƒํƒœ์—์š”.

์ด ๋ถ€๋ถ„์„ ์ฃผ์„์ฒ˜๋ฆฌํ•ด์„œ ์–ด๋””์„œ๋“  ์ ‘์†์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ์„ค์ •ํ•ด ์ค„๊ฒŒ์š”.

/opt/tomcat/latest/webapps/manager/META-INF/context.xml


์œ„์™€ ๊ฐ™์ด ์„ค์ •ํ•ด ์ฃผ์—ˆ์–ด์š”.




์ด๋ฒˆ์—๋Š” Host-manager ๋ถ€๋ถ„์— ๋™์ผํ•œ ์„ค์ •์„ ํ•ด ์ค„๊ฒŒ์š”.

/opt/tomcat/latest/webapps/host-manager/META-INF/context.xml


์œ„์™€ ๊ฐ™์ด ์„ค์ •์„ ํ•ด ์ฃผ์—ˆ์–ด์š”.




์ด์ œ ํ•ด๋‹น Tomcat์— ๋Œ€ํ•œ ์‚ฌ์šฉ์ž๋ฅผ ๋งŒ๋“ค์–ด ์ค„๊ฒŒ์š”.



vim /opt/tomcat/latest/conf/tomcat-users.xml




์ด ๊ณณ์— tomcat ๊ด€๋ จ ์‚ฌ์šฉ์ž๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ์–ด์š”.



๋งจ ์•„๋ž˜ ๋ถ€๋ถ„์— ๋‚ด์šฉ์„ ์œ„์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•ด ์ฃผ์„ธ์š”.






๊ทธ๋Ÿฐ ๋’ค tomcat์„ ์žฌ๊ธฐ๋™ ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.




https://{tomcat IP or Domain}:Port ๋ฒˆํ˜ธ/manager/html

 

tomcat // tomcat


์œ„์˜ tomcat-user.xml์—์„œ ์ •์˜ํ•œ tomcat ๊ณ„์ •์œผ๋กœ ์ ‘์†ํ•ด ๋ณผ๊ฒŒ์š”.


https://{tomcat IP or Domain}:Port ๋ฒˆํ˜ธ/manager/html

 

์œ„์™€ ๊ฐ™์ด Manager Page์— ์ ‘์†ํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.


 







๐Ÿง ์ฐธ๊ณ  ์ž๋ฃŒ

https://linuxize.com/post/how-to-install-tomcat-9-on-ubuntu-20-04/

 

 

 

 

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•