amaga38のブログ

twitter: @amaga38

factorio headlessサーバー 構築 覚え書き

factorio (https://factorio.com/) のマルチプレイ用サーバーを構築し直したときの覚え書きです。

サーバー準備@AWS EC2

インスタンス

メモリ1Gだと若干足りないようなので、2Gのt3a.smallで構築。t2.smallよりt3.smallt3a.smallの方が料金安いのね。

イメージはとりあえずUbuntu 20.04。apt update; apt upgrade; 忘れずに

セキュリティグループ設定

以下のインバウンドルールを追加したセキュリティグループで設定

IPバージョン タイプ プロトコル ポート範囲 メモ
IPv4 SSH TCP 22 サーバーメンテナンス用
IPv4 カスタムUDP UDP 34297 factorioサービスの待ち受けポート

factorio headlessのインストール

factorioの公式サイト (https://factorio.com/) のダウンロードページからLinuxのheadlessの圧縮ファイルをDL (下図1番左のリンク)

f:id:amaga38:20220218231609p:plain

バイナリのURLは https://factorio.com/get-download/1.1.53/headless/linux64 で固定みたいなので、EC2インスタンスWget

# mkdir /opt/factorio-k2
# cd /opt/factorio-k2
# wget -O factorio-1.1.53.tar.xz https://factorio.com/get-download/stable/headless/linux64

ファイルを展開

# tar -Jxvf factorio-1.1.53.tar.xz

サーバーのセッティングを用意。Diffだけ記録

$ diff /opt/factorio-k2/factorio/data/server-settings.example.json server-settings.json
2c2
<   "name": "Name of the game as it will appear in the game listing",
---
>   "name": "<削除>",
13,14c13,14
<     "public": true,
<     "lan": true
---
>     "public": false,
>     "lan": false
24c24
<   "game_password": "",
---
>   "game_password": "<削除>",
62c62
<   "autosave_only_on_server": true,
---
>   "autosave_only_on_server": false,
65c65
<   "non_blocking_saving": false,
---
>   "non_blocking_saving": true,

手元のWindowsからセーブファイルをEC2インスタンスにアップロード。/opt/factorio-k2/savesに格納。 (%AppData%\Factorio\saves にある該当のファイルをTeraTermのscpでアップロード)

EC2インスタンス起動時に自動起動するようにサービス登録

# /
# cat factorio-k2.service
[Unit]
Description=Factorio Service. https://factorio.com/
After=network-online.target

[Service]
ExecStart=/opt/factorio-k2/factorio/bin/x64/factorio --server-settings /opt/factorio-k2/server-settings.json --start-server /opt/factorio-k2/saves/save.zip
Restart=no
Type=simple

[Install]
WantedBy=multi-user.target

# cp factorio-k2.service /lib/systemd/system/
# ln -s /lib/systemd/system/factorio-k2.service /etc/systemd/system/multi-user.target.wants/factorio-k2.service
# systemctl enable factorio-k2

mod Krastrio-2 のインストール

Mod Portal(https://mods.factorio.com/) にアクセスして、Krastrio-2のModを入手。 Dependencies で青色に記載されているModに依存しているので、こちらも一緒に入手。今回入れたModは全部で3つ

DLしたZipファイルをまたscpでEC2インスタンスにアップロード。/opt/factorio-k2/factorio/mods/ディレクトリにZipファイルを格納

$ ls /opt/factorio-k2/factorio/mods/
Krastorio2Assets_1.1.0.zip Krastorio2_1.2.23.zip  flib_0.9.2.zip

今回はK2を1.1.5からアップデートしたけど、1.2.0からAssetを別に分けたみたいで、依存modとしてmodディレクトリに配置し忘れてた。そのせいでmodの読み込みが最初うまくいかなくて躓いた。エラーも特にでないんので、Modの依存はちゃんと確認。

動作確認

Windowsのクライアントから「マルチプレイ」→「アドレスに接続」でEC2インスタンスIPアドレスを入力、server-settings.jsongame_passwordに設定したパスワードを入力して接続できればOK。

Discord Bot

マルチプレイの仲間で好きな時にEC2インスタンスを起動、停止できるようにボットも作ってみた。こっちはAmazon Lightsailでほそぼそ運用テスト中

github.com