AWS EC2 ์„œ๋ฒ„์— Java ํ”„๋กœ์ ํŠธ ๋ฐฐํฌ/์žฌ๋ฐฐํฌ

๋ธ”๋กœ๊ทธ ์˜ฎ๊ฒผ์Šต๋‹ˆ๋‹ค! ๐Ÿก’ integer.blog

ํšŒ์‚ฌ ๊ณ ๊ฐ์„ผํ„ฐ์šฉ ์นด์นด์˜คํ†ก ์ฑ—๋ด‡์„ ๋งŒ๋“ค์–ด AWS EC2 ์„œ๋ฒ„์— ๋ฐฐํฌํ•˜๊ธฐ ์œ„ํ•ด
๋ฐ•์žฌ์„ฑ๋‹˜ ์œ ํŠœ๋ธŒ ๊ฐ•์˜๋ฅผ ๋“ค์œผ๋ฉฐ ์‹ค์Šตํ•œ ๋‚ด์šฉ

๋ชฉ์ฐจ

1. AWS EC2 ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
2. ์ƒ์„ฑํ•œ ์ธ์Šคํ„ด์Šค(์„œ๋ฒ„)์— ์ ‘์†
3. Java ์„ค์น˜
4. Symbolic Link ์ถ”๊ฐ€
5. ํ™˜๊ฒฝ๋ณ€์ˆ˜์— PATH ์„ค์ •
6. Git ์„ค์น˜ ๋ฐ ํ”„๋กœ์ ํŠธ Clone
7. ๋นŒ๋“œ ๋ฐ ๋ฐฐํฌ
8. ์ˆ˜์ •๋œ ์†Œ์Šค์ฝ”๋“œ ๋ฐ˜์˜
9. ์‰˜์Šคํฌ๋ฆฝํŠธ๋กœ ๋ฐฐํฌ ์ž๋™ํ™”

1. AWS EC2 ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ

AWS ๊ณต์‹ ํ™ˆํŽ˜์ด์ง€์˜ ์„ค๋ช…์ด ์ œ์ผ ๊น”๋”ํ•˜๋‹ค.

2. ์ƒ์„ฑํ•œ ์ธ์Šคํ„ด์Šค(์„œ๋ฒ„)์— ์ ‘์†

  • ํผ๋ธ”๋ฆญ DNS๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์Šคํ„ด์Šค์— ์—ฐ๊ฒฐ
    $ ssh -i "chatbot-kakao.pem" ubuntu@ec2~~~.ap-northeast-2.compute.amazonaws.com
  • Private Key ํŒŒ์ผ์ด ๊ณต๊ฐœ์ ์œผ๋กœ ํ‘œ์‹œ๋˜์–ด ์žˆ์„ ๊ฒฝ์šฐ
    $ chmod 400 chatbot-kakao.pem ๋ช…๋ น ์ž…๋ ฅ
  • $ sudo locale-gen ko_KR.UTF-8 ํ˜น์€ $ sudo apt-get install language-pack-ko ๋ช…๋ น์œผ๋กœ ์ธ์ฝ”๋”ฉ

3. Java ์„ค์น˜

  • apt-get์œผ๋กœ ์„ค์น˜
    • $ apt-get install openjdk-8-jre-headless
  • wget์œผ๋กœ ์„ค์น˜ํ•  ๊ฒฝ์šฐ
    • $ wget๋ช…๋ น์–ด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํŠน์ • ์ฃผ์†Œ์— ์žˆ๋Š” ์ž๋ฐ” ํ”„๋กœ๊ทธ๋žจ์„ ๋‹ค์šด๋กœ๋“œ
    • ๊ตฌ๊ธ€์— jdk download๋กœ ๊ฒ€์ƒ‰ํ•˜์—ฌ ์˜ค๋ผํด์— ๋“ค์–ด๊ฐ„ ํ›„, linux x64 ๋‹ค์šด๋กœ๋“œ ํŒŒ์ผ์„ ๋งˆ์šฐ์Šค ์šฐํด๋ฆญ์œผ๋กœ ๋งํฌ์ฃผ์†Œ๋ณต์‚ฌ
    • $ wget ๋ณต์‚ฌํ•œ ๋งํฌ์ฃผ์†Œ ๋ช…๋ น ์ž…๋ ฅ
    • ํ•˜์ง€๋งŒ, ์˜ค๋ผํด ๋‹ค์šด๋กœ๋“œ ํŽ˜์ด์ง€์—์„œ ์•ฝ๊ด€๋™์˜ํ•œ ๊ฒƒ์„ ์ ์šฉํ•  ์ˆ˜ ์—†์—ˆ์œผ๋ฏ€๋กœ ๋‹ค์šด๋กœ๋“œ๊ฐ€ ์ œ๋Œ€๋กœ ๋˜์ง€ ์•Š๋Š”๋‹ค.
    • $ rm jdk~~~~~ ๋ช…๋ น์œผ๋กœ jdk ํŒŒ์ผ ์‚ญ์ œ ๋ฐ ls -al ๋ช…๋ น์œผ๋กœ ์‚ญ์ œ ํ™•์ธ
    • $ wget --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u181-b13/96a7b8442fe848ef90c96a2fad6ed6d1/jdk-8u181-linux-x64.tar.gz
  • ์„ค์น˜์™„๋ฃŒ ํ›„ $ ls -al๋กœ ์„ค์น˜ ํ™•์ธ
  • $ tar -xvf jdk ~~~ tar.gz ๋ช…๋ น์œผ๋กœ ์••์ถ• ํ•ด์ œ
  • /jdk/bin ๊ฒฝ๋กœ๋กœ ์ด๋™ํ•˜์—ฌ ./java -version์œผ๋กœ ์ž๋ฐ” ๋ฒ„์ „ ํ™•์ธ
  • ์•„๋ฌด ์œ„์น˜์—์„œ๋‚˜ ์ž๋ฐ” ํ”„๋กœ๊ทธ๋žจ์— ์ ‘๊ทผ์ด ์•ˆ๋˜๋ฏ€๋กœ ์‹ฌ๋ณผ๋ง ๋งํฌ ์„ค์ •๊ณผ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •์„ ํ•ด์•ผ ํ•œ๋‹ค.
  • $ ln -s jdk~~/ java(๋ณ„์นญ)
  • $ cd java ๋ช…๋ น์œผ๋กœ ๊ฒฝ๋กœ์— ์ ‘๊ทผ ๊ฐ€๋Šฅ

5. ํ™˜๊ฒฝ๋ณ€์ˆ˜์— PATH ์„ค์ •

  • $ cd ~
  • $ vi .bash_profile
  • PATH=$PATH:~/java/bin ์ž‘์„ฑํ•˜๊ณ  esc - :wq
  • $ source .bash_profile ๋ช…๋ น์œผ๋กœ ํ™˜๊ฒฝ๋ณ€์ˆ˜ ํŒŒ์ผ ๋ฆฌ๋กœ๋“œ
  • java -version

6. Git ์„ค์น˜ ๋ฐ ํ”„๋กœ์ ํŠธ Clone

  • $ git --version ์œผ๋กœ git ์„ค์น˜ ํ™•์ธ
  • ์„ค์น˜ ์•ˆ๋˜์–ด์žˆ์„ ๊ฒฝ์šฐ, $ sudo apt-get update ํ›„์— $ sudo apt-get install git ๋ช…๋ น์œผ๋กœ git ์„ค์น˜
  • $ git clone ์ €์žฅ์†Œ ์ฃผ์†Œ ๋ช…๋ น์œผ๋กœ clone

7. ๋นŒ๋“œ ๋ฐ ๋ฐฐํฌ

  • $ ./mvnw clean package = ๊ธฐ์กด์˜ ๊ฒฐ๊ณผ๋ฌผ์„ ๋‚ ๋ ค๋ฒ„๋ฆฌ๊ณ (clean) ๋ฐฐํฌํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ๋กœ ๋‹ค์‹œ ๋นŒ๋“œํ•˜๋Š”(package) ๋ช…๋ น ์ž…๋ ฅ
    • ๋งŒ์•ฝ ๊ฐ ๋ฐฐํฌ ๋ฒ„์ „์„ ๋‚จ๊ธฐ๊ณ  ์‹ถ๋‹ค๋ฉด clean ์—†์ด $ ./mvnw package
  • ๋นŒ๋“œ์˜ ๊ฒฐ๊ณผ๋ฌผ์€ target ๋””๋ ‰ํ† ๋ฆฌ์— ์œ„์น˜ํ•˜๋ฏ€๋กœ $ cd target ์œผ๋กœ ์ด๋™ํ•˜์—ฌ $ nohup java -jar ChatBotTest-0.0.1-SNAPSHOT.jar & ๋ช…๋ น์œผ๋กœ ์„œ๋ฒ„์— ๋„์šฐ๊ธฐ
    • ์„œ๋ฒ„์— ๋„์šฐ๋Š” ๋ฐ์— ์‹œ๊ฐ„์ด ๋„ˆ๋ฌด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋ฉด $ java -Djava.security.egd=file:/dev/./urandom -jar ChatBotTest-0.0.1-SNAPSHOT.jar
    • ํ„ฐ๋ฏธ๋„์„ ๋ถ„๋ฆฌํ•˜์ง€ ์•Š์œผ๋ฉด ์„œ๋ฒ„์— ์˜ฌ๋ฆฌ๊ณ ๋‚˜์„œ Ctrl+c๋กœ ๋ช…๋ น์–ด๋ฅผ ์ž‘์„ฑํ•˜๊ฒŒ๋˜๋ฉด ์„œ๋ฒ„๊ฐ€ ๊ฐ™์ด ์ข…๋ฃŒ๋˜๋ฏ€๋กœ ํ„ฐ๋ฏธ๋„์„ ๋ถ„๋ฆฌํ•ด์„œ ์„œ๋ฒ„์— ์˜ฌ๋ ค์•ผํ•œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด java -jar ๋ช…๋ น์˜ ๋งˆ์ง€๋ง‰์— & ๋ฅผ ๋„ฃ์–ด์„œ ํ„ฐ๋ฏธ๋„์„ ๋ถ„๋ฆฌํ•ด์•ผํ•œ๋‹ค.
    • nohup ๋ช…๋ น์„ ํ•˜๋Š” ์ด์œ  ์ฝ์–ด๋ณด๊ธฐ.
  • $ nohup java -jar ChatBotTest-0.0.1-SNAPSHOT.jar & ๋ช…๋ น์œผ๋กœ ์‹คํ–‰ ํ›„ Ctrl+c๋กœ ํ„ฐ๋ฏธ๋„์—์„œ ๋น ์ ธ๋‚˜์™€์„œ
  • $ curl http://localhost:8080 ๋ช…๋ น์œผ๋กœ ํ™”๋ฉด์ด ๋œจ๋Š”์ง€ ํ™•์ธ
  • ๋ฐฐํฌํ•œ ์ฝ”๋“œ ํ™•์ธ
    • 8080ํฌํŠธ๋Š” ๋ณดํ†ต ๋ฐฉํ™”๋ฒฝ์— ๋ง‰ํ˜€์žˆ๊ธฐ ๋•Œ๋ฌธ์— EC2์˜ ๋ณด์•ˆ๊ทธ๋ฃน์—์„œ ์‚ฌ์šฉ์žTCP 8080 ์œ„์น˜๋ฌด๊ด€์„ ์„ ํƒํ•˜๊ณ  ์ถ”๊ฐ€ํ•œ๋‹ค.
    • ์ธ์Šคํ„ด์Šค์˜ ํผ๋ธ”๋ฆญip:8080 ํ˜น์€ ํผ๋ธ”๋ฆญDNS:8080 ์„ ์ฃผ์†Œ๋กœ ์ ‘์†ํ•˜๋ฉด ์ ‘์†์ด ๋œ๋‹ค.

8. ์ˆ˜์ •๋œ ์†Œ์Šค์ฝ”๋“œ ๋ฐ˜์˜

  • ์ˆ˜์ •๋œ ์ฝ”๋“œ push & pull
    • Ctrl+d๋กœ ssh์—์„œ ๋ฒ—์–ด๋‚˜์„œ ๋กœ์ปฌ์—์„œ ์ƒˆ๋กœ ์ž‘์—…ํ•œ ๋‚ด์šฉ์„ github์— ํ‘ธ์‹œ
    • ๋‹ค์‹œ ssh๋กœ ์šฐ๋ถ„ํˆฌ์— ์ ‘์†ํ•˜์—ฌ ํ”„๋กœ์ ํŠธ๊ฐ€ ์žˆ๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋กœ ์ด๋™ํ•˜์—ฌ $ git pull
  • ํ”„๋กœ์„ธ์Šค ์ข…๋ฃŒ ๋ฐ ์„œ๋ฒ„ ์žฌ์‹œ์ž‘
    • ์ƒˆ๋กœ์šด ์ถ”๊ฐ€๋œ ๋‚ด์šฉ์„ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์„œ๋ฒ„๋ฅผ ์žฌ์‹œ์ž‘ํ•ด์•ผ ํ•œ๋‹ค. ์ฆ‰,
    • ํ˜„์žฌ ๋– ์žˆ๋Š” ์„œ๋ฒ„๋ฅผ ๋ฉˆ์ถฐ์•ผ ํ•˜๋ฏ€๋กœ ํ˜„์žฌ ์‹คํ–‰๋˜๊ณ  ์žˆ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ $ ps -ef | grep java ๋ช…๋ น์œผ๋กœ ํ™•์ธํ•˜๊ฑฐ๋‚˜,
    • $ jps ๋ช…๋ น์œผ๋กœ ์‹คํ–‰๋˜๊ณ ์žˆ๋Š” ์ž๋ฐ” ํ”„๋กœ๊ทธ๋žจ๊ณผ PID๋งŒ ํ™•์ธํ•˜์—ฌ
    • ๋ฐฐํฌํ–ˆ๋˜ jarํŒŒ์ผ์˜ ํ”„๋กœ์„ธ์Šค์•„์ด๋””(PID, ์ฒซ๋ฒˆ์งธ ์ˆซ์ž)๋ฅผ $ kill -9 ํ”„๋กœ์„ธ์Šค์•„์ด๋””๋กœ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ข…๋ฃŒํ•˜๊ฑฐ๋‚˜
    • $ pkill -f 'ํ”„๋กœ๊ทธ๋žจ๋ช…'์œผ๋กœ ํ”„๋กœ์„ธ์Šค ์ข…๋ฃŒ ๊ฐ€๋Šฅ
    • $ jps๋กœ ํ”„๋กœ์„ธ์Šค ์ข…๋ฃŒ ํ™•์ธ
  • ๋นŒ๋“œ ๋ฐ ๋ฐฐํฌ
    • $ ./mvnw clean package ๋ช…๋ น์œผ๋กœ ์ˆ˜์ •๋œ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ๋นŒ๋“œํ•˜๊ณ ,
    • target ๋””๋ ‰ํ† ๋ฆฌ๋กœ ์ด๋™ํ•˜์—ฌ $ nohup java -jar ChatBotTest-0.0.1-SNAPSHOT.jar & ๋ช…๋ น์œผ๋กœ ๋ฐฐํฌ

9. ์‰˜ ์Šคํฌ๋ฆฝํŠธ๋กœ ๋ฐฐํฌ ์ž๋™ํ™”ํ•˜๊ธฐ

http://jojoldu.tistory.com/263?category=635883 ์ฐธ๊ณ ํ•˜์—ฌ Gradle์ด ์•„๋‹Œ Maven ๋ฒ„์ „์œผ๋กœ ์ž‘์„ฑํ•จ

  1. EC2 ์ธ์Šคํ„ด์Šค์˜ /home/ubuntu ๋””๋ ‰ํ† ๋ฆฌ์— `deploy.sh’ ํŒŒ์ผ ์ƒ์„ฑ

    $ nano deploy.sh
  2. deploy.sh ํŒŒ์ผ์— ๋ฐฐํฌ ์ž๋™ํ™”๋ฅผ ์œ„ํ•œ ์‰˜์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑ

    #!/bin/bash
    
    REPOSITORY=/home/ubuntu/Chat-Bot-Kakao
    
    echo "> ํ”„๋กœ์ ํŠธ ์ €์žฅ์†Œ๋กœ ์ด๋™!"
    cd $REPOSITORY
    
    echo "> Git Pull !!!"
    git pull
    
    echo "> ์‹คํ–‰์ค‘์ธ ํ”„๋กœ์„ธ์Šค ํ™•์ธ!"
    CURRENT_PID=$(pgrep -f ChatBotTest)
    
    echo "> ์‹คํ–‰์ค‘์ธ ํ”„๋กœ์„ธ์Šค ์•„์ด๋”” : $CURRENT_PID"
    if [ -z $CURRENT_PID ]; then
            echo "> ํ˜„์žฌ ์‹คํ–‰์ค‘์ธ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์—†์œผ๋ฏ€๋กœ ์ข…๋ฃŒํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค!"
    else
            echo "> ํ˜„์žฌ ์‹คํ–‰์ค‘์ธ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ข…๋ฃŒ! Kill -9 $CURRENT_PID"
            kill -9 $CURRENT_PID
            sleep 5
    fi
    
    echo "> ํ”„๋กœ์ ํŠธ Build ์‹œ์ž‘! (์ด์ „ ๋ฐฐํฌ ๋ฒ„์ „ ์‚ญ์ œ ์•ˆํ•จ)"
    ./mvnw package
    
    echo "> ์ƒˆ ๋ฐฐํฌ ๋ฒ„์ „์˜ ์ด๋ฆ„์€ ??"
    JAR_NAME=$(ls $REPOSITORY/target |grep 'ChatBotTest' |grep -v 'original' | tail -n 1)
    echo "> JAR Name: $JAR_NAME"
    
    echo "> ์ƒˆ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฐฐํฌ !!!"
    nohup java -jar $REPOSITORY/target/$JAR_NAME &
  3. ./deploy.sh ์‹คํ–‰ ๊ถŒํ•œ ์„ค์ •

    $ chmod 755 ./deploy.sh