Hejdaの見る夢

一人前のエンジニアを目指して頑張ったこととかをつらつら書くブログ

Node-REDをVagrant上で立ち上げて、ブラウザで確認する

Qiita

Node-REDをVagrant上で立ち上げて、ブラウザで確認すると同内容です。

使用するvagrant box

# cat /etc/debian_version
9.5

構築

vagrantの起動

割愛

Debianの基本設定

  • ROOT化
sudo su -
  • aptの更新
apt-get update
apt-get upgrade -y
  • (今回使う)最低限のアプリのインストール
apt-get install -y vim
curl https://gist.githubusercontent.com/iganari/a2baec1af976425cc8e21ccd68cf5585/raw/b6fce2cc91e1c77da759af1d2ea7b771b597787f/_vimrc -o /root/.vimrc
ls -la /usr/bin/vim.basic 
unlink /etc/alternatives/editor
ln -s  /usr/bin/vim.basic /etc/alternatives/editor
apt --purge remove -y nano
  • サーバの時刻を日本にする
rm -rfv /etc/localtime &&\
ln -s /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
echo 'LANG="en_US.UTF-8"' > /etc/default/locale

nodejs version8 をインストールする

  • 既存のnodejsを削除して、公式からインストールスクリプトを用いてインストール
    • 意図したnodejsをインストールした後にNode-REDもインストール
apt remove --purge -y nodejs
curl -sL https://deb.nodesource.com/setup_8.x | /bin/bash
apt-get install -y nodejs
npm install -g --unsafe-perm node-red

作業ユーザ作成

  • 作業ユーザは node-red
username='node-red'
useradd -m -s /bin/bash ${username}
echo "${username} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/${username}
chmod 0440                                  /etc/sudoers.d/${username}
cp    /root/.vimrc            /home/${username}/.vimrc
chown ${username}:${username} /home/${username}/.vimrc

Node-RED をnpmからインストールする

  • 作業ユーザ node-red になる
su - node-red
  • 専用のディスクトリを作成し、node-redをインストール
mkdir         /opt/nodered-vagrant
chmod 0777 -R /opt/nodered-vagrant
cd            /opt/nodered-vagrant
npm install node-red

起動・終了スクリプトを設置

  • 再び、ROOT化
sudo su -
  • ダウンロード
wget "https://raw.githubusercontent.com/iganari/nodered-vagrant/master/bin/usr/bin/node-red-start" -O /usr/bin/node-red-start
wget "https://raw.githubusercontent.com/iganari/nodered-vagrant/master/bin/usr/bin/node-red-stop"  -O /usr/bin/node-red-stop
  • 権限変更
chmod 0755                /usr/bin/node-red-st* 
chown root:root           /usr/bin/node-red-st*
wget "https://raw.githubusercontent.com/iganari/nodered-vagrant/master/bin/etc/systemd/system/node-red.service" -O /etc/systemd/system/node-red.service
  • 権限変更
chmod 0755      /etc/systemd/system/node-red.service
chown root:root /etc/systemd/system/node-red.service
  • 起動
systemctl start node-red
systemctl enable node-red

ブラウザで確認

http://192.168.33.131:1880/

f:id:nari_kyu:20180820191504p:plain

ソース全体

簡単な構築方法含め、以下のレポジトリにまとめたので確認してみて下さい :blush:

https://github.com/iganari/nodered-vagrant