Ubuntu22.04でのMineOS環境構築

# 初めに
MineOSというMineCraftサーバー管理ツールをご存じでしょうか？
WebUI上からサーバーの新規作成、起動、停止、定期バックアップなどいろいろできる便利ツールです。
昔から利用しているのですが毎回構築するたびにやり方を忘れるのでメモ程度に手順を残しておきます。

以下公式サイト
https://wiki.codeemo.com/

# 環境
今回はESXi8.0上のVMに構築
|項目|詳細|
|--|--|
|OS|Ubuntu22.04LTS|
|CPU|6vCPU|
|RAM|30GB|
|Storage|SAS HDD 100GB|

# 構築
基本的には公式ドキュメント通りに進める
## UbuntuOSのインストール
割愛

## curlのインストール
```shell-session
user@localhost:~$ sudo apt -y install curl
[sudo] password for user: 
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
curl is already the newest version (7.81.0-1ubuntu1.6).
curl set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 53 not upgraded.
```
入ってた・・・

## Node.jsのインストール
NodeJSのサイトを参照しつつ
https://github.com/nodesource/distributions
```shell-session
user@localhost:~$ curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash

## Installing the NodeSource Node.js 14.x repo...


## Populating apt-get cache...

+ apt-get update
Hit:1 http://jp.archive.ubuntu.com/ubuntu jammy InRelease
Hit:2 http://jp.archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:3 http://jp.archive.ubuntu.com/ubuntu jammy-backports InRelease
Hit:4 http://jp.archive.ubuntu.com/ubuntu jammy-security InRelease
Reading package lists... Done

## Confirming "jammy" is supported...

+ curl -sLf -o /dev/null 'https://deb.nodesource.com/node_14.x/dists/jammy/Release'

## Adding the NodeSource signing key to your keyring...

+ curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | gpg --dearmor | tee /usr/share/keyrings/nodesource.gpg >/dev/null

## Creating apt sources list file for the NodeSource Node.js 14.x repo...

+ echo 'deb [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_14.x jammy main' > /etc/apt/sources.list.d/nodesource.list
+ echo 'deb-src [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_14.x jammy main' >> /etc/apt/sources.list.d/nodesource.list

## Running `apt-get update` for you...

+ apt-get update
Hit:1 http://jp.archive.ubuntu.com/ubuntu jammy InRelease
Get:2 https://deb.nodesource.com/node_14.x jammy InRelease [4,563 B]
Hit:3 http://jp.archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:4 http://jp.archive.ubuntu.com/ubuntu jammy-backports InRelease
Hit:5 http://jp.archive.ubuntu.com/ubuntu jammy-security InRelease
Get:6 https://deb.nodesource.com/node_14.x jammy/main amd64 Packages [773 B]
Fetched 5,336 B in 2s (2,666 B/s) 
Reading package lists... Done

## Run `sudo apt-get install -y nodejs` to install Node.js 14.x and npm
## You may also need development tools to build native addons:
     sudo apt-get install gcc g++ make
## To install the Yarn package manager, run:
     curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null
     echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
     sudo apt-get update && sudo apt-get install yarn
```
```shell-session
user@localhost:~$ sudo apt -y install nodejs
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  nodejs
0 upgraded, 1 newly installed, 0 to remove and 53 not upgraded.
Need to get 25.7 MB of archives.
After this operation, 125 MB of additional disk space will be used.
Get:1 https://deb.nodesource.com/node_14.x jammy/main amd64 nodejs amd64 14.21.1-deb-1nodesource1 [25.7 MB]
Fetched 25.7 MB in 1s (20.0 MB/s) 
Selecting previously unselected package nodejs.
(Reading database ... 73556 files and directories currently installed.)
Preparing to unpack .../nodejs_14.21.1-deb-1nodesource1_amd64.deb ...
Unpacking nodejs (14.21.1-deb-1nodesource1) ...
Setting up nodejs (14.21.1-deb-1nodesource1) ...
Processing triggers for man-db (2.10.2-1) ...
Scanning processes...                                                                                                                          
Scanning linux images...                                                                                                                       

Running kernel seems to be up-to-date.

No services need to be restarted.

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.
```


## Javaのインストール
ここは構築したいサーバーのバージョンに対応したJavaのインストールが必要
今回はMinecraft1.19のサーバーを構築したいのでJava17をインストール
バージョンの対応は下記サイトがまとめてくれています
https://mcpoteton.com/mcserver/official-server#index_id5

### インストール可能なバージョンの確認
```shell-session
user@localhost:~$ sudo apt search "^openjdk.*jdk$"
Sorting... Done
Full Text Search... Done
openjdk-11-jdk/jammy-updates,jammy-security 11.0.17+8-1ubuntu2~22.04 amd64
  OpenJDK Development Kit (JDK)

openjdk-17-jdk/jammy-updates,jammy-security 17.0.5+8-2ubuntu1~22.04 amd64
  OpenJDK Development Kit (JDK)

openjdk-18-jdk/jammy-updates,jammy-security 18.0.2+9-2~22.04 amd64
  OpenJDK Development Kit (JDK)

openjdk-19-jdk/jammy-updates,jammy-security 19.0.1+10-1ubuntu1~22.04 amd64
  OpenJDK Development Kit (JDK)

openjdk-8-jdk/jammy-updates,jammy-security 8u352-ga-1~22.04 amd64
  OpenJDK Development Kit (JDK)
```

```shell-session
user@localhost:~$ sudo apt install -y openjdk-17-jdk
[sudo] password for user: 
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done

// 中略

Setting up libgtk2.0-0:amd64 (2.24.33-2ubuntu2) ...
Setting up humanity-icon-theme (0.6.16) ...
Setting up libgail18:amd64 (2.24.33-2ubuntu2) ...
Setting up libgtk2.0-bin (2.24.33-2ubuntu2) ...
Setting up libgail-common:amd64 (2.24.33-2ubuntu2) ...
Setting up openjdk-17-jre:amd64 (17.0.5+8-2ubuntu1~22.04) ...
Setting up ubuntu-mono (20.10-0ubuntu2) ...
Setting up openjdk-17-jdk:amd64 (17.0.5+8-2ubuntu1~22.04) ...
update-alternatives: using /usr/lib/jvm/java-17-openjdk-amd64/bin/jconsole to provide /usr/bin/jconsole (jconsole) in auto mode
Processing triggers for ca-certificates (20211016ubuntu0.22.04.1) ...
Updating certificates in /etc/ssl/certs...
0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...

done.
done.
Processing triggers for libglib2.0-0:amd64 (2.72.1-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.1) ...
Processing triggers for man-db (2.10.2-1) ...
Setting up at-spi2-core (2.44.0-3) ...
Processing triggers for libgdk-pixbuf-2.0-0:amd64 (2.42.8+dfsg-1ubuntu0.2) ...
Scanning processes...                                                                                                                          
Scanning linux images...                                                                                                                       

Running kernel seems to be up-to-date.

No services need to be restarted.

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.
```

## 依存関係のインストール
```shell-session
# sudo apt -y install git rdiff-backup screen
# sudo apt -y install build-essential
```

## WebUIファイルをインストール

```shell-session
# cd /usr/games
# git clone https://github.com/hexparrot/mineos-node minecraft
# cd minecraft
# git config core.filemode false
# chmod +x generate-sslcert.sh mineos_console.js webui.js
# cp mineos.conf /etc/mineos.conf
```

```shell-session
# cd /usr/games/minecraft
# npm install
[sudo] password for user: 

> diskusage@1.1.3 install /usr/games/minecraft/node_modules/diskusage
> node-gyp rebuild

gyp WARN EACCES current user ("user") does not have permission to access the dev dir "/root/.cache/node-gyp/14.21.1"
gyp WARN EACCES attempting to reinstall using temporary dev dir "/usr/games/minecraft/node_modules/diskusage/.node-gyp"
gyp WARN install got an error, rolling back install
gyp WARN install got an error, rolling back install
gyp ERR! configure error 
gyp ERR! stack Error: EACCES: permission denied, mkdir '/usr/games/minecraft/node_modules/diskusage/.node-gyp'
gyp ERR! System Linux 5.15.0-56-generic
gyp ERR! command "/usr/bin/node" "/usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /usr/games/minecraft/node_modules/diskusage
gyp ERR! node -v v14.21.1
gyp ERR! node-gyp -v v5.1.0
gyp ERR! not ok 
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! diskusage@1.1.3 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the diskusage@1.1.3 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2022-12-24T17_08_21_286Z-debug.log
```
怒られた
nodejsは何も分からないので調べたサイトと結果だけ書きます。

参考：https://qiita.com/zaburo/items/12f99aa07ab934d07d75#%E3%81%82%E3%82%8B%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB%E3%81%A7%E3%82%A8%E3%83%A9%E3%83%BC

rootユーザーで`--unsafe-perm`を付けたらとりあえず実行できました。

```shell-session
root@localhost:/usr/games/minecraft# npm install --unsafe-perm

> diskusage@1.1.3 install /usr/games/minecraft/node_modules/diskusage
> node-gyp rebuild

make: Entering directory '/usr/games/minecraft/node_modules/diskusage/build'
  CXX(target) Release/obj.target/diskusage/src/main.o
In file included from ../src/main.cpp:3:
../../nan/nan.h: In function ‘void Nan::AsyncQueueWorker(Nan::AsyncWorker*)’:
../../nan/nan.h:2298:7: warning: cast between incompatible function types from ‘void (*)(uv_work_t*)’ {aka ‘void (*)(uv_work_s*)’} to ‘uv_after_work_cb’ {aka ‘void (*)(uv_work_s*, int)’} [-Wcast-function-type]
 2298 |     , reinterpret_cast<uv_after_work_cb>(AsyncExecuteComplete)
      |       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from ../../nan/nan.h:54,
                 from ../src/main.cpp:3:
../src/main.cpp: At global scope:
/root/.cache/node-gyp/14.21.1/include/node/node.h:793:7: warning: cast between incompatible function types from ‘void (*)(v8::Local<v8::Object>)’ to ‘node::addon_register_func’ {aka ‘void (*)(v8::Local<v8::Object>, v8::Local<v8::Value>, void*)’} [-Wcast-function-type]
  793 |       (node::addon_register_func) (regfunc),                          \
      |       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/root/.cache/node-gyp/14.21.1/include/node/node.h:827:3: note: in expansion of macro ‘NODE_MODULE_X’
  827 |   NODE_MODULE_X(modname, regfunc, NULL, 0)  // NOLINT (readability/null_usage)
      |   ^~~~~~~~~~~~~
../src/main.cpp:42:1: note: in expansion of macro ‘NODE_MODULE’
   42 | NODE_MODULE(diskusage, Init)
      | ^~~~~~~~~~~
  CXX(target) Release/obj.target/diskusage/src/diskusage_posix.o
  SOLINK_MODULE(target) Release/obj.target/diskusage.node
  COPY Release/diskusage.node
make: Leaving directory '/usr/games/minecraft/node_modules/diskusage/build'
added 1 package from 1 contributor and audited 582 packages in 11.16s

2 packages are looking for funding
  run `npm fund` for details

found 70 vulnerabilities (9 low, 18 moderate, 35 high, 8 critical)
  run `npm audit fix` to fix them, or `npm audit` for details
root@localhost:/usr/games/minecraft# 
```

## 証明書生成
そのまま
```shell-session
# cd /usr/games/minecraft
# ./generate-sslcert.sh
```

## コンフィグ修正
言語設定を変更
```shell-session
root@localhost:/usr/games/minecraft# vi /etc/mineos.conf
```
`#webui_locale = 'en_US'`をコメントアウトして下に`webui_locale = 'ja_JP'`を追加。
```
#webui_locale = 'en_US'
webui_locale = 'ja_JP'
```



## サービスの自動起動設定
`systemd`か`supervisord`の好きな方を選んでねと書いてありますが特にこだわりはないのでそのまま使える`systemd`を使います。

```shell-session
root@localhost:/usr/games/minecraft# cp /usr/games/minecraft/init/systemd_conf /etc/systemd/system/mineos.service
root@localhost:/usr/games/minecraft# systemctl enable mineos
root@localhost:/usr/games/minecraft# systemctl start mineos
root@localhost:/usr/games/minecraft# systemctl status mineos
｡・mineos.service - Start MineOS minecraft front-end
     Loaded: loaded (/etc/systemd/system/mineos.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-12-24 17:59:42 UTC; 3s ago
    Process: 2867 ExecStart=/usr/bin/node service.js start (code=exited, status=0/SUCCESS)
   Main PID: 2874 (mineos)
      Tasks: 11 (limit: 35973)
     Memory: 34.7M
        CPU: 1.115s
     CGroup: /system.slice/mineos.service
             ｨｦｨ｡2874 mineos "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >

Dec 24 17:59:41 localhost systemd[1]: Starting Start MineOS minecraft front-end...
Dec 24 17:59:41 localhost node[2867]: Starting mineos daemon...
Dec 24 17:59:42 localhost node[2867]: mineos daemon started. PID: 2874
Dec 24 17:59:42 localhost systemd[1]: Started Start MineOS minecraft front-end.
root@localhost:/usr/games/minecraft# 
```

動いた

# ログイン
`https://サーバーIP:8443`にアクセス
Ubuntuのユーザーでログイン
ログインはできた
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/242165/246a5912-4f51-ecde-9f9d-15ef9be1313d.png)
が、いろいろ動いていない気がする。

ざっと確認したところ、
- 稼働率グラフ
- プロファイルダウンロードページ
- 表示言語変更
- 新規サーバー作成

辺りが動いていない模様

## トラブルシューティング

トラブルシューティングページを参考に
https://wiki.codeemo.com/maint/webui.html

上記ページを参考に下記を実行
```shell-session
# cd /usr/games/minecraft
# git fetch
# git reset --hard origin/master
# git checkout master
# rm -rf ./node_modules
# npm install --unsafe-perm
```
sudoではなくルートユーザーで`npm install --unsafe-perm`を実行したら成功しました。

## サービスの再起動

```shell-session
root@localhost:/usr/games/minecraft# systemctl start mineos
root@localhost:/usr/games/minecraft# systemctl status mineos
● mineos.service - Start MineOS minecraft front-end
     Loaded: loaded (/etc/systemd/system/mineos.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-12-24 20:45:51 UTC; 1s ago
    Process: 2314 ExecStart=/usr/bin/node service.js start (code=exited, status=0/SUCCESS)
   Main PID: 2321 (mineos)
      Tasks: 11 (limit: 35973)
     Memory: 26.5M
        CPU: 695ms
     CGroup: /system.slice/mineos.service
             └─2321 mineos "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >

Dec 24 20:45:50 localhost systemd[1]: Starting Start MineOS minecraft front-end...
Dec 24 20:45:50 localhost node[2314]: Starting mineos daemon...
Dec 24 20:45:51 localhost node[2314]: mineos daemon started. PID: 2321
Dec 24 20:45:51 localhost systemd[1]: Started Start MineOS minecraft front-end.
```

# 再ログイン
`https://サーバーIP:8443`にアクセス
Ubuntuのユーザーでログイン

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/242165/469b4a64-82bc-14da-ba9f-941d68f54281.png)
おっいいのでは？？？？
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/242165/e005e229-78d9-8b99-e9d1-a9fdb2efd980.png)
プロファイルも取得できている
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/242165/6e1fa3f8-e83b-ef14-7063-527e95f85c23.png)
サーバー作成も問題なし
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/242165/fb7253fe-fe1b-ff29-a96c-640a13ad4b49.png)
起動確認も完了

# まとめ
現行OSのUbuntu22.04での構築記事がどこにもなかったためかなり手探りな状態でしたがなんとか構築できたのでよかったです。ただNodeJSで脆弱性がなんとかってメッセージが出ていたりとガバガバな部分もあるので細かい部分の設定もこれから修正が必要そうな感じです。途中npmなんもわからんって言ってたのでもっと勉強しようと思いました。

最後に、ドキュメントはしっかり読もう。