Cloud Server Installation

Follow these steps to create a server and host the lobby server and/or the game server.

In this example, we are going to use Digital Ocean, but the server application can be hosted on any cloud provider of your choice.

If you do not have an account already, you can use this referral link to create an account, and the first two months will be free.

https://m.do.co/c/ca46a35fd4dd

Prerequisites

You will need to download these two applications in order to access your server. One is a SFTP file manager and the other is a SSH command line tool. These tools are for Windows, but there are plenty of SFTP and SSH alternatives on Mac/Linux.

WinSCP: https://winscp.net/eng/index.php

Putty: https://www.putty.org/

When installing Putty, make sure to also install PuttyGen (it is to generate ssh keys).

Create a Droplet

After you created a Digital Ocean account and entered your credit card information, and that your account is ready to use, you are ready to create a Droplet.

1) In Droplets section, click on Create -> Droplet

2) Select the OS: I use Ubuntu 22.04

3) Choose the specs of your server

4) Choose any region (closest to you will have better performances).

5) Select a SSH key (see next section to generate a new one)

6) Select a hostname for your server (can be anything).

7) Click Create Droplet

Once your droplet is ready, the IP of your server will be displayed. You will need that IP later when creating the unity build.

Generate SSH key with PuttyGen

To generate a key, open PuttyGen and click on generate.

Once done, save the private key file on your computer.

Then copy the public key displayed on screen to Digital Ocean.

On Digital Ocean, Add a name to your key and then click on Add SSH key.

Make sure to store your private key file in a secure place, if you lose the key you won’t be able to access your droplet.

Connect to your server with WinSCP

Open WinSCP and create a new connection.

Enter the IP of your server as Host, and enter Root as the username.

The password is blank since we use a SSH key instead.

Click on Advanced, then SSH/Authentication, then select your private key file there.

This will give you access to the server files

You can unzip and upload the NodeJS code into any folder. In my demo I created this folder and uploaded all the JS code there.

/server/api

To access the command line, from WinSCP, click on Commands->Open in Putty.

Once you gain access you can navigate to the server folder with

cd /server/api

Install MongoDB

Follow the instruction to install MongoDB

https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-ubuntu-20-04

If you get an error about libssl, check this to resolve it:

https://askubuntu.com/questions/1403619/mongodb-install-fails-on-ubuntu-22-04-depends-on-libssl1-1-but-it-is-not-insta

Start Mongo with

sudo systemctl start mongod

And make sure it is working correctly

sudo systemctl status mongod

Open mongo command line with

mongo

While mongo is running in the command line prompt, you can run mongo commands. Use this one to change to the admin database to manage users

use admin

Run these command to add mongo users. Make sure to set a different password for each, save them somewhere to use later, and note the database name (in this example its called userdb).

Create a Root user

db.createUser({user:"root", pwd:"123456789", roles:[{role: "dbAdminAnyDatabase", db: "admin"}, {role: "readWriteAnyDatabase", db: "admin"}] })

Create a user for your app

db.createUser({user: "api", pwd: "123456789", roles: [{role: "readWrite", db: "userdb"}]})

And finally I also like to create a read-only user

db.createUser({user: "viewer", pwd: "123456789", roles: [{role: "read", db: "userdb"}]})

If you made a mistake you can delete a user with

db.dropUser("viewer")

Use Ctrl-C at anytime to quit mongo and return to regular ubuntu command line.

Install NodeJS and NPM

Run this to install NodeJS and NPM

sudo apt install nodejs
sudo apt install npm

Install forever globally to run NodeJS as a service

npm install forever -g

Install node modules for the User API app

cd /server/api
npm install

In WinSCP, open the config.js file to edit a few things

Change mongo_user to "api" (or any other mongo user you created for your app)

Change mongo_pass to the password your set for your api user

Change jwt_secret to any random value

In command line, try to run the app to see if it is working

cd /server/api
node server.js

If there are no error message, this means the app is working! Use ctrl-c to quit the app.

Run NodeJS as a service

You can use forever to run NodeJS as a service so it stays on when you close the command line window

forever start --uid userapi -a server.js

When you make any changes, or reupload the app, you can restart with

forever restart userapi

You can also check if it is running properly

forever status

Last updated