Tech Tips

  1. プログラミング
  2. 193 view

[Node.js][Nginx]Node.jsのインストールと起動

Node.jsのインストール

Node.jsをインストールしてnginxをリバースプロキシとして動作させてみる。
まずはNode.jsをインストールしてみる。
# Install pythonbrew
$ curl -kL http://xrl.us/pythonbrewinstall | bash
$ vim ~/.bashrc
\[\[ -s $HOME/.pythonbrew/etc/bashrc \]\] && source $HOME/.pythonbrew/etc/bashrc
$ source ~/.bashrc
$ pythonbrew install 2.7.2
$ pythonbrew switch 2.7.2

#Install node.js
$ git clone https://github.com/creationix/nvm.git ~/.nvm
$ source ~/.nvm/nvm.sh
$ nvm install v0.10.26
$ vim ~/.bashrc
source ~/.nvm/nvm.sh
nvm use v0.10.26
$ source ~/.bashrc

「[[ -s $HOME/.pythonbrew/etc/bashrc ]]」のところはバックスラッシュを削除してください。 もしpythonbrewのダウンロード時に「Can not get Stable Version」というようなエラーが出た場合は、
pythonbrewのインストール部分を以下のように修正してみてください。
# Install pythonbrew
$ curl -kL http://xrl.us/pythonbrewinstall > pythonbrewinstaller.sh
$ vim pythonbrewinstaller.sh
STABLE_VERSION=`$CURL -sL${CURL_ADDITIONAL_PARAMETERS} https://github.com/utahta/pythonbrew/raw/master/stable-version.txt`
STABLE_VERSION=`trim $STABLE_VERSION`
if [[ -z "$STABLE_VERSION" ]] ; then
    echo 'Can not get stable-version of pythonbrew.'
    exit 1
fi
=>
#STABLE_VERSION=`$CURL -sL${CURL_ADDITIONAL_PARAMETERS} https://github.com/utahta/pythonbrew/raw/master/stable-version.txt`
#STABLE_VERSION=`trim $STABLE_VERSION`
#if [[ -z "$STABLE_VERSION" ]] ; then
#    echo 'Can not get stable-version of pythonbrew.'
#    exit 1
#fi
STABLE_VERSION='1.3.4'
--------
builtin cd $PATH_DISTS ; $CURL --progress-bar -L${CURL_ADDITIONAL_PARAMETERS} $DOWNLOAD_URL -o "$TEMP_TARBALL"
=>
builtin cd $PATH_DISTS ; $CURL --progress-bar -kL${CURL_ADDITIONAL_PARAMETERS} $DOWNLOAD_URL -o "$TEMP_TARBALL"
$ vim ~/.bashrc
$ \[\[ -s $HOME/.pythonbrew/etc/bashrc \]\] && source $HOME/.pythonbrew/etc/bashrc
$ source ~/.bashrc
$ pythonbrew install 2.7.2
$ pythonbrew switch 2.7.2

Node.js起動

まずはNginxの設定をする。
server {
    listen 80;

    server_name (IP addr or Server name);

    access_log (logpath);
    error_log (logpath);

    location / {
        proxy_pass http://localhost:8080/;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
}
次はnode.jsで動かすjsを書く。
var http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen(8080, '127.0.0.1');
console.log('Server running at http://127.0.0.1:8080/');
次のコマンドで実行してアクセスして、Hello Worldと表示されたらOK!
$ node sample.js

プログラミングの最近記事

  1. PlatformIO IDE for VSCode を使用して VSCode で Ardu…

  2. ROS Docker イメージで発生した GPG error の解消方法

  3. Streamlit で訪れた国を色づけした世界地図を作成できるアプリケーションを作成してみ…

  4. M5Stack Core2 for AWS – ESP32 IoT開発キットで…

  5. D3.js v7 で点・線・テキスト・ツールチップ・ズームを設定する方法

関連記事

PAGE TOP