AWS

[AWS] EC2에서 스프링 프로젝트 배포

NCOOKIE_ 2022. 10. 31. 02:34

로컬에서 구현하고 테스트해본 프로젝트를 AWS의 EC2에서 배포해보자. 

 

먼저 가상 컴퓨터에서 git과 java를 설치해준다. 자바를 설치할 때 중간에 설치를 진행할건지 물어보는데 이 때 y를 입력해주면 된다. 아니면 설치 명령어 뒤에 -y 옵션을 붙여줘도 된다.

 

sudo yum install -y git
sudo amazon-linux-extras install java-openjdk11

 

이제 프로젝트를 홈 디렉터리에 만들어줄거다. 나는 apps라는 디렉터리를 생성하고 거기에 프로젝트를 clone을 했다.

 

mkdir apps
cd apps
git clone [git remote 링크]

 

프로젝트 빌드를 하기 위해서 해당 디렉터리에 존재하는 gradlew의 실행권한을 변경해준다. 그리고 빌드를 해보자.

 

sudo chmod 777 ./gradlew
sudo ./gradlew build

 

나 같은 경우엔 build를 할 때 sudo 명령어를 붙여주지 않았더니 아래 에러가 떠서 한참 헤맸었다.

 

* What went wrong:
A problem occurred configuring root project 'spring-practice'.
> Could not resolve all files for configuration ':classpath'.
   > Could not download spring-boot-gradle-plugin-2.7.5.jar (org.springframework.boot:spring-boot-gradle-plugin:2.7.5): Skipped due to earlier error
   > Could not download dependency-management-plugin-1.0.15.RELEASE.jar (io.spring.gradle:dependency-management-plugin:1.0.15.RELEASE): Skipped due to earlier error
   > Could not download spring-boot-buildpack-platform-2.7.5.jar (org.springframework.boot:spring-boot-buildpack-platform:2.7.5): Skipped due to earlier error
   > Could not download spring-boot-loader-tools-2.7.5.jar (org.springframework.boot:spring-boot-loader-tools:2.7.5): Skipped due to earlier error
   > Could not download commons-compress-1.21.jar (org.apache.commons:commons-compress:1.21): Skipped due to earlier error
   > Could not download spring-core-5.3.23.jar (org.springframework:spring-core:5.3.23): Skipped due to earlier error
   > Could not download jackson-annotations-2.13.4.jar (com.fasterxml.jackson.core:jackson-annotations:2.13.4): Skipped due to earlier error
   > Could not download jackson-core-2.13.4.jar (com.fasterxml.jackson.core:jackson-core:2.13.4): Skipped due to earlier error
   > Could not download jackson-module-parameter-names-2.13.4.jar (com.fasterxml.jackson.module:jackson-module-parameter-names:2.13.4): Skipped due to earlier error
   > Could not download jackson-databind-2.13.4.2.jar (com.fasterxml.jackson.core:jackson-databind:2.13.4.2): Skipped due to earlier error
   > Could not download jna-platform-5.7.0.jar (net.java.dev.jna:jna-platform:5.7.0): Skipped due to earlier error
   > Could not download httpclient-4.5.13.jar (org.apache.httpcomponents:httpclient:4.5.13): Skipped due to earlier error
   > Could not download spring-jcl-5.3.23.jar (org.springframework:spring-jcl:5.3.23): Skipped due to earlier error
   > Could not download jna-5.7.0.jar (net.java.dev.jna:jna:5.7.0): Skipped due to earlier error
   > Could not download httpcore-4.4.13.jar (org.apache.httpcomponents:httpcore:4.4.13): Skipped due to earlier error
   > Could not download commons-codec-1.11.jar (commons-codec:commons-codec:1.11): Skipped due to earlier error
   > Could not download antlr4-runtime-4.7.2.jar (org.antlr:antlr4-runtime:4.7.2): Skipped due to earlier error
   > Could not download jsr305-3.0.2.jar (com.google.code.findbugs:jsr305:3.0.2): Skipped due to earlier error

 

여기까지 정상적으로 됐으면 프로젝트를 실행해보자. 파일의 이름은 프로젝트에 따라 다를 수 있다.

 

java -jar build/libs/spring-practice-0.0.1-SNAPSHOT.jar

 

 

서버 주소와 포트를 입력해서 접속해보면 정상적으로 스프링 서버가 실행되고 있는 것을 확인할 수 있다.

 

이렇게 프로젝트가 업데이트 될 떄마다 서버에서 빌드할 수도 있겠지만, gitignore의 파일들은 수동으로 수정해줘야 하고, DB 접속 계정 등 보안 정보가 유출될 가능성이 있다. 규모가 커지면 빌드 시간이 늘어나는 단점도 있다. 그래서 다음에는 로컬에서 빌드를 하면 자동으로 원격 배포가 되도록 해보자.

 

 

참고링크

 

https://victorydntmd.tistory.com/338

 

[SpringBoot] 게시판 (5) - AWS EC2에 배포하기 (feat. AWS RDS)

Springboot로 디자인이 하나도 없고 매우 간단한 게시판을 구현하는 시리즈입니다. 최종 소스는 깃헙에서 확인하실 수 있습니다. [SpringBoot] 게시판 (1) - 준비작업 [SpringBoot] 게시판 (2) - 게시글 추

victorydntmd.tistory.com