Skip to content

๐Ÿชต 3. ์ธํ”„๋ผ ์‹ค์Šต(2) : ๋„์ปค ์—†์ด ๋ฐฐํฌํ•˜๊ธฐ

ssum1ra edited this page Dec 5, 2024 · 1 revision

1. Nginx ๊ธฐ๋ฐ˜ ๋ฐฐํฌ

์„ค์น˜

# 1. ๊ธฐ๋ณธ ์—…๋ฐ์ดํŠธ
sudo apt update
sudo apt upgrade -y

# 2. Node.js ์„ค์น˜
# node.js 20.0 ๋ฒ„์ „์œผ๋กœ ์„ค์น˜
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs

# 3. Nginx ์„ค์น˜
# 2024-11-11 ๊ธฐ์ค€ ubuntu-22.04์—์„œ์—๋Ÿฌ
~~sudo apt install nginx -y~~

# ๊ณต์‹๋ฌธ์„œ ์ฐธ๊ณ ํ•ด์„œ ์„ค์น˜
~~~~https://nginx.org/en/linux_packages.html#Ubuntu

# 4. PM2 ์„ค์น˜
sudo npm install -g pm2

# 5. git ์„ค์น˜
sudo apt install git -y
  • curl ๋ช…๋ น ์‚ฌ์šฉ ์ „, ํฌํŠธ๋ฅผ ์—ด์–ด๋‘์–ด์•ผ ํ•œ๋‹ค.

  • ๋””๋ ‰ํ† ๋ฆฌ ๊ถŒํ•œ ์„ค์ • (์ฐธ๊ณ )

    # ์ผ๋ฐ˜์ ์ธ nginx ์„ค์ • ์˜ˆ์‹œ
    # ํ˜„์žฌ ๋ฐฐํฌ ๊ณผ์ •์—์„œ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์ง€ ์•Š๋‹ค.
    
    sudo chown -R $USER:$USER /var/www/html
    ls -ld /var/www/html
    • /var/www/html : ์›น ์„œ๋ฒ„๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์›น ํŒŒ์ผ์„ ์ œ๊ณตํ•˜๋Š” ๋ฃจํŠธ ๋””๋ ‰ํ„ฐ๋ฆฌ

ํ”„๋ก ํŠธ์—”๋“œ ๋นŒ๋“œ

# ๋ฆฌํฌ์ง€ํ† ๋ฆฌ ํด๋ก 
~$ git clone [๋ฆฌํฌ์ง€ํ† ๋ฆฌ์ฃผ์†Œ]

~/[project ์ด๋ฆ„]/client$ npm run build

NGINX ์„ค์ •

sudo systemctl start nginx # Nginx ์„œ๋น„์Šค๋ฅผ ์ฆ‰์‹œ ์‹œ์ž‘
sudo systemctl enable nginx # ์„œ๋ฒ„ ๋ถ€ํŒ… ์‹œ Nginx๊ฐ€ ์ž๋™์œผ๋กœ ์‹œ์ž‘๋˜๋„๋ก ์„ค์ •

sudo mkdir -p /etc/nginx/sites-available # ์‚ฌ์ดํŠธ๋ณ„ ์„ค์ • ํŒŒ์ผ์„ ์ €์žฅํ•  ๋””๋ ‰ํ„ฐ๋ฆฌ ์ƒ์„ฑ
sudo mkdir -p /etc/nginx/sites-enabled # ํ™œ์„ฑํ™”๋œ ์‚ฌ์ดํŠธ ์„ค์ • ํŒŒ์ผ์„ ์œ„ํ•œ ๋””๋ ‰ํ„ฐ๋ฆฌ ์ƒ์„ฑ

Nginx ๋ฉ”์ธ ์„ค์ • ํŒŒ์ผ ์ˆ˜์ •

sudo vim /etc/nginx/nginx.conf

# Nginx๊ฐ€ ๊ฐ ์‚ฌ์ดํŠธ์— ๋Œ€ํ•œ ์„ค์ • ํŒŒ์ผ์„ ์ž๋™์œผ๋กœ ์ธ์‹ํ•˜๊ณ  ์ ์šฉํ•˜๋„๋ก
http {
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*; # ์ถ”๊ฐ€
    ...
}

Nginx ์‚ฌ์ดํŠธ ์„ค์ • ํŒŒ์ผ ์ž‘์„ฑ

sudo vim /etc/nginx/sites-available/[ํ”„๋กœ์ ํŠธ ๋ช…]

server {
    listen 80; # HTTP ๊ธฐ๋ณธ ํฌํŠธ 80์—์„œ ์š”์ฒญ์„ ์ˆ˜์‹ 
    server_name [์ธ์Šคํ„ด์Šค์˜_PUBLIC_IP]; # ์„œ๋ฒ„์˜ ๊ณต์ธ IP ์ฃผ์†Œ ๋˜๋Š” ๋„๋ฉ”์ธ ์ด๋ฆ„ ์ง€์ •

    location / {
        root /home/ncloud/whiteboard/client/dist; # ์ •์  ํŒŒ์ผ์˜ ๋ฃจํŠธ ๊ฒฝ๋กœ ์„ค์ •
        try_files $uri $uri/ /index.html; # ์š”์ฒญํ•œ ํŒŒ์ผ์ด ์—†์„ ๊ฒฝ์šฐ index.html๋กœ ๋Œ€์ฒด (SPA ์ง€์›)
        index index.html; # ๊ธฐ๋ณธ ์ธ๋ฑ์Šค ํŒŒ์ผ ์„ค์ •
    }

    # WebSocket์„ ์œ„ํ•œ Socket.IO ํ”„๋ก์‹œ ์„ค์ •
    location /socket.io/ {
        proxy_pass http://localhost:3000; # ๋กœ์ปฌ ์„œ๋ฒ„์˜ 3000 ํฌํŠธ๋กœ ์š”์ฒญ์„ ํ”„๋ก์‹œ
        proxy_http_version 1.1; # WebSocket์„ ์œ„ํ•ด HTTP/1.1 ์‚ฌ์šฉ
        proxy_set_header Upgrade $http_upgrade; # WebSocket ์—ฐ๊ฒฐ ์—…๊ทธ๋ ˆ์ด๋“œ ์š”์ฒญ
        proxy_set_header Connection "upgrade"; # WebSocket ์—ฐ๊ฒฐ ์—…๊ทธ๋ ˆ์ด๋“œ ์„ค์ •
        proxy_set_header Host $host; # ์›๋ณธ ์š”์ฒญ์˜ ํ˜ธ์ŠคํŠธ ํ—ค๋” ์œ ์ง€
        proxy_cache_bypass $http_upgrade; # WebSocket ์—ฐ๊ฒฐ ์‹œ ์บ์‹œ ์šฐํšŒ
        proxy_read_timeout 86400s; # ํ”„๋ก์‹œ ์ฝ๊ธฐ ํƒ€์ž„์•„์›ƒ ์„ค์ • (24์‹œ๊ฐ„)
        proxy_send_timeout 86400s; # ํ”„๋ก์‹œ ์ „์†ก ํƒ€์ž„์•„์›ƒ ์„ค์ • (24์‹œ๊ฐ„)
    }
    
    # REST API (ํ•„์š”ํ•œ ๊ฒฝ์šฐ)
    location /api/ {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

Nginx ์‚ฌ์ดํŠธ ์„ค์ • ํ™œ์„ฑํ™”

# ์‚ฌ์ดํŠธ ์„ค์ • ํŒŒ์ผ์„ ํ™œ์„ฑํ™”ํ•˜๊ธฐ ์œ„ํ•ด ์‹ฌ๋ณผ๋ฆญ ๋งํฌ ์ƒ์„ฑ
sudo ln -s /etc/nginx/sites-available/[ํ”„๋กœ์ ํŠธ ๋ช…] /etc/nginx/sites-enabled/
# ์˜ค๋ฅ˜๊ฐ€ ์žˆ๋Š”์ง€ ํ…Œ์ŠคํŠธ
sudo nginx -t 
# Nginx ์„œ๋น„์Šค๋ฅผ ์žฌ์‹œ์ž‘ํ•˜์—ฌ ๋ณ€๊ฒฝ๋œ ์„ค์ • ์ ์šฉ
sudo systemctl restart nginx
  • Nginx๋Š” ์ •์  ํŒŒ์ผ์„ ์ง์ ‘ ์„œ๋น™ํ•˜๋ฉด์„œ, ํด๋ผ์ด์–ธํŠธ์˜ ๋™์  ์š”์ฒญ์„ ๋ฐฑ์—”๋“œ ์„œ๋ฒ„๋กœ ์ „๋‹ฌํ•˜๋Š” ๋ฆฌ๋ฒ„์Šค ํ”„๋ก์‹œ ์„œ๋ฒ„ ์—ญํ• ์„ ๋™์‹œ์— ์ˆ˜ํ–‰ํ•œ๋‹ค.

์„œ๋ฒ„ ๋นŒ๋“œ ๋ฐ ์‹คํ–‰

~/[project ์ด๋ฆ„]/server$ npm install -g @nestjs/cli
~/[project ์ด๋ฆ„]/server$ npm run build

# PM2๋ฅผ ์‚ฌ์šฉํ•ด dist/main.js ํŒŒ์ผ์„ "whiteboard-server"๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰
~/[project ์ด๋ฆ„]/server$ pm2 start dist/main.js --name "whiteboard-server"

# PM2 ๋กœ๊ทธ
pm2 log
  • PM2 (์ถ”ํ›„ ์ž‘์„ฑ)
    • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋ฐ๋ชฌ(๋ฐฑ๊ทธ๋ผ์šด๋“œ) ๋ชจ๋“œ๋กœ ์‹คํ–‰ํ•˜๋ฏ€๋กœ, SSH ์„ธ์…˜์ด ์ข…๋ฃŒ๋˜๋”๋ผ๋„ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๊ณ„์† ์‹คํ–‰๋œ๋‹ค.
    • ๊ธฐํƒ€ ๊ธฐ๋Šฅ

๋นŒ๋“œ ๋””๋ ‰ํ„ฐ๋ฆฌ ์†Œ์œ ๊ถŒ ๋ฐ ๊ถŒํ•œ ์„ค์ •

sudo chown -R $USER:$USER ~/whiteboard/client/dist
sudo chmod -R 755 ~/whiteboard/client/dist

๊ถŒํ•œ ์˜ค๋ฅ˜

~/[ํ”„๋กœ์ ํŠธ ๋ช…]/client/dist$ sudo tail -f /var/log/nginx/error.log

2024/11/11 17:26:55 [crit] 3326#3326: *5 stat() "/home/ncloud/whiteboard/client/dist/index.html" failed (13: Permission denied), client: 121.136.217.193, server: 223.130.137.162, request: "GET /favicon.ico HTTP/1.1", host: "223.130.137.162", referrer: "http://223.130.137.162/"
2024/11/11 17:26:55 [crit] 3326#3326: *5 stat() "/home/ncloud/whiteboard/client/dist/index.html" failed (13: Permission denied), client: 121.136.217.193, server: 223.130.137.162, request: "GET /favicon.ico HTTP/1.1", host: "223.130.137.162", referrer: "http://223.130.137.162/"
2024/11/11 17:26:55 [crit] 3326#3326: *5 stat() "/home/ncloud/whiteboard/client/dist/index.html" failed (13: Permission denied), client: 121.136.217.193, server: 223.130.137.162, request: "GET /favicon.ico HTTP/1.1", host: "223.130.137.162", referrer: "http://223.130.137.162/"
2024/11/11 17:26:55 [crit] 3326#3326: *5 stat() "/home/ncloud/whiteboard/client/dist/index.html" failed (13: Permission denied), client: 121.136.217.193, server: 223.130.137.162, request: "GET /favicon.ico HTTP/1.1", host: "223.130.137.162", referrer: "http://223.130.137.162/"
2024/11/11 17:26:55 [crit] 3326#3326: *5 stat() "/home/ncloud/whiteboard/client/dist/index.html" failed (13: Permission denied), client: 121.136.217.193, server: 223.130.137.162, request: "GET /favicon.ico HTTP/1.1", host: "223.130.137.162", referrer: "http://223.130.137.162/"
2024/11/11 17:26:55 [crit] 3326#3326: *5 stat() "/home/ncloud/whiteboard/client/dist/index.html" failed (13: Permission denied), client: 121.136.217.193, server: 223.130.137.162, request: "GET /favicon.ico HTTP/1.1", host: "223.130.137.162", referrer: "http://223.130.137.162/"
2024/11/11 17:26:55 [crit] 3326#3326: *5 stat() "/home/ncloud/whiteboard/client/dist/index.html" failed (13: Permission denied), client: 121.136.217.193, server: 223.130.137.162, request: "GET /favicon.ico HTTP/1.1", host: "223.130.137.162", referrer: "http://223.130.137.162/"
2024/11/11 17:26:55 [crit] 3326#3326: *5 stat() "/home/ncloud/whiteboard/client/dist/index.html" failed (13: Permission denied), client: 121.136.217.193, server: 223.130.137.162, request: "GET /favicon.ico HTTP/1.1", host: "223.130.137.162", referrer: "http://223.130.137.162/"
2024/11/11 17:26:55 [crit] 3326#3326: *5 stat() "/home/ncloud/whiteboard/client/dist/index.html" failed (13: Permission denied), client: 121.136.217.193, server: 223.130.137.162, request: "GET /favicon.ico HTTP/1.1", host: "223.130.137.162", referrer: "http://223.130.137.162/"
2024/11/11 17:26:55 [error] 3326#3326: *5 rewrite or internal redirection cycle while internally redirecting to "/index.html", client: 121.136.217.193, server: 223.130.137.162, request: "GET /favicon.ico HTTP/1.1", host: "223.130.137.162", referrer: "http://223.130.137.162/"
  • nginx๊ฐ€ dist ํด๋”์˜ ํŒŒ์ผ๋“ค์— ์ ‘๊ทผํ•  ๊ถŒํ•œ์ด ์—†๋‹ค๋Š” ์˜๋ฏธ

ํ•ด๊ฒฐ๋ฐฉ๋ฒ•

sudo chmod 755 /home/ncloud
  • /home/ncloud ๋””๋ ‰ํ† ๋ฆฌ๋ถ€ํ„ฐ ์ ‘๊ทผ ๊ถŒํ•œ์ด ํ•„์š”
    • Nginx๊ฐ€ /home/ncloud/whiteboard/client/dist/index.html์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์œผ๋ ค๋ฉด ์ƒ์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ์—๋„ Nginx๊ฐ€ ์ฝ๊ธฐ ๋ฐ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์ด ํ•„์š”ํ•˜๋‹ค.

ํ”„๋ก ํŠธ์—”๋“œ ๋นŒ๋“œ ๋ฐ ๋ฐฑ์—”๋“œ ์žฌ์‹œ์ž‘

# ํ”„๋ก ํŠธ์—”๋“œ ๋‹ค์‹œ ๋นŒ๋“œ
cd ~/whiteboard/client
npm run build

# ๋ฐฑ์—”๋“œ ์žฌ์‹œ์ž‘
cd ~/whiteboard/server
npm run build
pm2 restart whiteboard-server

2. Object Storage ํ™œ์šฉ

image (13)

  • AWS์˜ S3์™€ ์œ ์‚ฌ

๊ธฐ๋ณธ ์ •๋ณด

image (14)

  • ์ƒ์„ฑ๋œ ๋ฒ„ํ‚ท์˜ ์ด๋ฆ„์€ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅ
  • ๋ฒ„ํ‚ท ์ด๋ฆ„์€ ๊ฐ์ฒด์— ๋Œ€ํ•œ ๋„๋ฉ”์ธ์— ํ™œ์šฉ

์„ค์ • ๊ด€๋ฆฌ

image (15)

๊ถŒํ•œ ๊ด€๋ฆฌ

image (16)

  • ๋ฐฐํฌํ• ๊ฑฐ๋ฉด ๋ฐ˜๋“œ์‹œ ๊ณต๊ฐœ

์ •์  ์›น์‚ฌ์ดํŠธ ํ˜ธ์ŠคํŒ…

image (17)

๋ฒ„ํ‚ท ์›น ์‚ฌ์ดํŠธ ์—”๋“œ ํฌ์ธํŠธ

http://test-web30.s3-website.kr.object.ncloudstorage.com

์„œ๋ฒ„ ์ฝ”๋“œ ์ˆ˜์ •

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.enableCors({
    origin: 'http://test-web30.s3-website.kr.object.ncloudstorage.com',
    credentials: true,
  });

  app.useWebSocketAdapter(new IoAdapter(app));

  await app.listen(3000);
}
bootstrap();

ํด๋ผ์ด์–ธํŠธ ์ฝ”๋“œ ์ˆ˜์ •

const newSocket = io('http://223.130.137.162:3000', {
    transports: ['websocket'],
    reconnection: true,
});

๋นŒ๋“œ ํŒŒ์ผ ์—…๋กœ๋“œ

image (18)

  • Nginx ์ค‘์ง€

    sudo systemctl stop nginx
    sudo systemctl disable nginx
    
    # ์ƒํƒœ ํ™•์ธ
    sudo systemctl status nginx

Global CDN

  • ํ•„์š”ํ•  ๋•Œ, ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜
  • ์ปค์Šคํ…€ ๋„๋ฉ”์ธ ์„ค์ • ๊ฐ€๋Šฅ

์ฐธ๊ณ 

  • nginx: Linux packages
  • ์š”์ฒญ์„ ๋ณด๋ƒˆ๋Š”๋ฐ ์•„๋ฌด ๋ฐ˜์‘์ด ์—†๋‹ค?
    • ํฌํŠธ ๋ฌธ์ œ์ผ ๊ฐ€๋Šฅ์„ฑ์ด ํผ
    • ACG ๊ด€๋ฆฌ ์ž˜ ํ•˜์ž

๐Ÿ˜Ž ์›จ๋ฒ ๋ฒ ๋ฒ ๋ฒฑ

๐Ÿ‘ฎ๐Ÿป ํŒ€ ๊ทœ์น™

๐Ÿ’ป ํ”„๋กœ์ ํŠธ

๐Ÿชต ์›จ๋ฒ ๋ฒฑ ๊ธฐ์ˆ ๋กœ๊ทธ

๐Ÿช„ ๋ฐ๋ชจ ๊ณต์œ 

๐Ÿ”„ ์Šคํ”„๋ฆฐํŠธ ๊ธฐ๋ก

๐Ÿ“— ํšŒ์˜๋ก

Clone this wiki locally