Setting up a server

## Requirements

There are several requirements that **must** be met before setting up a server. Especially if you want to play over the Internet.

* Basic knowledge of the command line
* Administrator rights are required for the setup
* Make sure you're not behind a firewall. If you are, make sure that the server software is listed as exception. Providing a tutorial for that is not possible, it depends on your firewall.
* Firewalls come in many different sizes and shapes. Your router has one. Your virus scanners may have one.
	+ The UDP and TCP ports that will be used for the game must be forwarded to the machine that runs the game. These ports will need to be opened for the game to run by default:
		- 27000 - UDP & TCP
		- 27003 - UDP
		- 27020 - UDP
		- 27021 - UDP

If these preconditions are not met, the server might not work properly and people might not be able to connect.

## Setting up the server (Windows)

[[1]]

First and foremost, if you wish to run your server on a Windows PC that already has Avorion installed via Steam, you can skip the section: "Installing the Server Files" and simply replace "(C:\steamcmd" with your Steam directory in further instructions, beginning at the section: "Creating a Galaxy". Note also that your server will update when Avorion does through Steam, which may be undesirable. This neatly circumvents the connection issues many users have with their multiplayer server.

#### Installing the Server Files

1. Download and extract SteamCMD. (https://developer.valvesoftware.com/wiki/SteamCMD)
2. Create a directory for the server files, default being C:\steamcmd
3. Locate steamcmd.exe and run it with the following arguments, depending on whether you want to run a default or beta server. Note that you need to replace `<SERVER_PATH>` with the path of the directory you just created. These are entered into the Windows CMD program after navigating to the correct directory.
	* Default: `steamcmd.exe +login anonymous +force_install_dir <SERVER_PATH> +app_update 565060 validate +exit`
	* Beta: `steamcmd.exe +login anonymous +force_install_dir <SERVER_PATH> +app_update 565060 -beta beta validate +exit`

Note: If your <SERVER\_PATH> contains spaces, it is necessary to surround the path with "quotations".

#### Creating a Galaxy

This has to be done every time a new galaxy should be created:

1. Locate and enter the directory of the Avorion server files within the steamCMD directory (C:\steamcmd\steamapps\common\AvorionServer).
2. Copy **server.bat** and rename the copy to **myserver.bat**. The reason for that is that steam overwrites the server.bat every time you update the server.
3. Open **myserver.bat** with an editor.
4. Go ahead and change “avorion\_admin” to your 64bit SteamID to make sure you are the admin of the server.
5. Change the name of your galaxy by editing “avorion\_galaxy” to the name of your galaxy.
6. If you want to change the path where your galaxies are stored, you can add **--datapath C:\PATHHERE** to the list of arguments.
7. Save the changes.
8. Now run **myserver.bat**. This should start the server. Wait for it to finish initializing and then enter `/save` and `/stop` to stop the server. This initializes the galaxy config files.
9. Open the **server.ini** of your galaxy. If you didn't change the path of your galaxies, it is stored at `%appdata%/Avorion/galaxies/<YOURGALAXY>/`
10. An explanation of the available settings in this file is available here.
11. Save the file.
12. Congratulations! You finished setting up your galaxy.

#### Starting the server

To run your server, simply run your **myserver.bat**.

#### Stopping the server

1. **NEVER** stop the server by closing the console. It will cause save issues.
2. **ALWAYS** use /save in-game then follow up with /stop to stop the server.

#### Updating the server

1. Stop the server by running the `/save` and `/stop` commands on the server.
2. **Backup your galaxies before updating.**
3. Locate steamcmd.exe and run the same command as you used to install the server, depending on Beta or Default:
	* Default: `steamcmd.exe +login anonymous +force_install_dir <SERVER_PATH> +app_update 565060 validate +exit`
	* Beta: `steamcmd.exe +login anonymous +force_install_dir <SERVER_PATH> +app_update 565060 -beta beta validate +exit`
4. Now just start the server again by running **myserver.bat**

#### Switching between default and beta

This works exactly the same way as updating the server. Just choose the correct option when executing steamcmd.exe.

***Please note that downgrading from beta to default most certainly WILL corrupt your galaxy files.***

## Setting up the Server (Linux)

### Ubuntu 14.04

As of version 0.29.2, Avorion uses gcc 5, which is unavailable on Ubuntu 14.04 by default. You can install it with these commands:

```
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-5 g++-5

```

### Ubuntu 18.04

As of version 1.0, the above instructions work, but with a few more steps. As of today (April, 2020), the below instructions all work to get the server running. Other configurations may vary. Ubuntu 18.04 steps to install steamcmd:

```
sudo apt-get update
sudo dpkg --add-architecture i386
sudo add-apt-repository multiverse
sudo apt-get update
sudo apt-get install wget gdebi-core libgl1-mesa-dri:i386 libgl1-mesa-glx:i386
sudo apt-get install steamcmd
sudo apt-get install gcc-5 g++-5

```

The rest of the steps are the same as Ubuntu 14.04.

### Installing the Server Files

1. Install SteamCMD. (https://developer.valvesoftware.com/wiki/SteamCMD)
2. Create a directory for the server files.
3. Locate `steamcmd` and run it with the following arguments, depending on whether you want to run a default or beta server. Note that you need to replace `<SERVER_PATH>` with the path of the directory you just created.
	* Default: `steamcmd +login anonymous +force_install_dir <SERVER_PATH> +app_update 565060 validate +exit`
	* Beta: `steamcmd +login anonymous +force_install_dir <SERVER_PATH> +app_update 565060 -beta beta validate +exit`

### Creating a Galaxy

This has to be done every time a new galaxy should be created:

1. Locate and enter the directory of the server files (the one you created earlier).
2. Run server.sh with the following command line options: `./server.sh --galaxy-name <GALAXYNAME> --admin <ADMIN> --datapath <GALAXY_PATH>`
	1. `<GALAXYNAME>`: the name of your galaxy
	2. `<ADMIN>`: your 64bit SteamID to make sure you are the admin of the server
	3. `<GALAXY_PATH>`: the folder where the galaxies should be stored
3. Wait for the server to finish initializing and then enter `/save` and `/stop` to stop the server. This initializes the galaxy config files.
4. Open the **server.ini** of your galaxy. It is stored at `<GALAXY_PATH>/<GALAXYNAME>`.
5. An explanation of the available settings in this file is available here.
6. Save the file.
7. Congratulations! You finished setting up your galaxy.

### Starting the server

To run your server, run:

`./server.sh --galaxy-name <GALAXYNAME> --admin <ADMIN> --datapath <GALAXY_PATH>`

### Stopping the server

1. **NEVER** stop the server by closing the console. It will cause save issues.
2. **ALWAYS** use /save in-game then follow up with /stop to stop the server.

### Updating the server

1. Stop the server by running the `/save` and `/stop` commands on the server.
2. **Backup your galaxies before updating.**
3. Locate steamcmd.sh and run the same command as you used to install the server, depending on Beta or Default:
	* Default: `steamcmd +login anonymous +force_install_dir <SERVER_PATH> +app_update 565060 validate +exit`
	* Beta: `steamcmd +login anonymous +force_install_dir <SERVER_PATH> +app_update 565060 -beta beta validate +exit`
4. Start the server again

### Switching between default and beta

This works exactly the same way as updating the server. Just choose the correct option when executing steamcmd.exe.

***Please note that downgrading from beta to default most certainly WILL corrupt your galaxy files.***

## Further settings

The server will create a server.ini file and a few others, concerning blacklists, whitelists or user groups when it is started up. The files are located in the galaxy folder of the server. More detailed settings can be changed in there.

This file should only be edited when the server is not running, as the server will overwrite its settings in the file on shutdown.
An explanation of the available settings in this file is available here.

## Multithreading

The server supports several settings for multithreading:

* Worker Threads
* Generator Threads
* Script Background Threads

Worker threads are the threads that update the simulation and do the main work. They will run the server.

Generator threads generate new or load old sectors as the players progress and explore the galaxy.

Script Background threads do heavy script calculations (created with the async() call) that might be necessary. One example would be generation of ship models.

### General Recommendations for Multithreading Settings

In the following, hardware concurrency means the amount of threads that your machine can run at the same time. E.g.: A 4 core CPU with hyperthreading has a hardware concurrency of 8, while a e.g. 6 core CPU without hyperthreading has a hardware concurrency of 6. You can look up your hardware concurrency in your task manager on windows, with htop on linux.

It's recommended to have as many worker threads as your hardware concurrency.

It's recommended to have at least 2 generator threads, but never as many as your hardware concurrency, since they might starve the worker threads, which leads to the simulation staggering, which leads to lagging. If you're running a large server with lots of players who like to explore, you can increase the amount of generator threads to decrease hyperspace calculation time. Generator threads will only run for a short time each, while generating a new sector or loading an old one from disk. It can't be predicted for how long they will run, sometimes it can be a few seconds, sometimes up to minutes.

#### Examples for different machines:

Machine: 4 core CPU with Hyperthreading (hardware concurrency of 8):

* 8 Worker Threads
* 2 - 4 Generator Threads (for 5 - 15 players)
* 2 Script Background Threads

Machine: 8 core CPU with Hyperthreading (hardware concurrency of 16):

* 16 Worker Threads
* 4 - 8 Generator Threads (for 10 - 20 players)
* 2 - 4 Script Background Threads

Machine: 6 core CPU without Hyperthreading (hardware concurrency of 6):

* 6 Worker Threads
* 2 - 3 Generator Threads (for 5 - 10 players)
* 2 Script Background Threads

Machine: 16 core CPU with Hyperthreading (hardware concurrency of 32)

* 32 Worker Threads
* 8 Generator Threads
* 2 - 4 Script Background Threads

## Log File

The server will create log files when it is started up. The log files are located in the galaxy folder. They're called "serverlog [Date].txt". This logfile contains a lot of data concerning the activities on the server, such as a full chat log, or when which player logged in.

## Finding Errors

Should your server not run properly, there are multiple options for checking what's wrong.

### Log File

The log file does not only contain game-relevant data, but also a lot of technical data, such as connection problem diagnosis. You may want to check the log file for errors concerning the server.

### Tracing

This option will make the server print a lot more output, but it may also hurt performance a lot. To enable this, start the server with the additional command line options -t all.

`bin\AvorionServer.exe --datapath "C:\MyAwesomeGalaxy" --admin AwesomeUser -t all`

Your server will now emit a lot more information while running, which may help you to debug any problems arising with it.

Remember: This will hurt performance of the server and should only be used for debugging purposes.

## Troubleshooting

### Missing Libraries on Linux

If you run into an error similar to this:

```
./bin/AvorionServer: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by ./bin/AvorionServer)

```

#### Installing gcc 5

One way to resolve the issue is to try installing gcc 5 (see above). As of version 0.29.2 Avorion uses gcc 5 on linux, which may not be available for each system by default (ie. Ubuntu 14.04).

#### Running the server in the steam runtime

One possible way to fix that problem is to run the server in the official steam runtime:

* Download the latest 'steam-runtime.tar.xz' from http://repo.steampowered.com/steamrt-images-scout/snapshots/
* Exctract it to a folder of your choice
* On a command line, execute the script './setup.sh' in the steam runtime folder. This will adjust the steam runtime to work with your system. It is not necessary to run this command as root, as it will only change files inside of the steam runtime folder.
* Now, open your server's server.sh and search for the line './bin/AvorionServer ...'. Modify it so that it now starts with '<steam-rt>/run.sh ./bin/AvorionServer ...', where '<steam-rt>' is the path of the steam runtime directory.