Linux Server with Amazon Linux AMI

Listed below are the steps required to launch a dedicated game server on an Amazon Linux 64bit HVM EC2 instance. They do not cover any basic or general information regarding linux server administration, but they should be pretty straight forward to follow.

Note that Amazon Linux is a RedHat-like distribution, so a similar set of steps can probably be used to get a server running under Fedora, RHEL, CentOS, etc. (though you will have to have appropriate yum repositories configured and some package names may differ).

The code snippets below should all be directly paste-able into a command prompt (hence the bash comment characters and the creation of scripts using echo). There are a couple of files that will need to be edited though. The AMI should come with vim and nano, but if you prefer something else you will have to install it.

Create EC2 instance and connect
How to use Amazon Web Services and EC2 is beyond the scope of this document, but a few notes are given below:


 * You will want an instance with more than 6GB of RAM and enough CPU to not get bogged down as the game runs
 * You might want to create a second storage volume in which to store SteamCMD and games (leaving the root volume as the standard 8GB)
 * At the time of this writing, a c3.xlarge instance gets your 4 vCPUs, 7.5GB of RAM, and built in Enhanced Networking for around $0.21/hour. An rc3.large is cheaper with more RAM but less CPU.
 * Make a new security group and add access for the port you want to run the game on PLUS the 2 ports after it (so if you run on 26900, your policy should allow UDP ports 26900-26902)

Bring server up to date
 sudo su -l yum update -y

Install prerequisites
 yum install -y glibc.i686 libstdc++48.i686
 * 1) Install SteamCMD pre-reqs

yum install -y mesa-libGLU libXcursor libXrandr
 * 1) Install 7 Days to Die pre-reqs

Create a location for SteamCMD and Games
 mkdir /opt/games

mkfs.ext4 /dev/xvdb echo "/dev/xvdb  /opt/games  ext4    defaults,noatime  1   1" >>/etc/fstab mount /opt/games
 * 1) Format and mount game storage drive, if using another volume for storage
 * 2) Your device name might be something other than /dev/xvdb, so edit the lines
 * 3) accordingly

Create a user to run games as
 adduser --home /opt/games steam cp /etc/skel/.* /opt/games chown -R steam:steam /opt/games
 * 1) Add a steam user and setup the home directory

Install SteamCMD
 su -l steam
 * 1) Switch to steam user

mkdir ~/steamcmd && cd ~/steamcmd
 * 1) Create SteamCMD directory

wget 'https://steamcdn-a.akamaihd.net/client/installer/steamcmd_linux.tar.gz'
 * 1) Download SteamCMD

tar -xzvf steamcmd_linux.tar.gz && rm -f steamcmd_linux.tar.gz
 * 1) Unpack SteamCMD

./steamcmd.sh
 * 1) Launch SteamCMD

login YOURSTEAMUSERNAME YOURSTEAMPASSWORD
 * 1) Downloads updates and all that. Once it is done, verify that logging in
 * 2) works (and enter SteamGuard code if required)

exit
 * 1) Exit interactive session

Create a script to install and update 7 Days
 mkdir /opt/games/bin
 * 1) Create a directory for scripts

echo '#!/bin/bash' > /opt/games/bin/steamgame_update.sh echo 'APP_ID=$1' >> /opt/games/bin/steamgame_update.sh echo 'APP_DIR=$2' >> /opt/games/bin/steamgame_update.sh echo 'STEAM_USER="YOURSTEAMUSERNAME"' >> /opt/games/bin/steamgame_update.sh echo 'STEAM_PASS="YOURSTEAMPASSWORD"' >> /opt/games/bin/steamgame_update.sh echo '/opt/games/steamcmd.sh +login ${STEAM_USER} ${STEAM_PASS} +force_install_dir ${APP_DIR} +app_update ${APP_ID} +exit' >> /opt/games/bin/steamgame_update.sh
 * 1) Create general app installing/updating script
 * 2) (re-usable for other games)
 * 3) NOTE: You will either have to edit the YOURSTEAM lines below, or edit
 * 4) the script afterwards to put the correct username and password in

echo '#!/bin/bash' > /opt/games/bin/update_7days.sh echo '/opt/games/bin/steamgame_update.sh 294420 /opt/games/7days' >> /opt/games/bin/update_7days.sh
 * 1) Create 7days installing/updating script

chmod 750 /opt/games/bin/*.sh
 * 1) Set script permissions

/opt/games/bin/update_7days.sh
 * 1) Run update script to download and install 7days
 * 2) (also used whenever you need to update the game)

Modify server config and launch scripts
Since our EC2 instance is 64 bit, we need to adjust the server launching script. That script should be located here: /opt/games/7days/startserver.sh

Uncomment the x86_64 start line and comment out the x86 start line, so that this:  ./7DaysToDieServer.x86 -logfile 7DaysToDieServer_Data/output_log__`date +%Y-%m-%d__%H-%M-%S`.txt -quit -batchmode -nographics -dedicated $PARAMS
 * 1) if [ "$(uname -m)" == "x86_64" ]; then
 * 2)        ./7DaysToDieServer.x86_64 -logfile 7DaysToDieServer_Data/output_log__`date +%Y-%m-%d__%H-%M-%S`.txt -quit -batchmode -nographics -dedicated $PARAMS
 * 3) else
 * fi

Becomes this:  ./7DaysToDieServer.x86_64 -logfile 7DaysToDieServer_Data/output_log__`date +%Y-%m-%d__%H-%M-%S`.txt -quit -batchmode -nographics -dedicated $PARAMS
 * 1) if [ "$(uname -m)" == "x86_64" ]; then
 * 1) else
 * 2)       ./7DaysToDieServer.x86 -logfile 7DaysToDieServer_Data/output_log__`date +%Y-%m-%d__%H-%M-%S`.txt -quit -batchmode -nographics -dedicated $PARAMS
 * fi

We will also want to set a few settings in the server config file. That file is located here: /opt/games/7days/serverconfig.xml

Things you might want to change right away:
 * ServerPort : If you want to run on something other than 26900-26902
 * ServerIsPublic : Defaults to true, should control whether you server gets listed or not
 * ServerName : Name of your server
 * ServerPassword : Password for your server, if you want it locked
 * ServerDescription : Description for your server
 * TelnetEnabled : Whether you want the console telnet accessible (recommended for use from localhost only!)
 * TelnetPassword : Password required when accessing telnet
 * SaveGameFolder : Initially commented out, you can use this option to hardcode where save files go. Make sure the director you specify exists

Initial test: can we start the game?
Launch the game and watch the log output  /opt/games/7days/startserver.sh -configfile=serverconfig.xml &
 * 1) Start game server

tail -f /opt/games/7days/7DaysToDieServer_Data/output_log*.txt
 * 1) Watch the log file (the grep command weeds out some known warnings and blank lines)

There will be some shader errors, downsize warnings, etc... but eventually you should start to see some stats periodically reported. That means the game thinks it is up! Try launching 7 Days on your computer and see if you can connect!

Creating services
Assuming everything is now working, you can turn 7 Days into a service so it is easily started and stopped.

7days service
If the game is still running, you will want to stop it.  ps | grep 7DaysToDieServe | cut --delimiter=' ' -f 1
 * 1) In the terminal you launched the game from, this should do the trick

Create the file /etc/init.d/7days and make it executable. This requires elevated privileges, so be sure to exit the out of the steam user shell first:  touch /etc/init.d/7days chmod 755 /etc/init.d/7days

Open the file for editing and paste in the following contents: 
 * 1) !/bin/bash
 * 2) chkconfig: - 90 13
 * 3) processname: 7days
 * 4) pidfile: /opt/games/7days/server.pid
 * 5) BEGIN INIT INFO
 * 6) Provides: 7days
 * 7) Required-Start: $local_fs $remote_fs $network Xvfb
 * 8) Required-Stop: $local_fs $remote_fs $network
 * 9) END INIT INFO
 * 1) Required-Stop: $local_fs $remote_fs $network
 * 2) END INIT INFO


 * 1) CONFIG ###
 * 2) This is the section to modify for different install
 * 3) directories, game user accounts, etc.
 * 1) This is the section to modify for different install
 * 2) directories, game user accounts, etc.
 * 1) directories, game user accounts, etc.

GAME_ROOT_DIR='/opt/games' GAME_NAME='7days' GAME_DIR_NAME=${GAME_NAME} GAME_USER='steam' GAME_BIN_NAME='7DaysToDieServer.x86_64'
 * 1) Parent directory for all games
 * 1) Name of the game; assumed to match init script name
 * 1) Directory name for this game
 * 1) User to run the game as
 * 1) Name of this game
 * 2) Binary used to start the game

game_dir="${GAME_ROOT_DIR}/${GAME_DIR_NAME}" game_options="-configfile=${game_dir}/serverconfig.xml -logfile 7DaysToDieServer_Data/output_log__`date +%Y-%m-%d__%H-%M-%S`.txt -quit -batchmode -nographics -dedicated" pidfile="${game_dir}/server.pid" game_bin="${game_dir}/${GAME_BIN_NAME}"
 * 1) Derived game dir
 * 1) Game command line options
 * 1) PID file location
 * 1) Game binary full path


 * 1) END CONFIG ###
 * 1) END CONFIG ###

RETVAL=0

. /etc/rc.d/init.d/functions
 * 1) Source function library.

start { echo -n $"Starting ${GAME_NAME}: "

su -l ${GAME_USER} -c "${game_bin} ${game_options} 1>/dev/null 2>&1 & echo \$! >${pidfile}" sleep 1 service ${GAME_NAME} status >/dev/null 2>&1 if [ "$?" = "0" ]       then success RETVAL=0 else failure RETVAL=1 fi       echo } stop { echo -n $"Stopping ${GAME_NAME}: " killproc -p ${pidfile} -d 10 ${game_bin} RETVAL=$? echo }

case "$1" in start)        start        ;;  stop) stop ;; status)        if ! test -f ${pidfile}; then            echo ${GAME_NAME} is stopped            RETVAL=3        else            status -p ${pidfile} ${game_bin}            RETVAL=$?        fi        ;;  *) echo $"Usage: ${GAME_NAME} {start|stop|status}" exit 1 esac
 * 1) See how we were called.

exit $RETVAL

We can now use the service command to start, stop, or check the status of 7days.

Starting 7Days on boot
We can have the game server start on boot by turning them on with the chkconfig command:  chkconfig 7days on