Chris-D-Turk / Valheim.Google.Cloud

34 stars 2 forks source link

How to run Valheim Server in Google Cloud (90 days free trial)

In this tutorial you will learn how to setup a Valheim Dedicated Server in the Google Cloud

Some notes before we start:

Table of contents

Start Google Cloud Trial

Create VM

create-vm.png


The VM should appear as online after some seconds:

vm-created.png

[^1]: Update 20230101 Valheim needs more memory since Mistlands (see Reddit comment)

Connect via SSH

For some of the following steps you will need a SSH connection to your VM (to execute shell commands)

The remote shell should open in a new browser window:

connect-ssh.png

Port Forwarding

You need to forward the UDP ports 2456-2457 in the firewall settings to allow players to connect:

create-firewall-rule.png

Optional: Setup Static IP

If you want your server to always have the same IP:

static-ip.png

Note: Static IPs increase the monthly fee (after trial phase)

Optional: Setup DynDNS

Instead of a static IP you can setup a DynDNS service so that your server is always reachable at the same host name.
The following example uses noip.com (up to 3 DNS entries for free - but the entries need to be "confirmed" every 30 days).

Now you will need to install the No-IP Dynamic Update Client that will regulary transmit the VM's current IP to no-ip.com so that your DNS entry stays connected with your VM.

cd /usr/local/src curl -sqL "http://www.no-ip.com/client/linux/noip-duc-linux.tar.gz" | sudo tar xzf -

cd noip-2.1.9-1 sudo make sudo make install sudo noip2 -C

Enter your No-IP user/password when asked.  
The Dynamic Update Client ("noip2") will be started in "start_server_custom.sh" script (see [Setup Dedicated Server Daemon](#setup-dedicated-server-daemon))

## Optional: Schedule Start/Stop
The start_server_custom.sh script (see below) automatically performs a worlds backup on shutdown.
So to perform a backup at least once a day, you should schedule an automatic shutdown.  
You can skip this step, if you don't want automatic backups and want the VM to run 24/7 (ok during the free trial)

First you need to grant the default service account the permission to start/stop your VM: 
- Goto https://console.cloud.google.com/iam-admin
- Select the checkbox "Include Google-provided role grants"
- Edit the account ending with "@compute-system.iam.gserviceaccount.com"
- Add the role "Compute Instance Admin (v1)" and save

Then create the schedule and assign your VM to it:
- Open your VM from https://console.cloud.google.com/compute/instances  
- Select "Instance Schedule" and click "Create Schedule"
- Enter a suitable name like "stop-at-4am"
- Select your region
- Enter a stop time
- Optional: Enter a start time
- Enter your timezone
- Enter "Repeat daily" as frequency
- Click "Create"

<img src="https://github.com/Chris-D-Turk/Valheim.Google.Cloud/raw/master/screenshots/create-schedule.png" alt="create-schedule.png" height="200px" />

<br/>

Select the created schedule and add your VM to it:

<img src="https://github.com/Chris-D-Turk/Valheim.Google.Cloud/raw/master/screenshots/attach-vm-to-schedule.png" alt="attach-vm-to-schedule.png" height="200px" />

## Install SteamCMD
Execute the following commands to install [SteamCMD](https://developer.valvesoftware.com/wiki/SteamCMD#Linux):
- Create Steam directory: `mkdir ~/Steam && cd ~/Steam`
- Install SteamCMD dependencies: 

sudo apt install lib32gcc-s1 sudo apt install zip sudo apt install unzip

- Download and extract SteamCMD: `curl -sqL "https://steamcdn-a.akamaihd.net/client/installer/steamcmd_linux.tar.gz" | tar zxvf -`

## Install Valheim Dedicated Server
Execute the following SteamCMD commands to install the Valheim Dedicated Server:
- Start SteamCMD: `./steamcmd.sh `
- Login: `login anonymous`
- Install [Valheim Server](https://steamdb.info/app/896660/): `app_update 896660`
- Exit SteamCMD: `exit`
- Install Valheim Server dependencies (see Valheim Dedicated Server Manual.pdf):

sudo apt install libatomic1 sudo apt install libpulse-dev sudo apt install libpulse0


## Create Swap File
You should create a [Swap file](https://linuxize.com/post/create-a-linux-swap-file/) to allow the OS to swap memory to the disk, when the system runs low on memory (otherwise your Valheim Server might crash)

Enter the following commands to create and register the swap file:
```bash
sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo vim /etc/fstab

The last command opens the file /etc/fstab for editing using "vim". Hit "i" on your keyboard to start editing and add the following line at the end: /swapfile swap swap defaults 0 0

Then hit "Escape" and type :wq! to save the changes to the file.

Optional: Install MegaCMD

You can skip this step, if you don't want the server to automatically backup your worlds to the mega.nz Cloud

Optional: Upload your world

You can upload your existing world to the VM:

Setup Dedicated Server Daemon

You should register the Valheim Dedicated Server as a "Daemon" to automatically start the server when your VM boots up and to cleanly(!) stop the server when the VM shuts down.
Otherwise your world might get corrupted when the VM is shut down.

For this you need two scripts: start_server_custom.sh and valheim.sh (download these to your local PC: https://github.com/Chris-D-Turk/Valheim.Google.Cloud/archive/refs/heads/master.zip)
The start_server_custom.sh is a modified version of the start_server.sh that is shipped with the Valheim Dedicated Server.
The valheim.sh is the actual "Daemon" that is run by the OS on startup/shutdown.

Upload both scripts to your VM using the SSH window (cog symbol in the top/right corner and than "Upload File")
Then move the scripts to the right location and register the daemon:

mv ~/start_server_custom.sh ~/Steam/steamapps/common/Valheim\ dedicated\ server/
chmod u+x ~/Steam/steamapps/common/Valheim\ dedicated\ server/start_server_custom.sh 

sudo mv ~/valheim.sh /etc/init.d
chmod u+x /etc/init.d/valheim.sh 
sudo update-rc.d valheim.sh defaults

Now you should be able to query the daemon status using: sudo systemctl status valheim.service
You should see the following output:

● valheim.service - LSB: valheim service
   Loaded: loaded (/etc/init.d/valheim.sh; generated)
   Active: inactive (dead)
     Docs: man:systemd-sysv-generator(8)

You need to create two additional "init" scripts to specify your environment-specific parameters
start_server_custom_init.sh

cd ~/Steam/steamapps/common/Valheim\ dedicated\ server/
vim start_server_custom_init.sh

Enter the following parameters:

#! /bin/bash
server_name=""
server_password=""
world_name=""
crossplay=true
backup_enabled=true
backup_remote_dir="valheim.backup"
mega_session=""
auto_update=true

Then hit "Escape" and type :wq! to save the changes to the file.
Make the file executable: chmod u+x start_server_custom_init.sh

valheim_init.sh

cd /etc/init.d
sudo vim valheim_init.sh

Enter the following parameters:

#! /bin/bash
daemon_user=""
valheim_basedir=/home/$daemon_user/Steam/steamapps/common/Valheim\ dedicated\ server
valheim_pidfile=$valheim_basedir/valheim.pid
daemon_exec=/bin/bash
daemon_args=$valheim_basedir/start_server_custom.sh

Then hit "Escape" and type :wq! to save the changes to the file.
Make the file executable: sudo chmod u+x valheim_init.sh

Start the daemon using: sudo systemctl start valheim.service
Check the status again: sudo systemctl status valheim.service
The daemon should appear as 'running':

● valheim.service - LSB: valheim service
   Loaded: loaded (/etc/init.d/valheim.sh; generated)
   Active: active (running) since Sun 2021-09-26 15:34:43 UTC; 4min 7s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1584 ExecStart=/etc/init.d/valheim.sh start (code=exited, status=0/SUCCESS)
    Tasks: 37 (limit: 2310)
   Memory: 1.6G
   CGroup: ...

Now you should be able to connect to your server in-game using the External IP shown at: https://console.cloud.google.com/compute/instances

Troubleshoot

Useful commands: