Tutorial: Setting Up Node.js on an Amazon EC2 Instance

A common scenario for using Node.js with the SDK for JavaScript is to set up and run a Node.js web application on an Amazon Elastic Compute Cloud (Amazon EC2) instance. In this tutorial, you will create a Linux instance, connect to it using SSH, and then install Node.js to run on that instance.

Prerequisites

This tutorial assumes that you have already launched a Linux instance with a public DNS name that is reachable from the Internet and to which you are able to connect using SSH. For more information, see Step 1: Launch an Instance in the Amazon EC2 User Guide for Linux Instances.

You must also have configured your security group to allow SSH (port 22), HTTP (port 80), and HTTPS (port 443) connections. For more information about these prerequisites, see Setting Up with Amazon EC2 in the Amazon EC2 User Guide for Linux Instances.

Procedure

The following procedure helps you install Node.js on an Amazon Linux instance. You can use this server to host a Node.js web application.

To set up Node.js on your Linux instance

  1. Connect to your Linux instance as ec2-user using SSH.

  2. Install the current version of node version manager (nvm) by typing the following at the command line to install version 33.8.

    curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash

    We will use nvm to install Node.js because nvm can install multiple versions of Node.js and allow you to switch between them. See the nvm repo on GitHub for the current version to install.

  3. Activate nvm by typing the following at the command line.

    . ~/.nvm/nvm.sh
  4. Use nvm to install the version of Node.js you intend to use by typing the following at the command line.

    nvm install 6.11.5

    To install the latest LTS (long-term-support) release of Node.js, type the following at the command line.

    nvm install --lts

    Installing Node.js also installs the Node Package Manager (npm) so you can install additional modules as needed.

  5. Test that Node.js is installed and running correctly by typing the following at the command line.

    node -e "console.log('Running Node.js ' + process.version)"

    This should display the following message that confirms the installed version of Node.js running.

    Running Node.js v6.11.5


Creating an Amazon Machine Image

After you install Node.js on an Amazon EC2 instance, you can create an Amazon Machine Image (AMI) from that instance. Creating an AMI makes it easy to provision multiple Amazon EC2 instances with the same Node.js installation. For more information about creating an AMI from an existing instance, see Creating an Amazon EBS-Backed Linux AMI in the Amazon EC2 User Guide for Linux Instances.

Installing Node.js on Enterprise Linux

For instructions on how to install Node.js on any Red Hat® Enterprise Linux® / RHEL, CentOS and Fedora or other distributions, see the following Node.js documentation at https://nodejs.org/en/download/package-manager/.





Hello world 예제

기본적으로 이 앱은 여러분이 작성할 수 있는 가장 간단한 Express 앱일 것입니다. 이 앱은 하나의 파일로 된 앱이며 Express 생성기를 통해 얻게 되는 앱과는 같지 않습니다. (이 예제와 달리 Express 생성기를 통해 얻게 되는 앱은 다양한 목적을 위한 여러 JavaScript 파일, Jade 템플리트 및 하위 디렉토리를 포함하는 전체 앱에 대한 스캐폴딩을 작성합니다.)

먼저, myapp이라는 이름의 디렉토리를 작성한 후 이 디렉토리로 이동하여 npm init를 실행하십시오. 이후 설치 안내서에 따라 express를 종속 항목으로서 설치하십시오.

myapp 디렉토리에 app.js라는 이름의 파일을 작성한 후 다음과 같은 코드를 추가하십시오.



1
2
3
4
5
6
7
8
9
10
11
var express = require('express');
var app = express();
 
app.get('/'function (req, res) {
  res.send('Hello World!');
});
 
app.listen(3000function () {
  console.log('Example app listening on port 3000!');
});
 




앱은 서버를 시작하며 3000번 포트에서 연결을 청취합니다. 앱은 루트 URL(/) 또는 라우트에 대한 요청에 “Hello World!”로 응답합니다. 다른 모든 경로에 대해서는 404 Not Found로 응답합니다.

req(요청) 및 res(응답)는 Node가 제공하는 동일한 오브젝트이며, 따라서 req.pipe()req.on('data', callback) 그리고 Express의 관여가 필요 없는 다른 모든 항목을 호출할 수 있습니다.

다음의 명령을 이용하여 앱을 실행하십시오.


$ node app.js





ref : https://docs.aws.amazon.com/ko_kr/sdk-for-javascript/v2/developer-guide/setting-up-node-on-ec2-instance.html

ref : http://expressjs.com/ko/starter/hello-world.html

+ Recent posts