Node.js ์„œ๋ฒ„ ๊ตฌ์ถ• 1

AWS-Node.js ์„œ๋ฒ„ ๊ตฌ์ถ• ๊ณต๋ถ€.

Posted by Bangjinhyuk on February 14, 2021 · 2 mins read

aws ์„œ๋ฒ„ ๊ตฌ์ถ• ๋ฐ Node JS ์„ค์น˜ ์ฐธ์กฐ

  1. ์šฐ๋ถ„ํˆฌ ์„œ๋ฒ„ ์—…๋ฐ์ดํŠธ

    $ sudo apt-get update

  2. Node JS ์„ค์น˜

    $ sudo apt-get install nodejs

  3. Node Package Manager : NPM ์„ค์น˜

    $ sudo apt-get install npm

  4. express๋ฅผ ์„ค์น˜(-g๋Š” Global ์˜ต์…˜์œผ๋กœ ์–ด๋Š path์—์„œ๋‚˜ express๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ์˜ต์…˜)

    $ sudo npm install -g express

  5. Express generator ์„ค์น˜

    $ sudo npm install -g express-generator@4

  6. nodemon ์„ค์น˜ -์„ค๋ช… ์‚ฌ์ดํŠธ

    $ sudo npm install -g nodemon

  7. ์šฐ๋ถ„ํˆฌ ํด๋” ๋งŒ๋“œ๋Š” ๋ช…๋ น๋ฌธ

    $ sudo mkdir (๋งŒ๋“ค๊ณ  ์‹ถ์€ ํด๋” ์ด๋ฆ„1) && cd (๋งŒ๋“ค๊ณ  ์‹ถ์€ ํด๋” ์ด๋ฆ„1) && sudo mkdir (๋งŒ๋“ค๊ณ  ์‹ถ์€ ํด๋” ์ด๋ฆ„2) && cd (๋งŒ๋“ค๊ณ  ์‹ถ์€ ํด๋” ์ด๋ฆ„2)

  8. express ๊ธฐ๋ณธ template์„ ์ƒ์„ฑ (sudo ์•ˆ์“ธ์‹œ permission denied ๋œฐ์ˆ˜ ์žˆ์Œ)

    $ sudo express -e

  9. npm ์„ค์น˜

    $ sudo npm install

  10. app.jsใ€€์ˆ˜์ •

    $ sudo vi app.js

  11. app.jsใ€€์ˆ˜์ • ๋‚ด์šฉ

     // port setup
         app.set('port', process.env.PORT || 9000);
    
         module.exports = app;
    
         var server = app.listen(app.get('port'), function() {  
         console.log('Express server listening on port ' + server.address().port); 
    
  12. package.json ํŒŒ์ผ์˜ node ๋ชจ๋‹ˆํ„ฐ๋ง์„ ์œ„ํ•ด start ํ•ญ๋ชฉ ์ˆ˜์ •

    $ sudo vi package.json

  13. โ€œstartโ€ ํ•ญ๋ชฉ ์ˆ˜์ • ๋‚ด์šฉ

    "script":{
       "start": "nodemon app.js"
    },
    
  14. ์„œ๋ฒ„ ์‹คํ–‰

    $ npm start

  15. ์„œ๋ฒ„ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์‹คํ–‰ (ex. [1]1234 => process ID), ($ ps -ef => ํ”„๋กœ์„ธ์Šค ํ™•์ธ),(kill [processId] =>ํ”„๋กœ์„ธ์Šค kill)

    $ sudo nohup npm start &

  16. ์ž๋™ redirection (ํฌํŠธ๋ฅผ ์ž…๋ ฅํ•˜์ง€ ์•Š๊ณ  ๋„˜๊ธธ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๋ช…๋ น๋ฌธ)

    $ sudo iptables -A PREROUTING -t nat -i eth0 -p tcp โ€“dport 80 -j REDIRECT โ€“to-port 9000