EC2 に Node.js のインストール【Node.js, MongoDB インストールから Calipso インストールまで】

EC2 に Calipso のインストール手順を残すついでに Node.js, MongoDB のインストールも残しておきます。



1. サーバ設定 

ロケールを日本時間に設定

$ sudo ln -f /usr/share/zoneinfo/Japan /etc/localtime

アプリケーションディレクトリのユーザ権限をhiguchiに譲渡

$ sudo su -
# chown -R higuchi:higuchi /usr/local/
$ ls -l /usr/ | grep local
drwxr-xr-x 12 higuchi higuchi 4096 Nov 30 10:48 local

2. node.jsのインストール

node.jsのダウンロード

$ wget http://nodejs.org/dist/node-v0.4.3.tar.gz

展開

$ tar zxf node-v0.4.3.tar.gz

展開したディレクトリに移動

$ cd node-v0.4.3

configureしてみるがライブラリが足りない

$ ./configure
Checking for program g++ or c++ : not found
Checking for program icpc : not found
Checking for program c++ : not found
/usr/local/src/node-v0.4.3/wscript:232: error: could not configure a cxx compiler!

ライブラリgcc-c++のインストール

# yum install gcc-c++

ライブラリopenssl-develのインストール

# yum install openssl-devel

sudo抜けて再度configureしてみる

# exit
$ ./configure

大丈夫そうなのでmake、インストール

$ make
$ make install

起動確認

$ node -v
v0.4.3

JSをコマンドラインで実行してみる

$ node
> a = { a:1, b:2, c:3 }
{ a: 1, b: 2, c: 3 }
> a
{ a: 1, b: 2, c: 3 }
> a,a
{ a: 1, b: 2, c: 3 }
> a:a
{ a: 1, b: 2, c: 3 }
> a.a
1

Webサーバをつくってみる

$ vi example.js

// start
var http = require('http');

http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8124);

console.log('Server running at http://127.0.0.1:8124/');
// end

実行

$ node example.js
[1] 2304
Server running at http://127.0.0.1:8124/

ブラウザで確認する。
http://自サーバのIP:8124/

#ちなみに、EC2はデフォルト8124ポートを塞いでいるので解除しておく。