티스토리 뷰

Amazon EC2에 Node.js 설치하기

pilot376 2015. 12. 11. 14:17

EC2 Instance 생성하기

  • AWS(아마존 웹 서비스) 로그인 후 AWS Management Console 페이지로 이동한다. 그리고 EC2를 클릭한다.


  • 페이지 우측 상단 메뉴에서 지역을 선택한 후, Lunch Instance 버튼을 클릭한다.


  • 리스트 중 우분투를 선택한다. (다른 OS를 선택해도 상관없지만, 이 포스트에서는 우분투 기준으로 설명하고 있다.)


  • 서버의 상세 사양 리스트 화면이 나오면 원하는 사양을 선택한 후 Launch 버튼을 클릭한다.


  • key pair를 선택하는 화면이 나오면 키를 생성하고 (기존 키가 있으면 선택하고) Instance 생성을 완료한다.

    pem 키 파일은 나중에 EC2 Instance에 접근할 때 사용된다.


  • 조금 기다리면 Instance는 running 상태가 된다. Instance 리스트 메뉴에서 새로 생성된 Instance의 정보를 확인할 수 있다. Connect 버튼을 클릭하면 서버에 접속하는 방법을 확인할 수 있다.



EC2 instance에 Node.js 설치하기

  • Node.js는 NodeSource repository를 이용하여 설치할 수 있다.

    # Node.js v4 버전 설치
    curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
    sudo apt-get install -y nodejs
    

    설치 후 Node 명령어를 입력하면 버전을 확인할 수 있다.


  • Optional: build tools 설치하기

    컴파일을 하거나 npm의 native addons를 설치하기 위해서 build tools를 설치해야 한다.

    sudo apt-get install -y build-essential
    

  • 자세한 설치방법 참조
    https://nodejs.org/en/download/package-manager/ (우분투 Instance로 생성하지 않은 경우, 각 OS별 설치 방법 참조)



nginx proxy server 설치하기

  • Node.js를 proxy server 뒤에서 실행하는 것은 보안이나 유용성 면에서 좋은 방법이다. 아래의 명령어로 nginx를 설치한다.

    sudo apt-get install nginx
    

  • nginx로 요청이 들어오면 Node.js server로 보내주는 설정이 필요하다. vi로 설정파일을 연다.

    sudo vi /etc/nginx/sites-enabled/default
    

  • “location /“ 부분에 "proxy_pass http://127.0.0.1:3000/;" 구문을 추가한다. "try_files $uri $uri/ =404;" 구문을 주석 처리하면 node에서 router까지 처리하게 된다.

    server {
            listen 80 default_server;
            listen [::]:80 default_server ipv6only=on;
    
            root /usr/share/nginx/html;
            index index.html index.htm;
    
            # Make site accessible from http://localhost/
            server_name localhost;
    
            location / {
                    proxy_pass http://127.0.0.1:3000/;
                    # First attempt to serve request as file, then
                    # as directory, then fall back to displaying a 404.
                    try_files $uri $uri/ =404;
                    # Uncomment to enable naxsi on this location
                    # include /etc/nginx/naxsi.rules
            }
    
    ... 생략
    

  • 위 과정을 마무리하면 Node.js 서버의 3000 포트로 요청을 전달한다. 설정을 적용하려면 nginx의 재시작이 필요하다.

    sudo /etc/init.d/nginx restart
    


Node.js 서버 코드 작성

  • 3000 포트에서 응답하는 Node.js 서버 코드를 작성한다. 원하는 이름으로 폴더를 만들고 이동한다.

    mkdir ~/www
    cd ~/www
    

  • 새로운 파일을 생성한다.

    sudo vi app.js
    

  • 코드를 작성한다.

    var http = require('http');
    
    http.createServer(function (req, res) {
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end('Hello World\n'); 
    }).listen(3000, "127.0.0.1");
    
    console.log('Server running at http://127.0.0.1:3000/');
    

  • Node.js 앱을 실행시킨다.

    node app.js
    

    서버가 실행되면서 코드에 입력한 로그를 확인할 수 있다.



Security Groups 추가하기

  • 서버는 실행되었지만, 브라우저 주소창에 Public DNS 또는 Public IP를 입력해도 화면이 나오지 않는다면 Security Groups 수정이 필요하다. Instance 리스트 화면에서 Security Groups를 클릭한다.


  • 하단 Inbound 탭에서 Edit 버튼을 클릭한 후 http 80 포트를 추가한다.


  • Instance의 Public DNS 또는 Public IP를 브라우저 주소창에 입력하면 "Hello World"가 출력된다.



댓글