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:

To do that go to Firebase Console > Project Settings > Service accounts > genereate new private key.
This will download a .json file. Rename it to serviceAccountKey.json and copy it over to src/config/ folder
Get your AppId and Primary Certificate and add them in config.json
it's highly Recommended to have Ubuntu installed on your server, just to match maximum compatibility.
if you're on Linux/Mac you can use the command (copy and paste it in Terminal):
ssh root@your.server.ip.address

if you're on Windows you can use some 3rd party tools like Putty
update the system library
apt-get update

now install NodeJS and NPM
press Y to confirm installation
sudo apt install nodejs

sudo apt install npm

now install MySql
sudo apt install mysql-server

Install Redis
apt install redis-server

mysql -u root -p
and for the password just leave it empty

after that we need to create the database using the command
CREATE DATABASE db_name_here;

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.

run the exit command to exit from MySql
exit

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

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"

Now we need to save the file, press Ctrl+X
Then Press Y for confirmation

Lastly just restart the MySql service using
sudo service mysql restart
Enable Firewall
ufw allow ssh
ufw allow http
ufw allow https
ufw enable
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
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
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:~/

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

then we need to go into the server folder using:
cd APIServer/

npm i
this will install ALL dependencies
npm run build
this will transpile our Typescript to JS.

npm run start

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.
sudo npm i pm2 -g

Now run the PM2 Process
npm run pm2

Now the process should be up and running
To double check, run the logs command:
pm2 logs app

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

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