φ(.. ) 備忘録
   
  
ラベル WSL の投稿を表示しています。 すべての投稿を表示
ラベル WSL の投稿を表示しています。 すべての投稿を表示

2021年9月26日日曜日

既存UbuntuサーバをWindows10のWSL2に置換する!(其の六)

 其の六:WSL2にapache2とMySQL、phpmyadminをインストールする

今回は、WSL2のUbuntu 20.04 LTSに、apache2(ウェブサーバ)とMySQL、phpmyadminをインストールする。非常に簡単だった。


■apache2(ウェブサーバ)のインストール

パッケージをインストールして、起動用のバッチを編集。

$ sudo apt install apache2
$ vi wsl2_apach2_start.bat
起動用のバッチの内容は以下。Windows10側でportproxyの設定とWSL側でapache2を起動する。なぜかlistenport=80でアクセスできなかったので、listenport=3000で実施する。
@echo off
for /f "usebackq" %%t in (`wsl -d Ubuntu-20.04 exec hostname -I`) do set IP=%%t
netsh.exe interface portproxy delete v4tov4 listenport=3000
netsh.exe interface portproxy add v4tov4 listenport=3000 connectaddress=%IP% connectport=80
wsl -d Ubuntu-20.04 -u root service apache2 start
上記のバッチを管理者権限で実行すると、localhost:3000でアクセスできる。

■MySQLのインストール

パッケージをインストール。
$ sudo apt install mysql-server
次に、mysqlを起動し、rootのパスワード設定。
$ sudo service mysql start
$ sudo mysql_secure_installation
起動用バッチ(wsl2_mysql_start.bat)を作成。内容は以下。
@echo off
wsl -d Ubuntu-20.04 -u root service mysql start
PC再起動時は上記起動用バッチを管理者権限で実行すれば、MySQLを起動できる。

■phpmyadminインストール

パッケージをインストール。
$ sudo apt install phpmyadmin
コンフィグレーションが実行されるので、質問に答える。Webサーバは先ほどインストールしたapache2を選択しOK。


あとは、全部YesでOK(途中でパスワードを聞かれるので、パスワードも入力)。
コンフィグレーションを正しく設定できるとapache2側の/etc/apache2/conf-availableと/etc/apache2/conf-enabledにphpmyadmin設定が自動で設定されるので、apache2を再起動。
$ sudo service apache2 restart
apache2再起動後、localhost:3000/phpmyadminでアクセスできる。ユーザ名はphpmyadmin、パスワードはコンフィグレーション時に設定したもの。

2021年9月23日木曜日

既存UbuntuサーバをWindows10のWSL2に置換する!(其の五)

其の五:hubotの文字化けを直す

前回、hubotが無事インストールできたので、文字化けを対策する。

hubotで扱う文字コードはUTF-8が前提となっており、会社で使っているISO-2022-JPのチャットサーバーだと、文字化けを起こしてしまう。

以前perlのnkfモジュールを用いて対策を行ったことがあったが、coffeeスクリプトで対策できることが分かったので、そちらで対策する。

まずは、hubotをインストールしたディレクトリに移動して、必要なモジュール(jschardet、iconv)を追加インストールする。

$ cd ~/hubot
$ npm install jschardet
$ npm install iconv
次に、irc.coffeeに修正を加える。
$ cd ~/hubot/node_modules/hubot-irc/src
$ cp irc.coffee irc.coffee.org
$ vi irc.coffee
修正内容(diffイメージ)は以下で、最初にrequireで追加したモジュールをロード。
日本語のチャンネル名が指定された場合に備えて、チャンネル名をUTF8からISO-2022-JPに変更する処理を追加。
sendメソッドとnoticeメソッドに文字コードをUTF-8からISO-2022-JPに変更する処理を追加。messageやchannelを受け取るbotListener(message,notice等)に受け取った文字をUTF-8に変更する処理を追加。
ちなみに、iconv生成時のパラメータで指定している『TRANSLIT//IGNORE』はコード変換失敗時にエラーを無視するためのおまじないみたい。
 # Irc library
 Irc = require 'irc'

+# for ISO-2022-JP
+Jschardet = require 'jschardet'
+Iconv = (require 'iconv').Iconv
+
 class IrcBot extends Adapter
   send: (envelope, strings...) ->
     # Use @notice if SEND_NOTICE_MODE is set
@@ -15,6 +19,8 @@
       return console.log "ERROR: Not sure who to send to. envelope=", envelope

     for str in strings
+      iconv = new Iconv('UTF-8','ISO-2022-JP//TRANSLIT//IGNORE')
+      str = iconv.convert(str).toString()
       @bot.say target, str

   topic: (envelope, strings...) ->
@@ -52,6 +58,8 @@
       if not str?
         continue

+      iconv = new Iconv('UTF-8','ISO-2022-JP//TRANSLIT//IGNORE')
+      str = iconv.convert(str).toString()
       @bot.notice target, str

   reply: (envelope, strings...) ->
@@ -131,11 +139,15 @@

     do @checkCanStart

+    iconv = new Iconv('UTF-8','ISO-2022-JP//TRANSLIT//IGNORE')
+    roomsValue = iconv.convert(process.env.HUBOT_IRC_ROOMS).toString()
+
     options =
       nick:     process.env.HUBOT_IRC_NICK or @robot.name
       realName: process.env.HUBOT_IRC_REALNAME
       port:     process.env.HUBOT_IRC_PORT
-      rooms:    process.env.HUBOT_IRC_ROOMS.split(",")
+#      rooms:    process.env.HUBOT_IRC_ROOMS.split(",")
+      rooms:    roomsValue.split(",")
       ignoreUsers: process.env.HUBOT_IRC_IGNORE_USERS?.split(",") or []
       server:   process.env.HUBOT_IRC_SERVER
       password: process.env.HUBOT_IRC_PASSWORD
@@ -189,15 +201,27 @@

     if options.connectCommand?
       bot.addListener 'registered', (message) ->
+        detectResult = Jschardet.detect(message)
+        iconv = new Iconv(detectResult.encoding,'UTF-8//TRANSLIT//IGNORE')
+        message = iconv.convert(message).toString()
+
         # The 'registered' event is fired when you are connected to the server
         strings = options.connectCommand.split " "
         self.command strings.shift(), strings...

     bot.addListener 'names', (channel, nicks) ->
+      detectResult = Jschardet.detect(channel)
+      iconv = new Iconv(detectResult.encoding,'UTF-8//TRANSLIT//IGNORE')
+      channel = iconv.convert(channel).toString()
+
       for nick of nicks
         self.createUser channel, nick

     bot.addListener 'notice', (from, to, message) ->
+      detectResult = Jschardet.detect(message)
+      iconv = new Iconv(detectResult.encoding,'UTF-8//TRANSLIT//IGNORE')
+      message = iconv.convert(message).toString()
+
       if from in options.ignoreUsers
         console.log('Ignoring user: %s', from)
         # we'll ignore this message if it's from someone we want to ignore
@@ -209,6 +233,10 @@
       self.receive new TextMessage(user, message)

     bot.addListener 'message', (from, to, message) ->
+      detectResult = Jschardet.detect(message)
+      iconv = new Iconv(detectResult.encoding,'UTF-8//TRANSLIT//IGNORE')
+      message = iconv.convert(message).toString()
+
       if options.nick.toLowerCase() == to.toLowerCase()
         # this is a private message, let the 'pm' listener handle it
         return
@@ -231,6 +259,10 @@
       self.receive new TextMessage(user, message)

     bot.addListener 'action', (from, to, message) ->
+      detectResult = Jschardet.detect(message)
+      iconv = new Iconv(detectResult.encoding,'UTF-8//TRANSLIT//IGNORE')
+      message = iconv.convert(message).toString()
+
       console.log " * From #{from} to #{to}: #{message}"

       if from in options.ignoreUsers
@@ -247,9 +279,17 @@
       self.receive new TextMessage(user, message)

     bot.addListener 'error', (message) ->
+      detectResult = Jschardet.detect(message)
+      iconv = new Iconv(detectResult.encoding,'UTF-8//TRANSLIT//IGNORE')
+      message = iconv.convert(message).toString()
+
       console.error('ERROR: %s: %s', message.command, message.args.join(' '))

     bot.addListener 'pm', (nick, message) ->
+      detectResult = Jschardet.detect(message)
+      iconv = new Iconv(detectResult.encoding,'UTF-8//TRANSLIT//IGNORE')
+      message = iconv.convert(message).toString()
+
       console.log('Got private message from %s: %s', nick, message)

       if process.env.HUBOT_IRC_PRIVATE
@@ -267,21 +307,37 @@
       self.receive new TextMessage({reply_to: nick, name: nick}, message)

     bot.addListener 'join', (channel, who) ->
+      detectResult = Jschardet.detect(channel)
+      iconv = new Iconv(detectResult.encoding,'UTF-8//TRANSLIT//IGNORE')
+      channel = iconv.convert(channel).toString()
+
       console.log('%s has joined %s', who, channel)
       user = self.createUser channel, who
       user.room = channel
       self.receive new EnterMessage(user)

     bot.addListener 'part', (channel, who, reason) ->
+      detectResult = Jschardet.detect(channel)
+      iconv = new Iconv(detectResult.encoding,'UTF-8//TRANSLIT//IGNORE')
+      channel = iconv.convert(channel).toString()
+
       console.log('%s has left %s: %s', who, channel, reason)
       user = self.createUser '', who
       user.room = channel
       self.receive new LeaveMessage(user)

     bot.addListener 'kick', (channel, who, _by, reason) ->
+      detectResult = Jschardet.detect(channel)
+      iconv = new Iconv(detectResult.encoding,'UTF-8//TRANSLIT//IGNORE')
+      channel = iconv.convert(channel).toString()
+
       console.log('%s was kicked from %s by %s: %s', who, channel, _by, reason)

     bot.addListener 'invite', (channel, from) ->
+      detectResult = Jschardet.detect(channel)
+      iconv = new Iconv(detectResult.encoding,'UTF-8//TRANSLIT//IGNORE')
+      channel = iconv.convert(channel).toString()
+
       console.log('%s invited you to join %s', from, channel)

       if from in options.ignoreUsers

本来なら、環境変数もしくはコマンドパラメータで変換する文字コードを指定すべきだが、今回はそこまでやってない。
上記修正が正しく動作するか確認するために、hubot-scriptsのhttp-post-say.coffeeを使ったため、それの設定方法も残しておく。
まず、hubotインストールディレクトリに移動して、hubot-scripts.jsonを編集。
$ cd ~/hubot
$ vi hubot-scripts.json
hubot-scripts.jsonの内容は以下。他に追加したいスクリプトがあればカンマ(,)区切りで追加すればよい。
["http-post-say.coffee"]
hubotの待ち受けポートは、デフォルト8080になっている。変更する場合には、以下のようにhubot起動スクリプトに環境変数PORTの設定処理を追加し、起動すればよい。
$ vi runbot.sh
#!/bin/bash
# hubot http port setting
export PORT=9999
# runhubot
export HUBOT_IRC_NICK="hubot"
export HUBOT_IRC_ROOMS="#test01,#test02"
export HUBOT_IRC_SERVER="localhost"
#export HUBOT_IRC_PASSWORD="hoge"
bin/hubot -a irc
$ .runbot.sh 
http-post-say.coffeeを使って、http経由でhubotに発言させるにはcurlコマンド等で以下のようにすればよい。
$ curl -X POST http://localhost:9999/hubot/say -d message=文字化けた? -d room='#test01'
ISO-2022-JPのチャットサーバって、世の中一般では、レアなんですかね。。。うちの会社だけなのかなぁ。。。。

あと、おまけで、hubotの発言をnoticeにする方法がわかった。すごく簡単で、起動時に環境変数HUBOT_IRC_SEND_NOTICE_MODEにtrueを設定するだけだった。イメージは以下。
#!/bin/bash
# hubot http port setting
export PORT=9999
# runhubot
export HUBOT_IRC_NICK="hubot"
export HUBOT_IRC_ROOMS="#test01,#test02"
export HUBOT_IRC_SERVER="localhost"
#export HUBOT_IRC_PASSWORD="hoge"
export HUBOT_IRC_SEND_NOTICE_MODE="true"
bin/hubot -a irc

2021年9月22日水曜日

既存UbuntuサーバをWindows10のWSL2に置換する!(其の四)

   其の四:wsl2にhubotをインストールする

いよいよ本格的な移行作業として、wsl2のUbuntu 20.04 LTSにhubotをインストールする。いろんなサイトを参考にインストールしてみたがうまくいかず、かなり試行錯誤を繰り返したが、ようやくIRCチャットルームにbotをログインさせるところまで来たので、その手順を紹介する。

まず、hubotに必要なパッケージをインストールする。

$ sudo apt-get install nodejs npm coffeescript libicu-dev
次に、hubotをgit clone後、任意でディレクトリ名を変更し、ディレクトリを移動する。
$ git clone https://github.com/jgable/hubot-irc-runnable
$ mv hubot-irc-runnable hubot
$ cd hubot
次に、npmインストール実行。
$ npm install
次に、hubot-scripts.jsonを一応バックアップして編集。hubot-scripts.jsonうち、使用しないスクリプトを削除する。
$ cp hubot-scripts.json hubot-scripts.json.org
$ vi hubot-scripts.json
今回は全部つかわないので、以下の内容に変更。
[]
次に起動スクリプト(runbot.sh)を編集。
$ vi runbot.sh
内容は以下。ニックネームとかJOINするチャンネルとかは適当に変えてください。尚、同一WSL2内のチャットサーバに接続する場合には、以下設定(localhost)で接続できた。redis brainは今回使わないので起動しているところをコメントアウト。
#!/usr/bin/env sh

# Set Environment Variables
export HUBOT_IRC_NICK=hubot
export HUBOT_IRC_SERVER=localhost
export HUBOT_IRC_ROOMS="#test01,#test02"

# Using SSL?
#export HUBOT_IRC_PORT=6697
#export HUBOT_IRC_USESSL=true
#export HUBOT_IRC_SERVER_FAKE_SSL=true

# Server password?
#export HUBOT_IRC_PASSWORD=password

# Don't let hubot flood the room.
export HUBOT_IRC_UNFLOOD=true

# Output environment variables
echo HUBOT_IRC_NICK=$HUBOT_IRC_NICK
echo HUBOT_IRC_SERVER=$HUBOT_IRC_SERVER
echo HUBOT_IRC_ROOMS=$HUBOT_IRC_ROOMS

#echo HUBOT_IRC_PORT=$HUBOT_IRC_PORT
#echo HUBOT_IRC_USESSL=$HUBOT_IRC_USESSL
#echo HUBOT_IRC_SERVER_FAKE_SSL=$HUBOT_IRC_SERVER_FAKE_SSL
#echo HUBOT_IRC_PASSWORD=$HUBOT_IRC_PASSWORD

# Start the redis brain
#echo ""
#echo "Starting Redis Server"
#/usr/local/bin/redis-server > /dev/null &

echo ""
echo "Starting bot"
./bin/hubot -a irc

実行権を付与。
  $ chmod 755 runbot.sh
スクリプトを起動。正常に接続できると以下の応答がコンソールに出力される。
hubot has joined #test01
hubot has joined #test02
以上でインストールと起動は完了。ただし、hubotの文字コードはUTF-8が前提となっているため、ISO-2022-JP(Shift-jis)が前提のサーバだと、文字化けする。次回はこの文字化けを解消する。

既存UbuntuサーバをWindows10のWSL2に置換する!(其の三)

  其の三:wsl2にIRCチャットサーバをインストールする

hubotのwsl2移行のために、テスト用のIRCチャットサーバをwsl2で立ち上げる。手順は非常に簡単。

まず、wsl2のUbuntuにngircdをインストールする。

$ sudo apt install ngircd

次に、portproxyの設定とUbuntuのngircdサービスを起動するため、以下のバッチを作成する。バッチでは、wsl側のUbuntuが使用しているIPアドレスをwslコマンドで取得し、変数に設定。それを使って、portproxyを設定後、root権限でngircdサービスを起動する。

@echo off
for /f "usebackq" %%t in (`wsl -d Ubuntu-20.04 exec hostname -I`) do set IP=%%t
netsh.exe interface portproxy delete v4tov4 listenport=6667
netsh.exe interface portproxy add v4tov4 listenport=6667 connectaddress=%IP%
wsl -d Ubuntu-20.04 -u root service ngircd start

上記バッチをC:\Users\ユーザ名\tools\wsl\wsl2_ngircd_start.batとかに保存し、管理者権限で実行する。

これで、limechatとかでサーバをlocalhost:6667でつなげば、チャットが使用できる。

外部からのアクセスを許可する場合には、Windows Defender ファイアウォールの「受信の規則」でTCPポート6667番を許可に設定する必要があります。ご注意を!

 

2021年9月21日火曜日

既存UbuntuサーバをWindows10のWSL2に置換する!(其の二)

 其の二:wsl2にCygwinからsshログインする

まず、wsl2のUbuntuにopensshをインストールする。

$ sudo apt install openssh-server

次に、以下コマンドで鍵を作成する。

$ sudo ssh-keygen -A

次に、/etc/ssh/sshd_configのPasswordAuthenticationをyesに変更する。これをやらないと『Permission denied(publickey)』で怒られる。

PasswordAuthentication yes

次に、portproxyの設定とUbuntuのsshdサービスを起動するため、以下のバッチを作成する。バッチでは、wsl側のUbuntuが使用しているIPアドレスをwslコマンドで取得し、変数に設定。それを使って、portproxyを設定後、root権限でsshサービスを起動する。

@echo off
for /f "usebackq" %%t in (`wsl -d Ubuntu-20.04 exec hostname -I`) do set IP=%%t
netsh.exe interface portproxy delete v4tov4 listenport=22
netsh.exe interface portproxy add v4tov4 listenport=22 connectaddress=%IP%
wsl -d Ubuntu-20.04 -u root service ssh start

上記バッチをC:\Users\ユーザ名\tools\wsl\wsl2_ssh_start.batとかに保存し、管理者権限で実行する。

以上で、Cygwinからwslにsshログインできるようなる。
Windows起動時にこのバッチを起動するようタスクスケジューラに設定すれば、自動起動できると思う。
外部からのアクセスを許可する場合には、Windows Defender ファイアウォールの「受信の規則」でTCPポート22番を許可に設定する必要があります。ご注意を!






既存UbuntuサーバをWindows10のWSL2に置換する!(其の一)

其の一:WSL2のUbuntu 20.04 LTSをインストール

会社で使っている既存Ubuntuサーバの老朽化が心配・・・。Ubuntuサーバと言っても若手の転勤で不要となったデスクトップPCにUbuntuをインストールしただけの激安サーバである。ただし、もう7年くらい不具合なく常時稼働(ビル停電以外は常時ON)している。優秀。FUJITSU最高。

このサーバでは全文検索ツールとIRCチャットのbot(hubot)が動作しているため、故障に備えて、別なPCにあらかじめ環境を構築していつでも移行できるように作業を進めたい。

移行先は、別PCにUbuntuを入れるもしくは、仮想化マシンにする等あるが、今回はWindows10のWSL2に構築してみる。 

まず、WSL2のインストール。

  1. Windows->設定->プログラムと機能->Windowsの機能の有効化または無効化で『Linux用Windowsサブシステム』と『仮想マシンプラットフォーム』をチェックし再起動。
  2. Linux カーネル更新プログラム パッケージをダウンロードし、インストールする。https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi
  3. Windows PowerShellを起動し、『wsl --set-default-version 2』を実行し、WSL 2 を既定のバージョンとして設定する。
  4. MicroSoft Store からUbuntu 20.04 LTSをインストール。https://www.microsoft.com/store/apps/9n6svws3rx71
  5. スタートメニューからUbuntu 20.04 LTSを起動し、ユーザとパスワード設定。
  6. Windows PowerShellで『wsl -l -v』WSLの状態確認。STATEがRunning、VERSIONが2となっていればOK。
以上、これでWSL2のインストールは完了。詳しい手順は、https://docs.microsoft.com/ja-jp/windows/wsl/install-win10に書いてある。