AB Lab

abgata20000 blog.

Node.jsをnodebrewでインストール

nodebrewをインストール

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
curl https://raw.github.com/hokaccha/nodebrew/master/nodebrew | perl - setup

# ユーザーディレクトリにインストールされるので、全ユーザーで利用する場合はノードユーザー作ってインストールして共通パス通すのがいいかも

useradd node

cd /home/node

chmod -R 777 /home/node

echo 'export PATH="/home/node/.nodebrew/current/bin:$PATH"'     >> /etc/profile.d/nodebrew.sh
# echo 'export PATH="/home/node/.nodebrew/current/bin:$PATH"'     >> /etc/profile.d/nodebrew.sh

source /etc/profile.d/nodebrew.sh

echo $PATH

# ユーザー権限でも実行したい場合は実行グループ作って登録すれば大丈夫。

nodeグループを作成

1
2
3
4
groupadd node
usermod -G wheel,node myuser
# usermod -G wheel,node,ruby myuser
groups myuser

.nodebrewの所有グループを変更

1
2
3
cd /home/node
chgrp -R node .nodebrew
chmod -R g+rwxX .nodebrew

/home/nodeへの閲覧権限がなかったのでグループをwheelに変更

1
sudo chgrp wheel /root

インストール可能なバージョンの確認

1
2
nodebrew ls-all
# 奇数バージョンは開発版、偶数バージョンが安定版らしい

インストール(バイナリからインストールしたほうが速い)

1
2
nodebrew install-binary v0.10.20
# インストールはユーザーディレクトリにされるっぽいのでノードユーザーでインストールする

利用するバージョンを変更

1
nodebrew use v0.10.20

現在のバージョンを確認

1
node -v

サーバーサンプル

1
2
3
4
5
6
7
8
var http = require('http');

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

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