1. Ubuntu 환경에서 Node.js 설치하는 법
Ubuntu 환경에서 Express 서버를 실행시키려면 Node.js가 설치되어 있어야 한다.
링크 : https://github.com/nodesource/distributions
GitHub - nodesource/distributions: NodeSource Node.js Binary Distributions
NodeSource Node.js Binary Distributions. Contribute to nodesource/distributions development by creating an account on GitHub.
github.com
- Ubuntu
ubuntu@ip-172-31-43-245:~$ sudo su
root@ip-172-31-43-245:/home/ubuntu# apt-get update && /
apt-get install -y ca-certificates curl gnupg && /
mkdir -p /etc/apt/keyrings && /
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg && /
NODE_MAJOR=20 && /
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list && /
apt-get update && /
apt-get install nodejs -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]
Get:3 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports InRelease [126 kB]
Get:4 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble/universe amd64 Packages [15.0 MB]
Get:5 http://security.ubuntu.com/ubuntu noble-security InRelease [126 kB]
Get:6 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble/universe Translation-en [5982 kB]
Get:7 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble/universe amd64 Components [3871 kB]
Get:8 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble/universe amd64 c-n-f Metadata [301 kB]
Get:9 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble/multiverse amd64 Packages [269 kB]
Get:10 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble/multiverse Translation-en [118 kB]
Get:11 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble/multiverse amd64 Components [35.0 kB]
Get:12 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble/multiverse amd64 c-n-f Metadata [8328 B]
Get:13 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/main amd64 Packages [1684 kB]
Get:14 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/main Translation-en [311 kB]
Get:15 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/main amd64 Components [175 kB]
Get:16 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/main amd64 c-n-f Metadata [15.8 kB]
Get:17 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/universe amd64 Packages [1506 kB]
Get:18 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/universe Translation-en [306 kB]
Get:19 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/universe amd64 Components [378 kB]
Get:20 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/universe amd64 c-n-f Metadata [31.4 kB]
Get:21 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/restricted amd64 Packages [2413 kB]
Get:22 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/restricted Translation-en [550 kB]
Get:23 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/restricted amd64 Components [212 B]
Get:24 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/restricted amd64 c-n-f Metadata [516 B]
Get:25 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/multiverse amd64 Packages [30.4 kB]
Get:26 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/multiverse Translation-en [6048 B]
Get:27 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/multiverse amd64 Components [940 B]
Get:28 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/multiverse amd64 c-n-f Metadata [488 B]
Get:29 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports/main amd64 Packages [64.0 kB]
Get:30 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports/main Translation-en [9208 B]
Get:31 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports/main amd64 Components [7300 B]
Get:32 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports/main amd64 c-n-f Metadata [368 B]
Get:33 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports/universe amd64 Packages [33.0 kB]
Get:34 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports/universe Translation-en [17.9 kB]
Get:35 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports/universe amd64 Components [10.5 kB]
Get:36 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports/universe amd64 c-n-f Metadata [1444 B]
Get:37 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports/restricted amd64 Components [216 B]
Get:38 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports/restricted amd64 c-n-f Metadata [116 B]
Get:39 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports/multiverse amd64 Components [212 B]
Get:40 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports/multiverse amd64 c-n-f Metadata [116 B]
Get:41 http://security.ubuntu.com/ubuntu noble-security/main amd64 Packages [1391 kB]
Get:42 http://security.ubuntu.com/ubuntu noble-security/main Translation-en [225 kB]
Get:43 http://security.ubuntu.com/ubuntu noble-security/main amd64 Components [21.5 kB]
Get:44 http://security.ubuntu.com/ubuntu noble-security/main amd64 c-n-f Metadata [9504 B]
Get:45 http://security.ubuntu.com/ubuntu noble-security/universe amd64 Packages [916 kB]
Get:46 http://security.ubuntu.com/ubuntu noble-security/universe Translation-en [207 kB]
Get:47 http://security.ubuntu.com/ubuntu noble-security/universe amd64 Components [71.5 kB]
Get:48 http://security.ubuntu.com/ubuntu noble-security/universe amd64 c-n-f Metadata [19.4 kB]
Get:49 http://security.ubuntu.com/ubuntu noble-security/restricted amd64 Packages [2286 kB]
Get:50 http://security.ubuntu.com/ubuntu noble-security/restricted Translation-en [523 kB]
Get:51 http://security.ubuntu.com/ubuntu noble-security/restricted amd64 Components [212 B]
Get:52 http://security.ubuntu.com/ubuntu noble-security/multiverse amd64 Packages [27.4 kB]
Get:53 http://security.ubuntu.com/ubuntu noble-security/multiverse Translation-en [5956 B]
Get:54 http://security.ubuntu.com/ubuntu noble-security/multiverse amd64 Components [212 B]
Get:55 http://security.ubuntu.com/ubuntu noble-security/multiverse amd64 c-n-f Metadata [384 B]
Fetched 39.3 MB in 19s (2081 kB/s)
Reading package lists... Done
bash: /: Is a directory
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
ca-certificates is already the newest version (20240203).
ca-certificates set to manually installed.
curl is already the newest version (8.5.0-2ubuntu10.6).
curl set to manually installed.
gnupg is already the newest version (2.4.4-2ubuntu17.3).
gnupg set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 68 not upgraded.
bash: /: Is a directory
bash: /: Is a directory
bash: /: Is a directory
bash: /: Is a directory
deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main
bash: /: Is a directory
Hit:1 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble InRelease
Hit:2 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates InRelease
Hit:3 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports InRelease
Get:4 https://deb.nodesource.com/node_20.x nodistro InRelease [12.1 kB]
Hit:5 http://security.ubuntu.com/ubuntu noble-security InRelease
Get:6 https://deb.nodesource.com/node_20.x nodistro/main amd64 Packages [13.2 kB]
Fetched 25.3 kB in 1s (35.7 kB/s)
Reading package lists... Done
bash: /: Is a directory
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
nodejs
0 upgraded, 1 newly installed, 0 to remove and 68 not upgraded.
Need to get 32.0 MB of archives.
After this operation, 197 MB of additional disk space will be used.
Get:1 https://deb.nodesource.com/node_20.x nodistro/main amd64 nodejs amd64 20.19.6-1nodesource1 [32.0 MB]
Fetched 32.0 MB in 1s (59.7 MB/s)
Selecting previously unselected package nodejs.
(Reading database ... 71735 files and directories currently installed.)
Preparing to unpack .../nodejs_20.19.6-1nodesource1_amd64.deb ...
Unpacking nodejs (20.19.6-1nodesource1) ...
Setting up nodejs (20.19.6-1nodesource1) ...
Processing triggers for man-db (2.12.0-4build2) ...
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
root@ip-172-31-43-245:/home/ubuntu# node -v
v20.19.6
버전을 보며 잘 설치된 것을 확인할 수 있다.
3. Github로부터 Express 프로젝트 clone 하기
링크 : https://github.com/JSCODE-EDU/ec2-express-sample
GitHub - JSCODE-EDU/ec2-express-sample: <비전공자도 이해할 수 있는 AWS 입문/실전> 中 예제 프로젝트
<비전공자도 이해할 수 있는 AWS 입문/실전> 中 예제 프로젝트. Contribute to JSCODE-EDU/ec2-express-sample development by creating an account on GitHub.
github.com
- Ubuntu
root@ip-172-31-43-245:/home/ubuntu# git clone https://github.com/JSCODE-EDU/ec2-express-sample
Cloning into 'ec2-express-sample'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0 (from 0)
Receiving objects: 100% (6/6), 7.60 KiB | 1.90 MiB/s, done.
클론받은 프로젝트의 app.js 파일도 살펴본다.
- app.js
require('dotenv').config();
const express = require('express');
const app = express();
const port = 80;
app.get('/', (req, res) => {
res.send(`.env 테스트 : ${process.env.DATABASE_NAME}`);
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
express 파일을 실행시키기 위해 관련된 라이브러리를 설치한다.
- Ubuntu
root@ip-172-31-43-245:/home/ubuntu# cd ec2-express-sample
root@ip-172-31-43-245:/home/ubuntu/ec2-express-sample# ls
app.js package-lock.json package.json
root@ip-172-31-43-245:/home/ubuntu/ec2-express-sample# npm i
added 63 packages, and audited 64 packages in 2s
12 packages are looking for funding
run `npm fund` for details
6 vulnerabilities (3 low, 3 high)
To address all issues, run:
npm audit fix
Run `npm audit` for details.
npm notice
npm notice New major version of npm available! 10.8.2 -> 11.7.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v11.7.0
npm notice To update run: npm install -g npm@11.7.0
npm notice
4. .env 파일 직접 만들기
.env 와 같은 민감한 파일은 Git으로 버전 관리를 하지 않기 위해 .gitignore 파일에 추가해 놓는다. 따라서 .env 파일은 별도로 EC2 인스턴스에 올려주는데, .env 파일을 직접 만드는게 더 간단하다.
- Ubuntu
root@ip-172-31-43-245:/home/ubuntu/ec2-express-sample# ls
app.js node_modules package-lock.json package.json
root@ip-172-31-43-245:/home/ubuntu/ec2-express-sample# vi .env
민감한 값들을 .env 파일에 붙여넣는다.
- Ubuntu Vim
DATABASE_NAME=my_database
~
~
~
~
:wq
- Ubuntu
root@ip-172-31-43-245:/home/ubuntu/ec2-express-sample# ls -al
total 60
drwxr-xr-x 4 root root 4096 Dec 19 15:44 .
drwxr-x--- 5 ubuntu ubuntu 4096 Dec 19 15:30 ..
-rw-r--r-- 1 root root 26 Dec 19 15:44 .env
drwxr-xr-x 8 root root 4096 Dec 19 15:30 .git
-rw-r--r-- 1 root root 18 Dec 19 15:30 .gitignore
-rw-r--r-- 1 root root 282 Dec 19 15:30 app.js
drwxr-xr-x 65 root root 4096 Dec 19 15:36 node_modules
-rw-r--r-- 1 root root 24725 Dec 19 15:36 package-lock.json
-rw-r--r-- 1 root root 289 Dec 19 15:30 package.json
root@ip-172-31-43-245:/home/ubuntu/ec2-express-sample# cat .env
DATABASE_NAME=my_database
.env 파일이 잘 생성된 것을 확인할 수 있다.
5. pm2 설치해서 서버 실행시키기
Node 기반의 서버는 pm2를 활용해서 많이 실행시킨다. 서비스를 운영하는데 유용한 기능들을 많이 가지고 있기 때문이다.
pm2 설치 후 서버를 실행시킨다.
- Ubuntu
root@ip-172-31-43-245:/home/ubuntu/ec2-express-sample# sudo npm i -g pm2
added 133 packages in 15s
13 packages are looking for funding
run `npm fund` for details
root@ip-172-31-43-245:/home/ubuntu/ec2-express-sample# sudo pm2 start app.js
-------------
__/\\\\\\\\\\\\\____/\\\\____________/\\\\____/\\\\\\\\\_____
_\/\\\/////////\\\_\/\\\\\\________/\\\\\\__/\\\///////\\\___
_\/\\\_______\/\\\_\/\\\//\\\____/\\\//\\\_\///______\//\\\__
_\/\\\\\\\\\\\\\/__\/\\\\///\\\/\\\/_\/\\\___________/\\\/___
_\/\\\/////////____\/\\\__\///\\\/___\/\\\________/\\\//_____
_\/\\\_____________\/\\\____\///_____\/\\\_____/\\\//________
_\/\\\_____________\/\\\_____________\/\\\___/\\\/___________
_\/\\\_____________\/\\\_____________\/\\\__/\\\\\\\\\\\\\\\_
_\///______________\///______________\///__\///////////////__
Runtime Edition
PM2 is a Production Process Manager for Node.js applications
with a built-in Load Balancer.
Start and Daemonize any application:
$ pm2 start app.js
Load Balance 4 instances of api.js:
$ pm2 start api.js -i 4
Monitor in production:
$ pm2 monitor
Make pm2 auto-boot at server restart:
$ pm2 startup
To go further checkout:
http://pm2.io/
-------------
[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /home/ubuntu/ec2-express-sample/app.js in fork_mode (1 instance)
[PM2] Done.
┌────┬────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├────┼────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ app │ default │ 1.0.0 │ fork │ 2237 │ 0s │ 0 │ online │ 0% │ 29.0mb │ root │ disabled │
└────┴────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
확인을 위해 IP로 서버에 접속하면, 실행이 되는 것을 확인할 수 있다.

Express 서버를 EC2에 배포했다. CI/CD가 붙기 전엔 이런식으로 배포를 진행한다. 배포했다는 것은 다른 사용자들이 배포한 API 서버를 사용할 수 있게 되었다.
* 실행된 서버 종료하기
- Ubuntu
root@ip-172-31-43-245:/home/ubuntu/ec2-express-sample# ps aux | grep pm2
root 2226 0.0 5.4 1026704 53188 ? Ssl 15:50 0:00 PM2 v6.0.14: God Daemon (/root/.pm2)
root 2262 0.0 0.2 7076 2076 pts/1 S+ 15:58 0:00 grep --color=auto pm2
root@ip-172-31-43-245:/home/ubuntu/ec2-express-sample# kill 2226
root@ip-172-31-43-245:/home/ubuntu/ec2-express-sample# ps aux | grep pm2
root 2266 0.0 0.2 7076 2076 pts/1 S+ 16:00 0:00 grep --color=auto pm2
비전공자도 이해할 수 있는 AWS 입문/실전| JSCODE 박재성 - 인프런 강의
현재 평점 4.9점 수강생 2,819명인 강의를 만나보세요. 비전공자 입장에서도 쉽게 이해할 수 있고, 실전에서 바로 적용 가능한 AWS 입문 강의를 만들어봤습니다! EC2를 활용한 백엔드 API 서버 배포, R
www.inflearn.com
'강의 실습 > 비전공자도 이해할 수 있는 AWS 입문 실전' 카테고리의 다른 글
| 비용 나가지 않게 EC2 깔끔하게 종료하기 (0) | 2025.12.21 |
|---|---|
| [보충 강의] 7-2. Spring Boot 서버를 EC2에 배포하기 (1) | 2025.12.20 |
| [실습] 6. 탄력적 IP 연결하기 (0) | 2025.12.18 |
| [실습] 5. EC2 접속하기 (0) | 2025.12.17 |
| [실습] 4. EC2 셋팅하기 - 스토리지 구성 (0) | 2025.12.16 |
댓글