CentOS7 Terrariaのマルチプレイ用サーバをVPS上に構築

VPS上にTerrariaのマルチサーバを構築

SteamのサマーセールでTerrariaを購入しました!!

一人で遊ぶのも楽しいのですが、みんなでワイワイと遊びたくなったので、VPS上のCentOS7.3環境にマルチプレイ用のサーバを構築しましたので、今回はその構築手順をまとめてみました。

VPSについて

VPSのスペックはいっしょに遊ぶ人数やワールドの広さによって変わってくるのですが、3~4人でワールドの広さがMediumぐらいであれば、メモリが1Gほどあれば良いのではないかと思います。

今回は「ConoHaのVPS」と「さくらのVPS」両方のVPSにTerrariaサーバの環境を構築してみたのですが、どちらのVPSにもお試し期間があるので、まずは実際に試してみるのが良いでしょう。

かんたんなVPS比較

今回は使用したVPSについて、簡単にですが比較してみました。

ConoHaのVPS
ConoHaのVPSは初期費用が無料というのがおすすめな点です!

さくらのVPS
さくらのVPSはやはりサーバが安定しているところが良いですね!(初期費用が掛かってしまいますが…)

事前準備

今回の手順では下記のアプリケーションが必要になるので、インストールされていない場合は、あらかじめインストールしておいてください。

wgetインストール

Terrariaサーバをダウンロードする際に使用します。

# yum install wget

screen インストール

Terrariaサーバをscreenの仮想端末上で動作させるのに使用します。

# yum install screen

firewalld設定(ポートの開放)

Terrariaサーバで使用するポートは初期設定では7777番となっています。

まずは、こちらのポートを使用できるように「firewalld」の設定を行います。(使用するポートを7777番から変更したい場合は番号を適宜読み替えて下さい)

# firewall-cmd --permanent --add-port=7777/tcp
success
# firewall-cmd --reload
success

ポート番号について

Terrariaサーバを運用していると、色々なところから接続されていることがサーバのログからわかります。

もし、知り合いとだけ遊びたいのであれば、ポート番号を初期値の「7777」から変更して運用するのも良いと思います。

ポート番号を変更する場合は「0 - 1023」番ポート以外のポートを使用するようにしてください。あと、変更をしたポート番号を一緒に遊ぶ人に伝えるのも忘れないようにしてくださいね。

Terrariaサーバ動作用ユーザの作成

Terrariaサーバを動作させるためのユーザを作成します。

今回は「terraria」というユーザを作成していきますが、特に決まりが無いのでお好きなユーザを作成してくだださい。

# useradd terraria
# passwd terraria
Changing password for user terraria
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

Terrariaサーバのダウンロード

ユーザの作成が完了しましたら、次にTerrariaサーバのダウンロードを行っていきます。

Terarriaサーバ用ディレクトリの作成

まず最初にTerrariaサーバを動作させるためのディレクトリを作成して、そこにマルチプレイ用の環境を構築していきます。

今回は「/opt/terraria」というディレクトリを作成して、そこにTerarriaサーバの環境を構築して行きます。

# cd /opt/
# mkdir terraria
# mkdir terraria/{bin,downloads}
# chown -R terraria:terraria terraria

「downloads」ディレクトリにTerrariaサーバのデータをダウンロードして、「bin」ディレクトリに解凍したデータを格納していきます。

ディレクトリの所有者を、最初に作成したTerraria動作用のユーザに設定するのを忘れないようにしてください。

ダウンロード

ディレクトリを作成しましたら、下記URLにTerrariaサーバのダウンロード用のリンクがあるので、そこから最新のものを選んでURLをコピーしてダウンロードを行ってください。

http://terraria.gamepedia.com/Server#Downloads

※2017-06-27現在「terraria-server-1353.zip」が最新のデータだったので、こちらをダウンロードしていきます。

# su - terraria
$ cd /opt/terraria/downloads
$ wget http://terraria.org/server/terraria-server-1353.zip

解凍

ダウンロードしてきたデータはzipで圧縮されているので「unzip」コマンドで解凍します。

解凍する際に「-d」オプションを使用して、解凍先のディレクトリを先ほど作成した「/opt/terraria/bin」ディレクトリに指定してします。

$ unzip terraria-server-1353.zip -d /opt/terraria/bin

解凍すると「/opt/terraria/bin 」ディレクトリに、ダウンロードしてきたデータ「server-xxx.zip」の「xxx」と同じ名前のディレクトリが作成されます。

この手順の場合は「1353」というディレクトリになります。

$ cd /opt/terraria/bin/
$ ls -l
total 4
drwxrwxr-x 5 terraria terraria 4096 Apr 25 23:50 1353

解凍したフォルダ内の「Linux」フォルダの中にLinux用のTerarriaサーバのデータがあります。

$ cd 1353
$ ls -l 
total 12
drwxrwxr-x 4 terraria terraria 4096 Apr 25 23:50 Linux
drwxrwxr-x 3 terraria terraria 4096 Apr 25 23:50 Mac
drwxrwxr-x 2 terraria terraria 4096 Apr 25 23:50 Windows

設定ファイルの作成

ダウンロードしてきたデータの解凍が終わりましたら、次に設定ファイルを作成していきます。(設定ファイルを使用しなくても動作させることはできるのですが、今回の手順では設定ファイルを使用します)

サンプルの設定ファイル「serverconfig.txt」が「Windows」フォルダの中にあるので、それをコピーして設定ファイルを作成していきます。

$ cp -p Windows/serverconfig.txt /opt/terraria/
$ cd /opt/terraria/
$ vi serverconfig.txt

設定項目の説明

設定ファイルの各項目について簡単に説明しておきます。

この中から設定が必要な項目の「#」を外し有効化して、設定を行っていくという流れになります。

項目 内容
world サーバが起動する際に読み込むワールドデータファイル
autocreate ワールドデータが見つからない場合に自動的に生成し、その際に生成するワールドのサイズを設定
1(small), 2(medium), and 3(large)
seed ワールドを作成する際に使用するシード値
worldname ワールドを自動生成する場合のワールドネーム
difficulty ワールドを自動生成したさいの難易度
0(normal), 1(expert)
maxplayers 最大同時接続プレイヤー数(1 - 255 の範囲で指定)
port ポート番号
password サーバに設定するログインパスワード
motd サーバログイン時に表示するメッセージ
worldpath ワールドデータの保存ディレクトリ
デフォルトの保存ディレクトリは「/home/ユーザ名/.local/share/Terraria/Worlds」となる
banlist BANリストのファイル(通常はTerrariaサーバの実行ファイルがあるディレクトリにある)
secure=1 この項目を有効化することでチート防止機能の有効となる
language サーバの言語設定
English = en-US, German = de-DE, Italian = it-IT, French = fr-FR, Spanish = es-ES, Russian = ru-RU, Chinese = zh-Hans, Portuguese = pt-BR, Polish = pl-PL
upnp=1 この項目を有効化することでUPnPによるポート自動解放が有効化する
npcstream 数値をするくなくすることで敵のスキッピングを減らすが使用するネットワーク帯域が増える(0はoff)
priority システムの優先度設定
0(Realtime), 1(High), 2(AboveNormal), 3(Normal), 4(BelowNormal), 5(Idle)

編集内容(初回起動用)

初回起動時にワールドを自動生成させたいので「world」「autocreate」「worldname」「difficulty」のワールド自動生成に関する項目については必ず設定をしておきます。

「autocreate」で生成するワールドのサイズを指定しますが、はじめは「1」(small)か「2」(medium)にしておいたほうが良いでしょう。「3」(large)で生成されるワールドのサイズはとても大きく色々な資源をあつめるのも大変ですし、ワールドのデータサイズが大きいため、サーバの開始と停止処理にとても時間が掛かってしまいます。

まずは、しばらく運用してみてワールドのサイズが物足りなくなったときに、改めてlargeサイズのワールドを作成することをお勧めします。

その他の項目については、お好みにあわせて設定を行ってください。

変更前
#world=C:\Users\YOUR_USERNAME_HERE\My Documents\My Games\Terraria\Worlds\world1.wld
#autocreate=1
#worldname=Terraria
#difficulty=1
#maxplayers=8
#password=p@55w0rd
#worldpath=C:\Users\Defaults\My Documents\My Games\Terraria\Worlds\
変更後
world=/opt/terraria/Worlds/ワールド名.wld
autocreate=2
worldname=ワールド名
difficulty=0
maxplayers=4
password=パスワードを設定
worldpath=/opt/terraria/Worlds

※「worldname」には好きなワールド名を設定してください。

※Terrariaサーバへのログインにパスワードを設定したかったので「password」を設定していますが、必要が無い場合は「#」を外さないでそのままコメントにしておいてください。

Terrariaサーバ実行

設定ファイルが完成したら実際に起動させて動作試験を行っていきます。

実行権限の付与

CentOS7.3環境で動作させる場合は「TerrariaServer.bin.x86_64」を使用するのですが、初期設定では実行権限がないので「chmod」コマンドで実行権限を設定する必要があります。

$ cd bin/1353/Linux
$ chmod 744 TerrariaServer.bin.x86_64

TerrariaServer起動

まずは動作確認を行うので、直接「TerrariaServer.bin.x86_64」を実行します。

$ ./TerrariaServer.bin.x86_64 -config /opt/terraria/serverconfig.txt

初回実行時はワールドが生成されるためちょっと時間がかかりますので、一息つきながらお待ちください。

ワールドの生成が完了すると下記のような画面が表示され、コマンド入力待ち受け状態になります。

Terraria Server v1.3.5.3

Listening on port 7777
Type 'help' for a list of commands.

:

動作試験

Terrariaサーバが動作を開始しましたら、次はTerrariaのクライアントから接続して実際に遊べるか確認してください。

クライアントからの接続方法

Terrariaのクライアントからサーバに接続する方法は下記のようになります。

  1. 「Multiplayer」を選択
  2. 「Join via IP」を選択
  3. 「Select Player」でログインさせるキャラクターをダブルクリックで選択
  4. 「Enter Server IP Address:」でTerrariaサーバが動作しているサーバのIPアドレスを入力し「Accept」をクリック
  5. 「Enter Server Port」でサーバのポート番号を入力して「Accept」をクリック
  6. 「Server Requires Password:」と表示されるのでパスワードを入力(設定ファイルでpassword=を設定している場合のみ)
サーバの終了

クライアントでの動作確認が完了しましたら、サーバ上で「exit」と入力してTerrariaサーバを終了します。

: exit

ワールドデータの確認

Terrariaサーバを起動すると自動的にワールドが生成されるので、そのデータファイルが設定ファイルの「worldpath」で指定したディレクトリに「worldname」の項目で設定したワールド名と同じファイル「ワールド名.wld」が作成されているを確認してください。

設定ファイル再編集(ワールドデータ生成後)

Terrariaサーバを一度起動するとワールドが生成されるので、ワールドの自動生成設定を無効化しておきます。

$ cd /opt/terraria
$ cp -p serverconfig.txt serverconfig.txt_yyyymmdd
$ vi serverconfig.txt

編集内容

編集する項目はワールドの自動生成に関する「autocreate」「worldname」「difficulty」の設定を無効化しておきます。

※編集が必要な項目を抜粋

編集前
autocreate=2
worldname=ワールド名
difficulty=1
編集後
# autocreate=2
# worldname=Terraria
# difficulty=1

動作検証

設定ファイルの編集が終わりましたらTerrariaサーバを起動して、ワールドデータが正常に読み込まれるかの確認をしてください。

Terrariaのクライアントから接続して、先ほど動作確認を行ったワールドと同じワールドであることが確認できれば問題ないでしょう。

動作確認が終わりましたら、Terrariaサーバを終了させておいてください。

: exit

自動起動設定

Terrariaサーバ起動用の設定ファイルに問題ないことが確認できましたら、サーバ再起動時に自動的にTerrariaサーバが起動するように設定を行っていきます。

サーバを再起動するたびに毎回手動で「TerrariaServer.bin.x86_64」を実行するのはとても面倒ですからね。

スクリプト作成

Terrariaサーバの起動と停止を行うためのスクリプトを作成していきます。(おまけでワールドデータのセーブとサーバのステータス確認の機能もつけてみました)

$ vi terra_script.sh
変数説明

スクリプト内で使われている変数について説明します。

変数 設定内容
USERNAME terrariaサーバ動作用ユーザ
SCNAME screenの仮想端末名
TERA_VER ダウンロードしたterrariaサーバをデータを解凍した際のディレクトリ名
BIN_PATH terrariaサーバの実行ファイルがあるディレクトリ
SERVICE terrariaサーバの実行ファイル名
CONFIG 設定ファイル
#!/bin/bash

USERNAME='terraria'
SCNAME='terraria'
TERA_VER='1353'
BIN_PATH="/opt/terraria/bin/$TERA_VER/Linux/"
SERVICE='TerrariaServer.bin.x86_64'
CONFIG='/opt/terraria/serverconfig.txt'


ME=`whoami`

if [ $ME != $USERNAME ] ; then
   echo "Please run the $USERNAME user."
   exit
fi

start() {
   if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
       echo "$SERVICE is already running!"
       exit
   fi

   echo "Starting $SERVICE..."
   screen -AmdS $SCNAME $BIN_PATH$SERVICE -config $CONFIG
   exit
}

stop() {
   if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
       echo "Stopping $SERVICE "
   else
       echo "$SERVICE is not running!"
       exit
   fi

   screen -p 0 -S $SCNAME -X eval 'stuff "say SERVER SHUTTING DOWN IN 10 SECONDS. "\015'
   sleep 10
   screen -p 0 -S $SCNAME -X eval 'stuff "exit"\015'
   exit
}

save() {
   echo 'World data saving...'
   screen -p 0 -S $SCNAME -X eval 'stuff "say World saveing..."\015'
   screen -p 0 -S $SCNAME -X eval 'stuff "save"\015'
   exit
}

status() {
   if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
                echo "$SERVICE is already running!"
                exit
   else
       echo "$SERVICE is not running!"
       exit
        fi
}

case "$1" in
   start)
       start
       ;;
   stop)
       stop
       ;;
   save)
       save
       ;;
   status)
       status
       ;;
   *)
       echo  $"Usage: $0 {start|stop|status|save}"
esac

スクリプトの説明

このスクリプトはTerrariaサーバの「起動」「停止」「再起動」および「状態確認」と「ワールドデータの保存」を行うことが出来きます。

それぞれの実行方法は下記の通りとなっています。

  • サーバの起動 /opt/terraria/terra_script.sh start
  • サーバの停止 /opt/terraria/terra_script.sh stop
  • 状態確認 /opt/terraria/terra_script.sh status
  • ワールドデータ保存 /opt/terraria/terra_script.sh save

スクリプトの実行は、terraria実行用ユーザからのみ出来るようになっていますので注意してください。

パーミッション設定

スクリプトを作成したらパーミッションを変更して、スクリプトに実行権限を付けてあげてください。

$ chmod 744 terra_script.sh

スクリプト動作確認

スクリプトが完成しましたら、実際に実行してTerrariaサーバの起動と停止が出来るか動作確認を行ってください。

起動確認

「terra_script.sh」を実行してTerrariaサーバが正常に起動するかの確認を行っていきます。

$ sh -x ./terra_script.sh start
仮想端末状況確認

今回はTerariaサーバを「screen」の仮想端末上で動作させているため、正常に起動スクリプトが動作してれば、起動スクリプト内で設定した仮想端末が起動してるはずです。

「screen -ls」コマンドで仮想端末の状況を確認することができるので、仮想端末が起動していることを確認してください。

$ screen -ls
There is a screen on:
        883.terraria    (Detached)
1 Socket in /var/run/screen/S-terraria.
仮想端末に接続

screen上の仮想端末には「screen -r 仮想端末名」コマンドをで接続(Attache)できるので、仮想端末に接続してTerrariaサーバが起動しているか確認してください。

$ screen -r terraria

Terraria Server v1.3.5.3

Listening on port 17777
Type 'help' for a list of commands.

:

起動が確認できましたら、念のためにクライアントからも実際にログインして遊べるか試してみてください。

仮想端末から抜ける

Terrariaサーバの動作確認が完了しましたら、「Ctrl」+「a」を押下した後に「d」キーを押下することでscreen上の仮想端末から抜ける(Detache)ことができるので、いったん仮想端末から抜けておきます。

停止確認

起動の確認が出来ましたら、次に停止の動作確認を行っていきます。

$ sh -x ./terra_script.sh stop

正常に停止処理が完了しましたら、Terrariaサーバが終了すろtscreen上の仮想端末も終了しますので、「screen -ls」コマンドで仮想端末の状態を確認してください。

$ screen -ls
No Sockets found in /var/run/screen/S-terraria.

仮想端末が動作停止している場合は、「No Sockets found」と表示されて仮想端末が起動していないことが確認できます。

systemd用ファイル作成(自動起動設定用ファイル)

起動・停止用のスクリプトが完成しましたら、「systemd」に登録するための設定ファイルを作成して行きます。

# su -
# cd /etc/systemd/system
# vi terraria.service

設定ファイル内の「User」の部分にはTerrariaサーバを動作用のユーザを設定してください。

[Unit]
Description=Terraria Server
After=network.target local-fs.target

[Service]
Type=forking
User=terraria
ExecStart=/opt/terraria/terra_script.sh start
ExecStop=/opt/terraria/terra_script.sh stop

[Install]
WantedBy=multi-user.target

systemdへ登録

systemd登録用の設定ファイルの作成が終わりましたら、自動実行できるように「systemctl」コマンドを使用して「systemd」に登録していきます。

# systemctl enable terraria

自動動作確認

「systemd」への登録が完了しましたら、サーバ自体を再起動してTerrariaサーバが自動的に起動しているか確認を行ってください。

# reboot

サーバの再起動後に無事Terrariaサーバの起動が確認できましたら作業完了です。
おつかれさまでした!!

あとは、Terrariaを存分に楽しんでください。

おまけ

serverconfig.txtの初期内容

#this is an example config file for TerrariaServer.exe
#use the command 'TerrariaServer.exe -config serverconfig.txt' to use this configuration or run start-server.bat
#please report crashes by emailing crashlog.txt to support@terraria.org

#the following is a list of available command line parameters:

#-config                                       Specifies the configuration file to use.
#-port                                           Specifies the port to listen on.
#-players  / -maxplayers        Sets the max number of players
#-pass  / -password         Sets the server password
#-world                                           Load a world and automatically start the server.
#-autocreate <#>                                                Creates a world if none is found in the path specified by -world. World size is specified by: 1(small), 2(medium), and 3(large).
#-banlist                                                 Specifies the location of the banlist. Defaults to "banlist.txt" in the working directory.
#-worldname                                 Sets the name of the world when using -autocreate.
#-secure                                                                Adds addition cheat protection to the server.
#-noupnp                                                                Disables automatic port forwarding
#-steam                                                                 Enables Steam Support
#-lobby  or              Allows friends to join the server or sets it to private if Steam is enabled
#-ip        Sets the IP address for the server to listen on
#-forcepriority       Sets the process priority for this task. If this is used the "priority" setting below will be ignored.
#-disableannouncementbox   Disables the text announcements Announcement Box makes when pulsed from wire.
#-announcementboxrange    Sets the announcement box text messaging range in pixels, -1 for serverwide announcements.
#-seed  Specifies the world seed when using -autocreate

#remove the # in front of commands to enable them.

#Load a world and automatically start the server.
#world=C:\Users\YOUR_USERNAME_HERE\My Documents\My Games\Terraria\Worlds\world1.wld

#Creates a new world if none is found. World size is specified by: 1(small), 2(medium), and 3(large).
#autocreate=1

#Sets the world seed when using autocreate
#seed=AwesomeSeed

#Sets the name of the world when using autocreate
#worldname=Terraria

#Sets the difficulty of the world when using autocreate 0(normal), 1(expert)
#difficulty=1

#Sets the max number of players allowed on a server.  Value must be between 1 and 255
#maxplayers=8

#Set the port number
#port=7777

#Set the server password
#password=p@55w0rd

#Set the message of the day
#motd=Please dont cut the purple trees!

#Sets the folder where world files will be stored
#worldpath=C:\Users\Defaults\My Documents\My Games\Terraria\Worlds\

#The location of the banlist. Defaults to "banlist.txt" in the working directory.
#banlist=banlist.txt

#Adds addition cheat protection.
#secure=1

#Sets the server language from its language code.
#English = en-US, German = de-DE, Italian = it-IT, French = fr-FR, Spanish = es-ES, Russian = ru-RU, Chinese = zh-Hans, Portuguese = pt-BR, Polish = pl-PL,
#language=en-US

#Automatically forward ports with uPNP
#upnp=1

#Reduces enemy skipping but increases bandwidth usage. The lower the number the less skipping will happen, but more data is sent. 0 is off.
#npcstream=60

#Default system priority 0:Realtime, 1:High, 2:AboveNormal, 3:Normal, 4:BelowNormal, 5:Idle
priority=1

コメント

  1. まるすけ より:

    変な時間に申し訳ありません。
    こちらのサイトのおかげで無事マルチでプレイ他の人も入れる環境を構築することが出来ました。ありがとうございます。

    質問があるのですが、この上記の状態で難易度だけ変更をすることは可能でしょうか?
    なんとか出来たマップなので難易度だけの変更で済ませたいのですが、お時間ありましたらご教授賜りたいです。

    • tamohiko より:

      まるすけさん

      コメントありがとうございます。
      無事マルチ環境の構築ができたようでよかったですね。

      > 質問があるのですが、この上記の状態で難易度だけ変更をすることは可能でしょうか?

      バニラのTerrariaサーバでは、ワールドの難易度設定は作成時にしか設定できなかったはずです。
      modを使用してワールドデータを直接いじれば出来たような…
      ちょっと調べてみますね。

  2. たっくんさん より:

    Terrariaのアップデートが来た際、どうやってアップデートをすればよいかご教授いただけないでしょうか。
    ConoHaのVPSを利用しているのですが調べてもどうしてもわからず仕舞いになってしまっています。
    扱いの慣れていない私からの質問で恐縮ですが、どうぞよろしくお願いします。

    • tamohiko より:

      たっくんさん

      コメントありがとうございます。
      Terrariaのアップデート方法ですが、1.3から1.4といったように大きくバージョンが変わった場合は、別ディレクトリに新しくインストールするという方法をお勧めします。

      作成されるワールドの内容は、初回作成時におおよそ決まっているので、以前のバージョンのワールドには新しいワールドの要素が適用されない可能性があります。

      「1.4.0.4」から「1.4.0.5」といった小さなアップデートの場合は、実行する「TerrariaServer.bin.x86_64」を新しいものにすることでアップデートすることが出来ます。

      このページの手順でインストールされている場合は、下記のような手順でアップデートが出来ると思います。

      1.terrariaサーバを停止後インストールディレクトリ丸ごとバックアップ
      2.インストールディレクトリにあるdownloadディレクトリに新しいバージョンのterrariaサーバをダウンロード
      3.インストールディレクトリにあるbinディレクトリに解凍
      4.TerrariaServer.bin.x86_64に実行権限を付与
      5.terrariaを制御するスクリプト(terra_script.sh)のTERA_VER部分を解凍して出来たディレクトリ名に変更

      詳しい手順については、あとでまとめてみようと思いますが、取り急ぎ回答させていただきました。

      • たっくんさん より:

        返信ありがとうございます。
        参考にさせていただきます!
        またさらにまとめていただけるようで大変助かります。
        また公開された際にも参考にさせていただこうと思います。

タイトルとURLをコピーしました