さくらのVPSにnode.jsをインストールしました。
OSは標準のままのCentOS6です。
全てSSHでサーバにログインして作業をしています。
まずはnode.jsをインストール
”ここ”を参考にして作業を進めます。
ちなみに上記のページには、Node.js 日本ユーザグループの公式ページから
ドキュメント→v*.*.*(**版)→ダウンロードボタン→パッケージマネージャを利用したインストール
から行けます。
そこの、CentOSの項目から
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wget http://nodejs.tchol.org/repocfg/el/nodejs-stable-release.noarch.rpm | |
yum localinstall --nogpgcheck nodejs-stable-release.noarch.rpm | |
yum install nodejs-compat-symlinks npm | |
rm nodejs-stable-release.noarch.rpm |
1番上のwgetはrpmパッケージを取得しています。
2番目はローカルにあるrpmパッケージを使用してインストールを行なっています。
3番目は何をしているかよくわかりませんw 誰か教えてくださいm(_ _)m
4番目は掃除です。
2,3番目はルート権限が必要なので、適宜sudoして下さい。
インストールの確認
インストールが完了したら、インストールの確認をします。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node -v |
と打ち込んでバージョンが表示されれば大丈夫です。
2012/7/11現在ではv0.6.18がインストールされました。
最新版を使用したい人はソースコードからインストールするのが良さそうです。
サンプルプログラムの作成
サンプルプログラムを作ります。
僕がインストールしたのはv0.6系列なので
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
http.createServer(function (req, res) { | |
res.writeHead(200, {'Content-Type': 'text/plain'}); | |
res.end('Hello World\n'); | |
}).listen(1337, '127.0.0.1'); | |
console.log('Server running at http://127.0.0.1:1337/'); |
エディタで内容を写経して、exsample.jsというファイル名で保存します。
サンプルプログラムの実行
以下のコマンドを発行して、サーバを立ち上げます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node exsample.js |
動作確認
立ち上げたサーバにアクセスして正常に動作しているか確認します。
サーバを立ち上げているSSHのセッションとは別にもう一つセッションを作って、そちら側で以下のコマンドを発行します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
curl http://127.0.0.1:1377 |
すると、以下の文字列が返ってきます。
Hello World
これで正常動作していることが確認できました。
以上です。
0 件のコメント:
コメントを投稿