## Deploy the server

We are going to deploy the server to a virtual machine on Kamatera . This process should work on any other cloud provider as well. All you need to have is access to a virtual machine with a public IP address.

We are taking into consideration that you already have a virtual machine running with Ubuntu.

You also need to have access to a PostgreSQL database. If you don’t have a PostgreSQL database, there are plenty of cloud providers that offer managed PostgreSQL databases. (One example is Render ). If you don’t want to use managed database, you can always follow the steps to install PostgreSQL on your virtual machine. A nice tutorial can be found here .

### Prepare the virtual machine

1. In order for the deployment script to work, you need to make sure that you have SSH access to the virtual machine.

> You can say that you have SSH access to the machine, if you can connect to it by running the following command from a terminal:

    ssh <user>@<ip_of_machine>

2. Make sure that the database is accessible from the IP of the virtual machine.

3. Make sure you have docker installed on the virtual machine. You can install docker by running the following command, while connected to the virtual machine:

    sudo apt install docker.io

You can also find here a tutorial on how you can install Docker.

2. Make sure you have NodeJs installed on the virtual machine. You can install NodeJs by running the following command, while connected to the virtual machine:

    sudo apt install nodejs
    sudo apt install npm

3. Install and configure nginx if not already configured. We will start the server on port 7777 by default and we need to make sure that whenever the server is accessed on port 80, the requests are redirected to port 7777.

   - install nginx by running the following command, while connected to the virtual machine:

       sudo apt install nginx

   - open the nginx configuration file by running the following command:

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

   - add a reverse proxy configuration to the nginx configuration file:

       server {
           listen 80;
           server_name <your-server-domain-or-ip>;

           location / {
               proxy_pass http://localhost:7777;
               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;
           }
       }

   - test if the nginx configuration is correct by running the following command:

       sudo nginx -t

   - restart the nginx service by running the following command:

       sudo systemctl restart nginx

### Publish the server

1. Create a new file inside the `scripts` directory called `deploy.sh` and copy the content from the `scripts/deploy_example.sh` file.

   - make sure that you replace the `REMOTE_USER` , `REMOTE_IP` and `REMOTE_DIR` with the correct values.

   > `REMOTE_USER` is the user that you use to connect to the virtual machine. `REMOTE_IP` is the IP of the virtual machine. `REMOTE_DIR` is the directory where you want to deploy the server. (e.g. `~/biddo` )

   - make sure you give execute permissions to the script by running the following command from a terminal in the root of the server:

       chmod +x ./scripts/deploy.sh

2. Create a new file inside the root of the server called `ecosystem.config.cjs` and copy the content from the `ecosystem.config.example.cjs`

   - make sure that you replace the values from the `env` object with the correct values. (These are the database credentials that you use to connect to the PostgreSQL database)

   > This file is used by PM2 to start the server. You can find more information about the ecosystem file here

3. Open a terminal in the root of the server and run the following command to deploy the server:

    npm run publish

4. Check if the server is running by accessing the IP of the virtual machine in the browser. You should see a message similar to `API HTTP SERVER 1.0.0` .

> You can also connect to the server using SSH and check the status of the docker container by running the following command:

    docker ps -a

You should see a similar output as the following:

> !!Note!! If you did not implement the `Integrate Google Cloud Storage` tutorial, make sure that the server has enough access to store the files in the filesystem. If you implemented the tutorial, make sure that the server has access to the Google Cloud Storage bucket.

5. See server logs

   - You need to take the container ID and run connect to it, by running the following command:

       docker exec -it <container-id> sh

   - You can see the logs of the server by running the following command:

       pm2 logs

   - You can also see the logs of the server by running the following command:

       docker logs <container-id>

   > You can find the container id by running the `docker ps -a` command.

Congratulations! You have successfully deployed the server to a virtual machine.

## Add SSL certificate

In order to secure the communication between the server and the clients, you need to add an SSL certificate to the server. We are going to use Let’s Encrypt to generate the SSL certificate.

First of all, you need to make sure that you have a bought domain. You can buy a domain from Namecheap , GoDaddy , Google Domains , etc.

There is a short tutorial on how to integrate Let’s Encryps here

Add a new module Setup