2011年10月31日月曜日

Amazon EC2を始める(node.js編)

経緯
Amazon EC2を始める(導入編)
Amazon EC2を始める(SSL編)
Amazon EC2を始める(SCP編)
Amazon EC2を始める(Apache編)
の続きです。

概要
Amazon EC2に作成したインスタンスにnode.jsをインストールした際の履歴です。

node.js
http://nodejs.org/

↓こちらを参考にしました。
Amazon EC2 Node.js, npm, Socket.IOをインストール

手順
1. node.jsをインストール
# 現時点の安定最新版
$ wget http://nodejs.org/dist/node-v0.4.12.tar.gz
# 必要なものをインストール
$ sudo yum -y install gcc-c++
$ sudo yum -y install openssl-devel

# configure
$ tar xvzf node-v0.4.12.tar.gz
$ cd node-v0.4.12
$ ./configure
Checking for program g++ or c++          : /usr/bin/g++ 
Checking for program cpp                 : /usr/bin/cpp 
Checking for program ar                  : /usr/bin/ar 
Checking for program ranlib              : /usr/bin/ranlib 
Checking for g++                         : ok  
Checking for program gcc or cc           : /usr/bin/gcc 
Checking for gcc                         : ok  
Checking for library dl                  : yes 
Checking for openssl                     : yes 
Checking for library util                : yes 
Checking for library rt                  : yes 
--- libeio ---
Checking for library pthread             : yes 
Checking for function pthread_create     : yes 
Checking for function pthread_atfork     : yes 
Checking for futimes(2)                  : yes 
Checking for readahead(2)                : yes 
Checking for fdatasync(2)                : yes 
Checking for pread(2) and pwrite(2)      : yes 
Checking for sendfile(2)                 : yes 
Checking for sync_file_range(2)          : yes 
--- libev ---
Checking for header sys/inotify.h        : yes 
Checking for function inotify_init       : yes 
Checking for header sys/epoll.h          : yes 
Checking for function epoll_ctl          : yes 
Checking for header port.h               : not found 
Checking for header poll.h               : yes 
Checking for function poll               : yes 
Checking for header ['sys/types.h', 'sys/event.h'] : not found 
Checking for header sys/queue.h                    : yes 
Checking for function kqueue                       : not found 
Checking for header sys/select.h                   : yes 
Checking for function select                       : yes 
Checking for header sys/eventfd.h                  : yes 
Checking for function eventfd                      : yes 
Checking for SYS_clock_gettime                     : yes 
Checking for library rt                            : yes 
Checking for function clock_gettime                : yes 
Checking for function nanosleep                    : yes 
Checking for function ceil                         : yes 
Checking for fdatasync(2) with c++                 : yes 
'configure' finished successfully (2.825s)

# make 入ってなかった><
$ make
 -bash: make: command not found
$ sudo yum -y install make

# make
$ make

# make install
$ sudo make install

# 入りました(*^-^)
$ node -v
v0.4.12

makeはマイクロのインスタンスだと重すぎてkillするハメになった方もいるようですが、 私のときは調子よくて18分で完了しました。
node.jsをAmazon EC2のmicroインスタンスに入れる時の注意

2. npmをインストール
npm ⇒ Node Package Manager

詳しくは↓こちらです。
http://npmjs.org/

$ sudo chown -R $USER /usr/local
$ curl http://npmjs.org/install.sh | sh
$ sudo chown -R root /usr/local
$ npm -v
1.0.103

# パスを通す(nodeコマンドもnpmコマンドも/usr/local/bin以下)
$ sudo visudo
Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin
# ↓↓↓↓
Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

# 試しにsocket.ioを入れてみる。
# socket.io
$ sudo npm install socket.io
socket.io@0.8.6 ./node_modules/socket.io 
├── policyfile@0.0.4
├── redis@0.6.7
└── socket.io-client@0.8.6

3. Hello worldを作成
Hello worldアプリを作って、nodeサーバーとして起動してみます。

$ vi example.js
var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(80, "");
console.log('Server running at ec2-xxx.ap-northeast-1.compute.amazonaws.com:80/');
# node.jsにexample.jsアプリを起動させます。
$ sudo node example.js 
# ⇒ エラーが起きました。
node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
Error: EADDRINUSE, Address already in use
    at Server._doListen (net.js:1100:5)
    at net.js:1071:14
    at Object.lookup (dns.js:153:45)
    at Server.listen (net.js:1065:20)
    at Object. (/home/ec2-user/example.js:5:4)
    at Module._compile (module.js:411:26)
    at Object..js (module.js:417:10)
    at Module.load (module.js:343:31)
    at Function._load (module.js:302:12)
    at Array. (module.js:430:10)

$ netstat -antpu
tcp        0      0 :::80                       :::*                        LISTEN      - 
# Apache立ちあげてました。

# Apache停止
$ sudo su root
#  /etc/rc.d/init.d/httpd stop
Stopping httpd:                                            [  OK  ]
# 自動起動設定を外します。
# chkconfig httpd off
# exit
exit

# リトライ
$ sudo node example.js 
Server running at ec2-xxx-xx-xxx-xxx.ap-northeast-1.compute.amazonaws.com:80/
# ⇒ 今度は成功しました。


4. ブラウザから確認
http://ec2-xxx-xx-xxx-xxx.ap-northeast-1.compute.amazonaws.com/

※ 自分のサーバーのドメインは「AWS Management Console」から確認できます。(Public DSN)

0 件のコメント:

コメントを投稿