Ubuntu+nginxでnode.js+npmのインストールからデプロイまで

作業ログ。あんまり日本語でまとまってなかったので。
Apacheが稼働しているサーバーで何度も試行錯誤したくなかったのでnginxを入れた。nodejsとnginxは相性がいいらしい。
Heroku等でもホスティングしてるようだが、あえて自鯖で動かしてみる。

# githubからnodejs のインストール
$ git clone git://github.com/ry/node.git
$ cd node
$ ./configure
$ make 
$ make test 
$ sudo make install

# npm(nodejs用のパッケージライブラリ)のインストール
$ sudo chown -R $USER /usr/local # インストールスクリプトの実行のためにownerに
$ curl http://npmjs.org/install.sh | sh
$ sudo chown -R root /usr/local # 元に戻す
$ sudo npm install express # パッケージのインストール

# nginxのインストール
$ sudo apt-get install nginx 

node.jsのサンプル

~/workplace/mynode.js

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

$ sudo node ~/workplace/mynode.js
localhost:8124で起動する。http://localhost:8124/Hello World がみえているはず。

nginxの設定

Apacheとの競合を避けるのにポートは81を使う。事前にあけておく。
nginxの設定は /etc/nginx/sites-available/default

server { 
  listen 81; 
  server_name localhost;                    
  location / { 
    server_tokens off; 
    add_header    Server "Wheat (node.JS)"; 
    proxy_pass http://127.0.0.1:8124; 
  } 
} 

編集してから $ sudo /etc/init.d/nginx restart
あとは http://hogehoge.com:81/ にアクセスすれば外部から見える