Difference between revisions of "Running a Dedicated Server"

From Voxel Turf Wiki
Jump to: navigation, search
(initial edit)
 
m
Line 1: Line 1:
This article explains how to deploy a Voxel Turf Dedicated Server on a VPS.
+
This article explains how to deploy a Voxel Turf Dedicated Server on a Linux VPS.
  
 
== Deploying a VPS ==
 
== Deploying a VPS ==
Line 142: Line 142:
  
 
===serverscript.lua===
 
===serverscript.lua===
 +
[[File:serverscript_lua.png|thumb|Placing a serverscript.lua file in a save game's directory will cause it to be executed by vtserver on startup]]
 
Placing a plaintext file serverscript.lua in the games save directory will cause the server to execute the script whenever that save is executed. You can use this to write custom mods for the server, give the players stuff when they log in or even redirect players to other servers (you can make stargates)!
 
Placing a plaintext file serverscript.lua in the games save directory will cause the server to execute the script whenever that save is executed. You can use this to write custom mods for the server, give the players stuff when they log in or even redirect players to other servers (you can make stargates)!
  
 
===Periodic Shutdown and Restart with serverscript.lua===
 
===Periodic Shutdown and Restart with serverscript.lua===
This can be useful to clear the map of dumped vehicles and fix any bugs that may have creeped into the server's state. Here is an example script from the Lets Build A City server:
+
This can be useful to clear the map of dumped vehicles and fix any bugs that may have creeped into the server's state. Here is an example script from the Lets Build A City server that shuts down the server every 6 hours. It also sets the Message Of The Day and shows some other hooks.
 +
 
 
<syntaxhighlight lang=lua>
 
<syntaxhighlight lang=lua>
 
SERVER_RESET_PERIOD = 60*6;
 
SERVER_RESET_PERIOD = 60*6;
Line 173: Line 175:
  
  
-- On player login
+
-- On new player login
 
customFunc.OnNewPlayer_extra = function (GMS, P, I, PCr, W)
 
customFunc.OnNewPlayer_extra = function (GMS, P, I, PCr, W)
-- Give an 80 stack of explosive crates
+
-- Give them items, send a message, etc
 
end
 
end
  

Revision as of 03:41, 13 February 2018

This article explains how to deploy a Voxel Turf Dedicated Server on a Linux VPS.

Deploying a VPS

You can grap a cheap VPS. A 1 core VPS will get you started but it is recommended that you have a 2 core VPS.

This article assumes that you have one. HOST can be substituted with the IP address of the server (eg 1.2.3.4).

SSH into the server

(enter this on your local linux termanal)

LOCAL MACHINE:
ssh root@HOST


Set up a user

useradd -m turf
passwd turf
adduser turf sudo
su - turf

Security (Optional)

Set up passwordless access via ssh (using keys and not passwords)

LOCAL MACHINE:
ssh-keygen
ssh-copy-id -i ~/.ssh/id_rsa turf@HOST

Set the ssh port to something other than the default (21). This will help stop people trying to brute force into your server. Also disable root login & password login. To do this, change:

sudo nano /etc/ssh/sshd_config

To:

Port <NEW PORT NUMBER>
PermitRootLogin no
ChallengeResponseAuthentication no
PasswordAuthentication no
UsePAM no

To login to your server you'll have to use:

ssh -p <NEW PORT NUMBER> turf@HOST

Tips

You can set the default shell to bash on the server. This enables tab completion and arrow keys.

sudo chsh -s /bin/bash USERNAME

Installing Voxel Turf

Installing Dependencies

sudo apt-get update 
sudo apt-get upgrade
sudo apt-get install libsdl2-2.0-0 
sudo apt-get install libsdl2-net-2.0-0


Installing Steam and SteamCMD

sudo apt-get install lib32gcc1
mkdir Steam
cd Steam
wget http://media.steampowered.com/client/steamcmd_linux.tar.gz
tar -xvzf steamcmd_linux.tar.gz

Alternatively you can just copy the files from a linux installation of Voxel Turf onto the server

Using SteamCMD to install Voxel Turf

./steamcmd.sh

In Steam>

login anonymous
force_install_dir /home/<USER>/turf
app_update 526340
quit

You should be now able to run the server and play.

Running The Server

Running vtserver --help will give you a list of command line switches.

Start Command

An easy way to generate a start command is to run the game locally (on your machine) and extract the start command from the logs (GAMEFOLDER/logs/startserver.log).

You should see something like this: --serverName "YOUR Turf Server" --saveGame "Sora_Turf" --publicGame OFF --dedicated OFF --difficulty 1 --maxPlayers 8 --hostPlayer 36563646 --newGame ON --gameMode "Turf" --genmap 4096 4096 --randomSeed "46756168" --steam

Change it to --serverName "NAME Turf Server" --saveGame "GAMESAVENAME" --publicGame ON --difficulty 1 --maxPlayers 8 --gameMode "Turf" --genmap 2048 2048

Permissions

Go to GAMEFOLDER/settings/globalpermissions.txt and make it look like:

%ADMINS
;UID

replace ;UID with the UID from steam (SteamID3) For example "[U:1:145678]" so I put ";145678" Can be found here: http://steamid.co/ or from "--HostPlayer" in startserver.log on the local machine.


Advanced Stuff

Running the Server Continuously

This allows you to auto-restart the server if it crashes. You can find this script in /linux_server_admin_scripts_and_instructions/runvtserver.sh.

#!/bin/bash
BASE=/home/USER/
DIR=$BASE/turf/
while :; do
	# Run the server
	$DIR/vtserver --saveGame SAVE_FILE --publicGame ON --maxPlayers 8 --serverName "SERVER_NAME"
	sleep 1
done

It is recommended that you run runvtserver.sh through solo.pl (which is provided in /linux_server_admin_scripts_and_instructions/solo.pl. Solo.pl will ensure that only one instance of the script is running. Solo.pl binds to a port, not a lockfile so it won't break if your system randomly hard resets.

Use crontab (crontab -e) to enter the following:

* * * * * /home/USER/solo.pl -port=3801 -verbose ./runvtserver.sh

Port 3801 is the lock port for solo.pl, NOT the port that vtserver uses. vtserver uses port 5728 by default

This provides triple redundancy: When the server starts up crontab will run solo.pl which starts runvtserver.sh which runs vtserver. If vtserver crashes then runvtserver.sh will immediately restart it. Crontab will start runvtserver.sh if it is not running, and solo.pl prevents crontab spawning endless instances of runvtserver.sh.

Orderly Shutdown

vtserver accepts SIG_HUP interrupts. This will cause an orderly shutdown (save and exit). It will also instruct any connected clients to try and reconnect.

killall -s HUP vtserver

serverscript.lua

Placing a serverscript.lua file in a save game's directory will cause it to be executed by vtserver on startup

Placing a plaintext file serverscript.lua in the games save directory will cause the server to execute the script whenever that save is executed. You can use this to write custom mods for the server, give the players stuff when they log in or even redirect players to other servers (you can make stargates)!

Periodic Shutdown and Restart with serverscript.lua

This can be useful to clear the map of dumped vehicles and fix any bugs that may have creeped into the server's state. Here is an example script from the Lets Build A City server that shuts down the server every 6 hours. It also sets the Message Of The Day and shows some other hooks.

SERVER_RESET_PERIOD = 60*6;

 --Worker functions
function timeTillNextServerReset ()
	local NH = turf.NetworkHandler.getInstance();	
	local uptime = NH:getUptimeInSeconds();
	return SERVER_RESET_PERIOD*60 - uptime, uptime;
end

function getFormatedTimeTillReset (timeTillReset)
	local minutesRemaining = math.floor(timeTillReset/60);
	if (minutesRemaining > 2) then return tostring(minutesRemaining) .. " minutes"; end
	return tostring(math.max(0, timeTillReset)) .. " seconds!";
end

-- CustomFunc - code injection
customFunc.OnHour_extra = function (GMS, Net, W, timeOfDay)
	local NH = turf.NetworkHandler.getInstance();	
	local timeToReset = timeTillNextServerReset ();

	if (timeToReset > 10 and timeToReset < 600) then
		NH:broadcastSM ("Server will be rebooting in "..getFormatedTimeTillReset(timeToReset), 0);
	end
end


-- On new player login
customFunc.OnNewPlayer_extra = function (GMS, P, I, PCr, W)
	-- Give them items, send a message, etc
end



-- Toggleable static bool for sending the "restart in X seconds" messages
customFunc.pollServerTick_extra_static_tickCount = 0;

-- This function is called every tile-entity tick (4Hz), not every server frame tick (variable, 15Hz/33Hz/67Hz depending on user configuration as defined in serverprefs_1000.txt)
customFunc.pollServerTick_extra = function (NH) 
	local timeToReset = timeTillNextServerReset ();

	-- Send a "RESTARTING IN X" seconds if we are less than 10 seconds to go and this is the first of every 4 ticks.
	if (timeToReset < 10 and timeToReset > 0 and customFunc.pollServerTick_extra_static_tickCount == 0) then
		NH:broadcastSM ("Rebooting Server in ".. tostring(timeToReset) .. " seconds!", 0);
		NH:playSoundToAllClients ("CLICK");
	end
	customFunc.pollServerTick_extra_static_tickCount = customFunc.pollServerTick_extra_static_tickCount+1;
	if (customFunc.pollServerTick_extra_static_tickCount >= 4) then customFunc.pollServerTick_extra_static_tickCount = 0; end

	if (timeToReset <= 0) then
		NH:sendAllPlayersToServer ("", ""); -- Telling players to go to (empty string) server tells them to drop and reconnect to this server
		NH:flagShutdown (); -- Showdown the server. You'll have to restart it with a shell script
	end
end


-- No missions on this server!
--turf.MissionHandler.getInstance():removalAllMissions();

-- Set the MOTD
turf.NetworkHandler.getInstance():setMessageOfTheDay ("==============================\nWelcome to the Let's Build A City server!\n    ~SnapperTheTwig\n==============================");