端くれプログラマの備忘録 Minecraft [Minecraft] サーバーをセットアップする

[Minecraft] サーバーをセットアップする

DigitalOceanで開発用に立ち上げているDroplet(1CPU 512MB CentOS 7ベース)でMinecraftサーバーを稼動させてみたい。以下のサイトの説明が大変わかりやすいので、ほぼそのままなぞらせてもらう。

さくらのVPSでminecraftサーバを構築 minecraft_serverインストール編 | server-memo.net
http://www.server-memo.net/minecraft/mc-install/minecraftserver-3.html

ポートを開ける

$ sudo firewall-cmd --list-all
$ sudo firewall-cmd --permanent --add-port=25565/tcp
$ sudo firewall-cmd --reload

javaのインストール

$ sudo yum -y install java
$ java -version
openjdk version "1.8.0_91"
OpenJDK Runtime Environment (build 1.8.0_91-b14)
OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)

screenのインストール

$ sudo yum -y install screen

ユーザ作成

$ sudo adduser minecraft
$ sudo passwd minecraft
Changing password for user minecraft.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

minecraftサーバーのインストール

配布元: minecraft.net – Server Download
https://minecraft.net/ja/download/server

$ sudo mkdir /opt/minecraft
$ sudo chown minecraft:minecraft /opt/minecraft/
$ su - minecraft
$ cd /opt/minecraft
$ wget https://s3.amazonaws.com/Minecraft.Download/versions/1.10.2/minecraft_server.1.10.2.jar
$ cp -p minecraft_server.1.10.2.jar minecraft_server.jar
$ java -Xms1024M -Xmx1024M -jar minecraft_server.jar nogui
[16:55:59] [Server thread/INFO]: Starting minecraft server version 1.10.2
[16:55:59] [Server thread/INFO]: Loading properties
[16:55:59] [Server thread/WARN]: server.properties does not exist
[16:55:59] [Server thread/INFO]: Generating new properties file
[16:55:59] [Server thread/WARN]: Failed to load eula.txt
[16:55:59] [Server thread/INFO]: You need to agree to the EULA in order to run the server. Go to eula.txt for more info.
[16:55:59] [Server thread/INFO]: Stopping server
$ vi eula.txt
eula=true
$ java -Xms1024M -Xmx1024M -jar minecraft_server.jar nogui
[17:00:25] [Server thread/INFO]: Starting minecraft server version 1.10.2
[17:00:25] [Server thread/INFO]: Loading properties
[17:00:25] [Server thread/INFO]: Default game type: SURVIVAL
[17:00:25] [Server thread/INFO]: Generating keypair
[17:00:26] [Server thread/INFO]: Starting Minecraft server on *:25565
[17:00:26] [Server thread/INFO]: Using epoll channel type
[17:00:26] [Server thread/INFO]: Preparing level "world"
[17:00:27] [Server thread/INFO]: Preparing start region for level 0
[17:00:30] [Server thread/INFO]: Preparing spawn area: 0%
[17:00:31] [Server thread/INFO]: Preparing spawn area: 7%
[17:00:32] [Server thread/INFO]: Preparing spawn area: 20%
[17:00:33] [Server thread/INFO]: Preparing spawn area: 40%
[17:00:34] [Server thread/INFO]: Preparing spawn area: 63%
[17:00:34] [Server thread/INFO]: Done (8.078s)! For help, type "help" or "?"

起動用スクリプト作成

$ vi mc_start.sh
#!/bin/bash

USERNAME='minecraft'
SERVICE='minecraft_server.jar'
SCNAME='minecraft'
MC_PATH='/opt/minecraft'

XMX="1024M"
XMS="1024M"

cd $MC_PATH

ME=`whoami`

if [ $ME == $USERNAME ] ; then
  if pgrep -u $USERNAME -f $SERVICE > /dev/null
    then
      echo "$SERVICE is already running!"
    else
      echo "Starting $SERVICE..."
      screen -AmdS $SCNAME java -Xmx$XMX -Xms$XMS -jar $SERVICE nogui
  fi
else
  echo "Please run the minecraft user."
fi
$ chmod 744 mc_start.sh

停止用スクリプト作成

$ vi mc_stop.sh
#!/bin/bash

USERNAME='minecraft'
SERVICE='minecraft_server.jar'
SCNAME='minecraft'
MC_PATH='/opt/minecraft'

cd $MC_PATH

ME=`whoami`

if [ $ME == $USERNAME ] ; then
  if pgrep -u $USERNAME -f $SERVICE > /dev/null
    then
      echo "Stopping $SERVICE"
      screen -p 0 -S $SCNAME -X eval 'stuff "say SERVER SHUTTING DOWN IN 10 SECONDS. Saving map..."\015'
      screen -p 0 -S $SCNAME -X eval 'stuff "save-all"\015'
      sleep 10
      screen -p 0 -S $SCNAME -X eval 'stuff "stop"\015'
      sleep 10
      echo "Stopped minecraftserver"
    else
      echo "$SERVICE was not runnning."
  fi
else
  echo "Please run the minecraft user."
fi
$ chmod 744 mc_stop.sh

自動起動設定

$ sudo vi /etc/systemd/system/minecraft.service
[Unit]
Description=Minecraft Server
After=network.target local-fs.target

[Service]
Type=forking
User=minecraft
ExecStart=/opt/minecraft/mc_start.sh
ExecStop=/opt/minecraft/mc_stop.sh

[Install]
WantedBy=multi-user.target
$ sudo systemctl enable minecraft
Created symlink from /etc/systemd/system/multi-user.target.wants/minecraft.service to /etc/systemd/system/minecraft.service.