1. Ubuntu 환경에서 JDK 설치하는 법
Spring Boot는 3.2 버전 이상을 사용할 예정이라서, JDK 17 버전을 사용할 예정이다. 이에 맞게 환경을 구성한다.
- Ubuntu
ubuntu@ip-172-31-43-245:~$ sudo apt update && /
sudo apt install openjdk-17-jdk -y
Hit:1 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble InRelease
Get:2 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates InRelease [126 kB]
...
update-alternatives: using /usr/lib/jvm/java-17-openjdk-amd64/bin/jconsole to provide /usr/bin/jconsole (jconsole) in auto mode
Processing triggers for libc-bin (2.39-0ubuntu8.6) ...
Processing triggers for libgdk-pixbuf-2.0-0:amd64 (2.42.10+dfsg-3ubuntu3.2) ...
Scanning processes...
Scanning linux images...
Running kernel seems to be up-to-date.
No services need to be restarted.
No containers need to be restarted.
No user sessions are running outdated binaries.
No VM guests are running outdated hypervisor (qemu) binaries on this host.
2. 잘 설치됐는 지 확인
- Ubuntu
ubuntu@ip-172-31-43-245:~$ java -version
openjdk version "17.0.17" 2025-10-21
OpenJDK Runtime Environment (build 17.0.17+10-Ubuntu-124.04)
OpenJDK 64-Bit Server VM (build 17.0.17+10-Ubuntu-124.04, mixed mode, sharing)
자바 버전을 확인하니 잘 설치된 것을 확인할 수 있다.
3. Github으로부터 Spring Boot 프로젝트 clone 하기
링크 : https://github.com/JSCODE-EDU/ec2-spring-boot-sample
GitHub - JSCODE-EDU/ec2-spring-boot-sample: <비전공자도 이해할 수 있는 AWS 입문/실전> 中 예제 프로젝트
<비전공자도 이해할 수 있는 AWS 입문/실전> 中 예제 프로젝트. Contribute to JSCODE-EDU/ec2-spring-boot-sample development by creating an account on GitHub.
github.com
- Ubuntu
ubuntu@ip-172-31-43-245:~$ git clone https://github.com/JSCODE-EDU/ec2-spring-boot-sample.git
Cloning into 'ec2-spring-boot-sample'...
remote: Enumerating objects: 30, done.
remote: Counting objects: 100% (30/30), done.
remote: Compressing objects: 100% (17/17), done.
remote: Total 30 (delta 1), reused 30 (delta 1), pack-reused 0 (from 0)
Receiving objects: 100% (30/30), 63.60 KiB | 12.72 MiB/s, done.
Resolving deltas: 100% (1/1), done.
ubuntu@ip-172-31-43-245:~$ ls -al
total 40
drwxr-x--- 6 ubuntu ubuntu 4096 Dec 20 01:49 .
drwxr-xr-x 3 root root 4096 Dec 16 13:13 ..
-rw------- 1 ubuntu ubuntu 27 Dec 19 16:00 .bash_history
-rw-r--r-- 1 ubuntu ubuntu 220 Mar 31 2024 .bash_logout
-rw-r--r-- 1 ubuntu ubuntu 3771 Mar 31 2024 .bashrc
drwx------ 2 ubuntu ubuntu 4096 Dec 16 13:18 .cache
-rw-r--r-- 1 ubuntu ubuntu 807 Mar 31 2024 .profile
drwx------ 2 ubuntu ubuntu 4096 Dec 16 13:13 .ssh
-rw-r--r-- 1 ubuntu ubuntu 0 Dec 19 15:25 .sudo_as_admin_successful
drwxr-xr-x 4 root root 4096 Dec 19 15:44 ec2-express-sample
drwxrwxr-x 5 ubuntu ubuntu 4096 Dec 20 01:49 ec2-spring-boot-sample
ubuntu@ip-172-31-43-245:~$ cd ec2-spring-boot-sample
ubuntu@ip-172-31-43-245:~/ec2-spring-boot-sample$ ls
build.gradle gradle gradlew gradlew.bat settings.gradle src
클론받은 프로젝트의 Ec2SpringBootSampleApplication.java 파일도 살펴본다.
- Ec2SpringBootSampleApplication.java
package com.example.ec2springbootsample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Ec2SpringBootSampleApplication {
public static void main(String[] args) {
SpringApplication.run(Ec2SpringBootSampleApplication.class, args);
}
}
4. application.yml 파일 직접 만들기
application.yml 과 같은 민감한 파일은 Git으로 버전 관리를 하지 않기 위해 .gitignore 파일에 추가해 놓는다. 따라서 application.yml 파일은 별도로 EC2 인스턴스에 올려주는데, application.yml 파일을 직접 만드는게 더 간단하다.
- Ubuntu
ubuntu@ip-172-31-43-245:~/ec2-spring-boot-sample$ cd src/main/resources
ubuntu@ip-172-31-43-245:~/ec2-spring-boot-sample/src/main/resources$ vi application.yml
- Ubuntu Vim
server:
port: 80
~
~
~
~
:wq
- Ubuntu
ubuntu@ip-172-31-43-245:~/ec2-spring-boot-sample/src/main/resources$ cat application.yml
server:
port: 80
application.yml 파일이 잘 생성된 것을 확인할 수 있다.
5. 서버 실행시키기
기존 빌드된 파일을 삭제하고 새롭게 .jar로 빌드하기 위해 clean build 명령어를 사용했다.
- Ubuntu
ubuntu@ip-172-31-43-245:~/ec2-spring-boot-sample/src/main/resources$ ./gradlew clean build
-bash: ./gradlew: No such file or directory
ubuntu@ip-172-31-43-245:~/ec2-spring-boot-sample/src/main/resources$ cd ..
ubuntu@ip-172-31-43-245:~/ec2-spring-boot-sample/src/main$ cd ..
ubuntu@ip-172-31-43-245:~/ec2-spring-boot-sample/src$ cd ..
ubuntu@ip-172-31-43-245:~/ec2-spring-boot-sample$ ls
build.gradle gradle gradlew gradlew.bat settings.gradle src
ubuntu@ip-172-31-43-245:~/ec2-spring-boot-sample$ ./gradlew clean build
Downloading https://services.gradle.org/distributions/gradle-8.4-bin.zip
............10%............20%.............30%............40%.............50%............60%.............70%............80%.............90%............100%
Welcome to Gradle 8.4!
Here are the highlights of this release:
- Compiling and testing with Java 21
- Faster Java compilation on Windows
- Role focused dependency configurations creation
For more details see https://docs.gradle.org/8.4/release-notes.html
Starting a Gradle Daemon (subsequent builds will be faster)
OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/8.4/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD SUCCESSFUL in 1m 23s
8 actionable tasks: 7 executed, 1 up-to-date
만약 EC2 인스턴스 사양이 부족하여 EC2가 멈추거나 버벅이거나 빌드 작업이 끝나지 않는 현상이 발생한다면, 스왑 파일을 활용하여 메모리를 추가 할당한다.
참고 링크 : https://velog.io/@shawnhansh/AWS-EC2-%EB%A9%94%EB%AA%A8%EB%A6%AC-%EC%8A%A4%EC%99%91
AWS EC2 램 늘리기(feat. 스왑)
이제 gradle이 멈추지 않습니다✨
velog.io
빌드가 성공되었다면, Spring Boot 서버를 실행한다.
- Ubuntu
ubuntu@ip-172-31-43-245:~/ec2-spring-boot-sample$ ls
build build.gradle gradle gradlew gradlew.bat settings.gradle src
ubuntu@ip-172-31-43-245:~/ec2-spring-boot-sample$ cd build
ubuntu@ip-172-31-43-245:~/ec2-spring-boot-sample/build$ ls
classes generated libs reports resolvedMainClassName resources test-results tmp
ubuntu@ip-172-31-43-245:~/ec2-spring-boot-sample/build$ cd libs
ubuntu@ip-172-31-43-245:~/ec2-spring-boot-sample/build/libs$ ls
ec2-spring-boot-sample-0.0.1-SNAPSHOT-plain.jar ec2-spring-boot-sample-0.0.1-SNAPSHOT.jar
ubuntu@ip-172-31-43-245:~/ec2-spring-boot-sample/build/libs$ sudo java -jar ec2-spring-boot-sample-0.0.1-SNAPSHOT.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.2.0)
2025-12-20T02:14:06.497Z INFO 13448 --- [ main] c.e.e.Ec2SpringBootSampleApplication : Starting Ec2SpringBootSampleApplication v0.0.1-SNAPSHOT using Java 17.0.17 with PID 13448 (/home/ubuntu/ec2-spring-boot-sample/build/libs/ec2-spring-boot-sample-0.0.1-SNAPSHOT.jar started by root in /home/ubuntu/ec2-spring-boot-sample/build/libs)
2025-12-20T02:14:06.504Z INFO 13448 --- [ main] c.e.e.Ec2SpringBootSampleApplication : No active profile set, falling back to 1 default profile: "default"
2025-12-20T02:14:08.831Z INFO 13448 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 80 (http)
2025-12-20T02:14:08.857Z INFO 13448 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2025-12-20T02:14:08.858Z INFO 13448 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.16]
2025-12-20T02:14:09.163Z INFO 13448 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2025-12-20T02:14:09.168Z INFO 13448 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2545 ms
2025-12-20T02:14:10.448Z INFO 13448 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 80 (http) with context path ''
2025-12-20T02:14:10.502Z INFO 13448 --- [ main] c.e.e.Ec2SpringBootSampleApplication : Started Ec2SpringBootSampleApplication in 5.085 seconds (process running for 6.48)
* 백그라운드에서 Spring Boot 서버 실행시키기
- Ubuntu
ubuntu@ip-172-31-43-245:~/ec2-spring-boot-sample/build/libs$ sudo nohup java -jar ec2-spring-boot-sample-0.0.1-SNAPSHOT.jar &
[1] 13499
ubuntu@ip-172-31-43-245:~/ec2-spring-boot-sample/build/libs$ nohup: ignoring input and appending output to 'nohup.out'
6. 잘 작동하는 지 확인
확인을 위해 IP로 서버에 접속하면, 실행이 되는 것을 확인할 수 있다.

* 백그라운드로 실행된 서버 종료하기
- Ubuntu
ubuntu@ip-172-31-43-245:~/ec2-spring-boot-sample/build/libs$ ps aux | grep java
ubuntu 3737 4.9 37.4 2016800 367324 ? Ssl 02:05 0:52 /usr/lib/jvm/java-17-openjdk-amd64/bin/java --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-opens=java.base/java.nio.charset=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED -XX:MaxMetaspaceSize=384m -XX:+HeapDumpOnOutOfMemoryError -Xms256m -Xmx512m -Dfile.encoding=UTF-8 -Duser.country -Duser.language=en -Duser.variant -cp /home/ubuntu/.gradle/wrapper/dists/gradle-8.4-bin/1w5dpkrfk8irigvoxmyhowfim/gradle-8.4/lib/gradle-launcher-8.4.jar -javaagent:/home/ubuntu/.gradle/wrapper/dists/gradle-8.4-bin/1w5dpkrfk8irigvoxmyhowfim/gradle-8.4/lib/agents/gradle-instrumentation-agent-8.4.jar org.gradle.launcher.daemon.bootstrap.GradleDaemon 8.4
root 13557 0.0 0.6 17132 6768 pts/0 S 02:22 0:00 sudo nohup java -jar ec2-spring-boot-sample-0.0.1-SNAPSHOT.jar
root 13558 0.0 0.2 17132 2476 pts/1 Ss+ 02:22 0:00 sudo nohup java -jar ec2-spring-boot-sample-0.0.1-SNAPSHOT.jar
root 13559 91.1 14.9 2346624 147000 pts/1 Sl 02:22 0:06 java -jar ec2-spring-boot-sample-0.0.1-SNAPSHOT.jar
ubuntu 13590 0.0 0.2 7076 2104 pts/0 S+ 02:22 0:00 grep --color=auto java
ubuntu@ip-172-31-43-245:~/ec2-spring-boot-sample/build/libs$ kill 13557
ubuntu@ip-172-31-43-245:~/ec2-spring-boot-sample/build/libs$ ps aux | grep java
ubuntu 3737 4.9 37.4 2016800 367324 ? Ssl 02:05 0:52 /usr/lib/jvm/java-17-openjdk-amd64/bin/java --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-opens=java.base/java.nio.charset=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED -XX:MaxMetaspaceSize=384m -XX:+HeapDumpOnOutOfMemoryError -Xms256m -Xmx512m -Dfile.encoding=UTF-8 -Duser.country -Duser.language=en -Duser.variant -cp /home/ubuntu/.gradle/wrapper/dists/gradle-8.4-bin/1w5dpkrfk8irigvoxmyhowfim/gradle-8.4/lib/gradle-launcher-8.4.jar -javaagent:/home/ubuntu/.gradle/wrapper/dists/gradle-8.4-bin/1w5dpkrfk8irigvoxmyhowfim/gradle-8.4/lib/agents/gradle-instrumentation-agent-8.4.jar org.gradle.launcher.daemon.bootstrap.GradleDaemon 8.4
ubuntu 13595 0.0 0.2 7076 2068 pts/0 S+ 02:22 0:00 grep --color=auto java
[1]+ Exit 143 sudo nohup java -jar ec2-spring-boot-sample-0.0.1-SNAPSHOT.jar
비전공자도 이해할 수 있는 AWS 입문/실전| JSCODE 박재성 - 인프런 강의
현재 평점 4.9점 수강생 2,819명인 강의를 만나보세요. 비전공자 입장에서도 쉽게 이해할 수 있고, 실전에서 바로 적용 가능한 AWS 입문 강의를 만들어봤습니다! EC2를 활용한 백엔드 API 서버 배포, R
www.inflearn.com
'강의 실습 > 비전공자도 이해할 수 있는 AWS 입문 실전' 카테고리의 다른 글
| Route 53이란? / DNS란? / 현업에서의 Route 53 활용 여부 (0) | 2025.12.22 |
|---|---|
| 비용 나가지 않게 EC2 깔끔하게 종료하기 (0) | 2025.12.21 |
| [실습] 7. Express 서버를 EC2에 배포하기 (0) | 2025.12.20 |
| [실습] 6. 탄력적 IP 연결하기 (0) | 2025.12.18 |
| [실습] 5. EC2 접속하기 (0) | 2025.12.17 |
댓글