Configure Files before uploading

  1. go to APIServer/src/config.json and open it in any text editor.

You have to edit the values in here:

dbName: the MySql Database name that you will create later eg. equal_chat

dbHost: the Host of the DB, if you're running the MySql server in the same server as we will explain here, just leave it as localhost, otherwise you have to add the URL to the External MySql server

dbUser: the Database user default is root

dbPassword the Database password

port if you're running the server locally(for testing) add your port, otherwise, leave it as 443

debug: set this to true if you're running the server locally(for testing). it will show up the Logs if enabled and it will create an HTTP server instead of HTTPS.

androidPackageName: Your app pakcage name. you can get it from build.gradle in Android Studio

secret : which is the JWT secret, it's used to hash the token and send it to server. we recommend using some websites like this to generate a LONG random strings to make it more secure.

Here is an example on how the Config.json should be:

image

  1. You need to generate a Firebase Admin SDK key which is used to verify auth token while signing in.

To do that go to Firebase Console > Project Settings > Service accounts > genereate new private key. image This will download a .json file. Rename it to serviceAccountKey.json and copy it over to src/config/ folder

  1. You will have to add your Agora config for Calls. To do that go to 'Agora's website and create a new account. Then you will need to create a new app in Agora Console.

Get your AppId and Primary Certificate and add them in config.json

How to Setup the Server?

it's highly Recommended to have Ubuntu installed on your server, just to match maximum compatibility.

  1. ssh into your server, you can get your SSH information from your VPS provider.

if you're on Linux/Mac you can use the command (copy and paste it in Terminal):

ssh root@your.server.ip.address

image

if you're on Windows you can use some 3rd party tools like Putty

  1. once you've successfully logged into ssh, start executing the following commands

update the system library

apt-get update

image

now install NodeJS and NPM

press Y to confirm installation

sudo apt install nodejs

image

sudo apt install npm

image

now install MySql

sudo apt install mysql-server

image

Install Redis

apt install redis-server

image

  1. Now it's time to configure MySql. inside the ssh terminal login to mysql using
mysql -u root -p

and for the password just leave it empty

image

after that we need to create the database using the command

CREATE DATABASE db_name_here;

image

add mysql user to the database using:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password'

NOTE: password should be in single quotes ' '

Don't forget to add semicolon at the end to execute the command.

image

run the exit command to exit from MySql

exit

image

Now we need to change MySql Config To do that, We need to open the MySql Config file using a TextEditor. We can use Nano for this.

nano /etc/mysql/mysql.cnf

image

This will open up a Text editor, navigate the cursor to the bottom and paste in the following command (using Ctrl+V)

[mysqld]
sql_mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"

image

Now we need to save the file, press Ctrl+X

Then Press Y for confirmation image

Lastly just restart the MySql service using

sudo service mysql restart
  1. Enable Firewall

    ufw allow ssh
    ufw allow http
    ufw allow https
    ufw enable
  2. Installing NGINX We will use NGINX as a reverse proxy to forwared incoming requests to our NodeJS server. We will also use it to get a FREE SSL Certificate thanks to Let's Encrypt.

You can install NGINX using the command

sudo apt install nginx

Now we need to configure NGINX

sudo nano /etc/nginx/sites-available/default

delete everything in this file and paste in the following:

      server{
      root /var/www/html;

      server_name your_website.com www.your_website.com;
      underscores_in_headers on;

      location / {
      proxy_pass http://localhost:5000; #whatever port your app runs on
      proxy_http_version 1.1;
      proxy_set_header Host $host;
      proxy_pass_request_headers on;
      add_header 'Access-Control-Allow-Origin' "*";
      add_header 'Access-Control-Allow-Methods' 'GET,PUT,POST,DELETE';
      add_header 'Access-Control-Allow-Headers' 'Content-Type';
      add_header 'Access-Control-Allow-Headers' 'Authorization';

      }

      }

      #subdomain
      server{
      server_name join.your_website.com www.join.your_website.com;
      location / {
      proxy_pass http://localhost:5001; #whatever port your app runs on
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_cache_bypass $http_upgrade;
      }

      }

Make sure to replace 'your_website.com' with your website domain

Exit and Save the file using Ctrl+X

You can run the command nginx -t to check if the file syntax is valid

Then we need to restart the NGINX server to apply changes

sudo service ngninx restart
  1. Enable SSL Certifiate

Thanks to Let's Encrypt, we will get a FREE SSL Certificate that's valid for 90 days. We will use CertBot to automatically renew the Certificate before it expires.

apt-get install python3-certbot-nginx

Now run this command with your website domain to enable SSL certificate for main domain AND subdomain(group domain)

sudo certbot --nginx -d your_website.com -d www.your_website.com -d join.your_website.com -d www.join.your_website.com 

Choose redirect(if prompted)

NOTE: make sure that your VPS server is linked to your domain BEFORE running this command.

To make the cerficiate automatically renewed use the following command:

certbot renew --dry-run
  1. now we need to copy the 'APIServer' folder to our server.

You can use any tool that supports SFTP like WinScp on Windows or Cyberduck on Mac or using WinScp or scp command.

copy the folder 'APIServer' to your server. Open a new terminal and paste in the command:

scp -r /path_to_project_folder_on_your_pc/APIServer root@your.server.ip.address:~/

image

NOTE: this command should be executed on your PC not using ssh.

  1. now back to our ssh terminal, to view the folders of our server we can use the command
ls

image

then we need to go into the server folder using:

cd APIServer/

image

  1. now run the command
npm i

this will install ALL dependencies

  1. run the command
npm run build

this will transpile our Typescript to JS.

image

  1. lastly run the server using the command:
npm run start

image

now our server should be up and running.

By this, our server will work normally, however we need to make it survive server-restarts, for that we will use PM2 which will restart the server if the server restarts.

Press Ctrl+C to stop the Server.

  1. Configure PM2:
    • Install PM2
sudo npm i pm2 -g

image

Now run the PM2 Process

npm run pm2

image

Now the process should be up and running

To double check, run the logs command:

pm2 logs app

image

Lastly, we need to add the startup command to allow it to survive server restarts.

pm2 save
pm2 startup

image

To check the status of our process we can use the command:

pm2 status app

image