UNET Server Hosting Tutorial
In this Tutorial we will learn how to build a Headless Mode UNET server, find a decent host and then use SSH to transfer and run our game server on it. We will explain the whole process step by step, so that everyone can understand it.
Building in Headless Mode
Unity allows us to build Linux executables as Headless, so that no graphics card is needed, which saves lots of performance and is perfect for game servers.
Note: headless mode currently only works for Linux builds, so that's our best option.
Our first step is to open our UNET Project and select File->Build Settings... where we build for Linux with Headless Mode enabled:
After pressing the Build button, Unity will create the headless executable in the directory that we specified. We have to transfer the build to the server later, so let's also compress it as one zip file and name it headless.zip:
Renting a Server
In order to host the game server on the Internet, we first need to rent a server. Most hosting companies offer virtual servers and dedicated servers. A virtual server means that we share the resources with a few other virtual servers, that all run one one physical machine. A dedicated server means that it's just one physical machine all for us alone.
When it comes to the hardware, we have to understand that all our Unity Scripts run in a single thread, so we should prefer a server with only a few really fast cores over many slow cores. For example, 4 cores with 4 GHz would give us better performance than 8 cores with 2 GHz. The amount of RAM mostly depends on the amount of entities in our Scene, so it's recommended to test the game locally first and monitor the RAM usage over a few hours. The hard drive should be an SSD, so that loading and saving data from/to it happens as fast as possible. The size of the disk mostly depends on the amount of players in the database, which starts small and then grows over time.
The best choice for the server's operating system is a Linux that is supported by Unity. According to posts on the Unity forums, their main target is Ubuntu Linux, but Linux Mint (which is an Ubuntu derivate) works just as well.
For real world game servers, we recommend renting a dedicated server. For development and testing, a virtual server (also known as VPS) is totally fine though.
As the next step, we will rent a host. We recommend to use a major hosting company that doesn't mess with the networking settings too much. Smaller companies tend to have weird configurations and limitations that may cause UNET bugs. The following hoster works perfectly fine with our uMMORPG and has outstanding customer support:
It's important to select Ubuntu as the operating system.
After purchasing, it will take up to 2 hours for them to setup our server. Afterwards we will receive an email with the SSH credentials. The email contains something like:
- IP Address: 18.104.22.168
- Port: 22
- Username: root
- Password: password
Configuring the Server
SSH and SCP
We will need the SCP command to transfer the file and the SSH command to configure the server.
SSH stands for secure shell, which is like running a Terminal or Command Prompt on the remote server. On Linux and MacOS, we can simply open a Terminal and use the ssh command directly. On Windows systems we need the PuTTY tool for that.
Note: the rest of this Tutorial will use the Linux/MacOS Terminal. Windows users will need to use the tool mentioned above and click a button here and there.
SCP is a tool that copies a file from our local machine over SSH to the remote server. On Linux/MacOS we can use the scp command in the Terminal. On Windows we need the WinSCP tool.
Note: the rest of this Tutorial will use the Linux/MacOS Terminal. Windows users will need to use the tool mentioned above and click through the interface to transfer the file.
Transferring the File
Alright, let's begin by copying the headless.zip file to the server. We can open a Terminal and then run the following command:
scp /path/to/headless.zip firstname.lastname@example.org:~/headless.zip
Note: replace the first path with the path to your headless.zip file and replace the IP with your server's IP address.
We will be asked for our password (the SSH password) after a few seconds. Afterwards it starts transferring the file:
Unpacking the File
Now we can establish a ssh connection to the server:
Note: 22.214.171.124 is just an example IP, you will need to use the one from your server.
Afterwards we may get a warning that asks us if we want to continue. In this case we enter yes. Afterwards we also enter the SSH password. Now our Terminal looks like this:
We can enter ls to see the files on the server:
Which shows us our file:
Awesome! Let's install the unzip tool really quick:
apt-get install unzip
Now we can unzip our file:
Running the Server
We can run the server with the following command:
If you get a 'file not found' error:
- Type 'ls' to make sure that the file really exists
- If it exists and still shows the error, it's because you are trying to run a 32 bit binary on a 64 bit system that doesn't have 32 bit support installed. Run the following commands, then try again.
sudo dpkg --add-architecture i386
sudo apt-get update
sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386
If you get a 'permission denied' error:
- Try setting CHMOD to executable
- Try running it as super user with:
After successfully running it, we can see the default Unity headless output:
Note: closing the Terminal window will also close the server.
It's important to know that this command just runs the game server. UNET also requires us to call the StartServer function after startup. Read our Detect Headless Mode Tutorial for more info.
Note: uMMORPG already does that automatically.
Showing Debug Messages
Sadly Unity doesn't show any Debug.Log/Warning/Error messages here, but there is a workaround. We can open a new Terminal window, ssh to the server again and then constantly show the latest Log messages like this:
tail -f ~/.config/unity3d/noobtuts.com/uMMORPG/Player.log
Note: replace the path with your Project's Company Name and Product Name that can be found in under Edit->Project Settings->Player.
If you also feel like they should show Debug messages directly when running the server, then please reply in our Unity Thread.
Keeping the Server running after closing the Console
By default, the server process will end as soon as we close the SSH session. The solution is a little tool called screen. Here is how to use it:
Install it on the server:
sudo apt-get install screen
Start a new screen session:
Start the game server:
Detach from the screen session with:
Ctrl + A and then Ctrl + D
You will see a [detached] message. The server is still running and you can close the console.
Connecting with a Game Client
Finally we can open our Unity Project, select the NetworkManager component and insert our server's IP address under Network Info->Network Address in the Inspector. If we press Play then we can now connect to our remote server!
In this Tutorial we learned how to run the server remotely in the easiest way possible. It is also recommened to learn how to secure a Ubuntu server and how to run the game as a non-root user.