Skip to content

Self‐hosting

Leia edited this page Nov 14, 2023 · 9 revisions

Hosting your own Suroi server

Looking to host your own Suroi server to mess around or play against friends? This is the place for you.

Prerequisites

Before diving into the setup instructions, make sure you have the correct tools for the job. Hosting a dedicated Suroi server requires two things:

  • SSH access to a VPS (or some other form of dedicated hosting)
  • The ability to open ports on said machine

You can find cheap VPS hosts at Linode, A2 Hosting, or Vultr.

NOTE: I am not endorsing or recommending any of the following hosts, they are just a few I have found people to use.

If you accept the risk, you can host a dedicated server on your home network. However, you will need to port forward the necessary ports via your router for others to access your server. Do not do this unless you understand what you are doing. Previous Linux knowledge required.

Setup

This tutorial is written assuming you are using Ubuntu 20.04. Follow accordingly based on the distribution you are using.

Logging into the machine

If you bought a VPS (or other form of hosting) from a hosting company, then these credentials should be accessible via their dashboard. Typically, they will consist of three things:

  • IP: The IP of your server.
  • Username The username (usually root), to log in with.
  • Password: The password to log in with.

Optionally, if your VPS provides SSH access on a different port, that will also be listed.

Open a terminal (Command Prompt for Windows) and write the following command:

ssh username@host

replacing username and host with your username and host.

If you have a custom SSH port (not 22), then use this instead:

ssh username@host -p port

additionally replacing port with the custom SSH port provided.

After writing this command, hit enter. You will be prompted for a password.

For security reasons, you cannot view your password as you type it. Type in the password, and hit enter.

If you see a notice notifying you of the machine you have just logged into, congratulations! You have succesfully SSH'd into your server.

Dependencies

Suroi requires a few dependencies:

If you are logged in as root, start by making sure sudo is installed:

apt -y install sudo

Then install git and nginx with the following command.

sudo apt -y install git nginx

To install Node.js, install the appropriate package from your distro, or for Ubuntu:

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - &&\
sudo apt-get install -y nodejs

And finally, install pnpm:

npm i -g pnpm

Building source

Next, move into /opt, clone the repository and traverse into it:

cd /opt
git clone https://github.com/HasangerGames/suroi.git suroi
cd suroi

Install the necessary dependencies:

pnpm install

Update the server config file's:

nano server/src/config.ts
export const Config = {
    host: "0.0.0.0",
    port: 8000,
    // ...

changing host to 0.0.0.0.

Update the client config:

nano client/src/scripts/config.ts
export const Config = {
    regions: {
        default: { name: "My cool server", address: "YOURPUBLICIPHERE", https: false }
    },
    defaultRegion: "default"
    // ...

Replace YOURPUBLICIPHERE with your public IP and set https to true if you have https

Build the client & server:

pnpm build

Make sure the build directory has the proper permissions:

sudo chown -R www-data:www-data /opt/suroi/client/dist

Setting up NGINX

We will now setup NGINX to serve the client, server API, and WebSocket server.

First, remove the default file:

sudo unlink /etc/nginx/sites-enabled/default
sudo rm /etc/nginx/sites-available/default

Create a new file:

nano /etc/nginx/sites-available/suroi.conf

And populate it with the following content:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name _;

    # Client build
    location / {
        root /opt/suroi/client/dist;
    }

    # API server
    location /api {
        proxy_http_version 1.1;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Host $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;

        proxy_pass "http://127.0.0.1:8000";
    }

    # WebSocket server
    location /play {
        proxy_http_version 1.1;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Host $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;

        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";

        proxy_pass "http://127.0.0.1:8000";
        proxy_redirect off;
    }
}

Save the file using Ctrl + X, and press Y to confirm the file name.

To enable the configuration:

sudo ln -s /etc/nginx/sites-available/suroi.conf /etc/nginx/sites-enabled/suroi.conf
sudo systemctl restart nginx

Running the game server

Next, we will create a systemd unit file, which will ensure our application starts at boot and won't terminate if we end our SSH session:

sudo nano /etc/systemd/system/suroi.service

And populate it with the following content:

[Unit]
Description=Suroi dedicated server.

[Service]
Type=simple
WorkingDirectory=/opt/suroi
ExecStart=/usr/bin/pnpm start

[Install]
WantedBy=multi-user.target

Save the file using Ctrl + X, and press Y to confirm the file name.

Enable the unit:

sudo systemctl daemon-reload
sudo systemctl enable --now suroi

If you've done everything correctly, you should be able to access the server at http://youriphere (ex: http://1.1.1.1). Congratulations! You can stop here.

Security

Optionally, let's install a firewall to keep your server safe:

sudo apt -y install ufw fail2ban

Allow the correct ports.

sudo ufw limit 22/tcp
sudo ufw allow 80/tcp

NOTE: If you are using an SSH port other than 22, adjust accordingly.

Enable the firewall.

sudo ufw enable

And that's it! You're all good to go.