๐ŸŒฑ nodejs ์‹œ์ž‘๋ถ€ํ„ฐ ํด๋ผ์šฐ๋“œ ๋ฐฐํฌ๊นŒ์ง€

์ •๋‚˜์˜ยท2022๋…„ 11์›” 30์ผ
1
post-thumbnail
post-custom-banner

๐Ÿ‘ฉโ€๐Ÿ’ป node.js ์‹œ์ž‘ํ•˜๊ธฐ

0. ํ”„๋กœ์ ํŠธ ํด๋” ์ƒ์„ฑ

Local Directory์— ํด๋” ์ƒ์„ฑ

1. github repository ์ƒ์„ฑ

repository ์ƒ์„ฑ ๋ฐฉ๋ฒ•์€ https://docs.github.com/en/get-started/quickstart/create-a-repo ์ฐธ๊ณ !

echo "# nodejs_A_to_Z" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin "${Your repository url}"
git push -u origin main

2. nodejs ์„ค์น˜

https://nodejs.org/ko/

3. npm init

Local Directory์— npm init ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด node ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•œ๋‹ค.

4. express ์„œ๋ฒ„ ์ƒ์„ฑํ•˜๊ธฐ

nodejs ์ž์ฒด๋กœ๋Š” ์„œ๋ฒ„ ๊ฐœ๋ฐœ์ด ์–ด๋ ค์›Œ, express framework๋ฅผ ์‚ฌ์šฉ!

express ์„ค์น˜
npm(nodejs package manager)์„ ํ†ตํ•ด express ์„ค์น˜

npm i express

์„œ๋ฒ„ ์‹คํ–‰์„ ์œ„ํ•œ ๋ช…๋ น์–ด ์ •์˜
package.json ํŒŒ์ผ์— ์•„๋ž˜ ์ฝ”๋“œ ์ถ”๊ฐ€

"scripts": {
        "start" : "node index.js"
  }

index.js ์ƒ์„ฑ
git init ๋ช…๋ น์–ด์—์„œ entry point ๋กœ ์ง€์ •ํ–ˆ๋˜ index.js ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ , ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑ

const express = require('express')
const app = express()
const port = 8080

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

์„œ๋ฒ„ ์‹คํ–‰ํ•˜๊ธฐ

npm start

์œ„ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๊ณ  http://localhost:3000/ ์— Hello World!๊ฐ€ ๋ณด์ธ๋‹ค๋ฉด ์„ฑ๊ณต!๐ŸŽ‰๐ŸŽ‰

5. github์— ์ €์žฅ

git add .
git commit -m "[Add] ์ดˆ๊ธฐ ์„ค์ •"
git push

๐Ÿ‘ฉโ€๐Ÿ’ป ํด๋ผ์šฐ๋“œ ๋ฐฐํฌํ•˜๊ธฐ

0. AWS EC2 ์ƒ์„ฑ

์ด๋ฆ„ : nodejs A_to_Z
์šด์˜์ฒด์ œ : Ubuntu Server 22.04 LTS (HVM), SSD Volume Type [ํ”„๋ฆฌํ‹ฐ์–ด]
์ธ์Šคํ„ด์Šค ์œ ํ˜• : t2.micro [ํ”„๋ฆฌํ‹ฐ์–ด]
๋„คํŠธ์›Œํฌ ์„ค์ • : HTTP, HTTPS ํŠธ๋ž˜ํ”ฝ ํ—ˆ์šฉ
์Šคํ† ๋ฆฌ์ง€ ๊ตฌ์„ฑ : ๊ธฐ๋ณธ ์„ค์ •

1. EC2 ์—ฐ๊ฒฐ

์ƒ์„ฑํ•œ EC2์— ์ ‘๊ทผํ•˜๊ธฐ (๋งฅ OS ๊ฒฝ์šฐ, ํ„ฐ๋ฏธ๋„์„ ์ด์šฉํ•ด ์ ‘๊ทผ)

  1. SSH ํด๋ผ์ด์–ธํŠธ(ํ„ฐ๋ฏธ๋„)๋ฅผ ์—ฝ๋‹ˆ๋‹ค.
  2. ํ”„๋ผ์ด๋น— ํ‚ค ํŒŒ์ผ(ํ‚ค ํŽ˜์–ด)์„ ์ฐพ์Šต๋‹ˆ๋‹ค. ์ด ์ธ์Šคํ„ด์Šค๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ํ‚ค๋Š” ${ํ‚ค ํŽ˜์–ด}์ž…๋‹ˆ๋‹ค.
  3. ํ‚คํŽ˜์–ด ์•”ํ˜ธํ™” ์ด ๋ช…๋ น์„ ์‹คํ–‰ํ•˜์—ฌ ํ‚ค๋ฅผ ๊ณต๊ฐœ์ ์œผ๋กœ ๋ณผ ์ˆ˜ ์—†๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
chmod 400 ${ํ‚ค ํŽ˜์–ด}
  1. EC2 ์—ฐ๊ฒฐ
ssh -i ${ํ‚ค ํŽ˜์–ด} ${DNS ์ฃผ์†Œ}

2. EC2์— node js ์„ค์น˜

  1. ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด nvm (node version manager)๋ฅผ ์„ค์น˜
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
  1. nvm ํ™œ์„ฑํ™”
. ~/.nvm/nvm.sh
  1. nvm์„ ํ†ตํ•ด node.js ์„ค์น˜
nvm install --lts
  1. ํ…Œ์ŠคํŠธ
node -e "console.log('Version: ' + process.version)"

3. github repository ๋ณต์‚ฌ

  • EC2์— ๋ณต์‚ฌ
git clone ${your repository url}

๐Ÿฏ Permission denied ์—๋Ÿฌ ๋ฐœ์ƒ ์‹œ

sudo git clone ${your repository url}

4. ํ”„๋กœ์ ํŠธ์— ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜

  • node module ํด๋”๋ฅผ github์— ์˜ฌ๋ฆฌ์ง€ ์•Š์„ ๊ฒฝ์šฐ package.json ํŒŒ์ผ์— ๊ธฐ๋ก๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์„ค์น˜
npm install

๐Ÿฏ Permission denied ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์‹œ

  • git clone์‹œ root ๊ถŒํ•œ์œผ๋กœ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ, user ๊ถŒํ•œ์œผ๋กœ ํŒŒ์ผ ๋ณ€๊ฒฝ ํ•„์š”
  • ubuntu os์˜ ๊ธฐ๋ณธ ์œ ์ € ์ด๋ฆ„์€ ubuntu
sudo chown -R ubuntu: package.json
sudo chown -R ubuntu: package-lock.json

5. EC2 ์„œ๋ฒ„ ํฌํŠธ ํฌ์›Œ๋”ฉ

  • ์ธํ„ฐ๋„ท ํฌํŠธ์ธ 80๋ฒˆ ํฌํŠธ๋กœ๋Š” ์„œ๋ฒ„๋ฅผ ์ง์ ‘ ์—ด ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์—, 8080์œผ๋กœ ์„œ๋ฒ„๋ฅผ ์—ฐ ๋’ค์— 80๋ฒˆ ํฌํŠธ์˜ ์š”์ฒญ์„ 8080์œผ๋กœ ํฌ์›Œ๋”ฉ
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080

6. ํด๋ผ์šฐ๋“œ์—์„œ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์„œ๋ฒ„ ์‹คํ–‰

npm install -g pm2
pm2 start index.js

  • ํผ๋ธ”๋ฆญ IP ์ฃผ์†Œ ํ™•์ธ
    http://15.165.77.127 ์— ์ ‘์†ํ–ˆ์„ ๋•Œ Hello World!๊ฐ€ ๋ณด์ธ๋‹ค๋ฉด ์„ฑ๊ณต!
post-custom-banner

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2022๋…„ 12์›” 4์ผ

์™€.. ๋„ˆ๋ฌด ์ž˜ ์ •๋ฆฌํ•ด์ฃผ์…จ๋„ค์š” ๐Ÿ˜ฑ

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ