Quick Start
NodeJS Server
Deployment

Deploy the server

We are going to deploy the server to a virtual machine on Kamatera (opens in a new tab). 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 (opens in a new tab)). 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 (opens in a new tab).

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>
  1. Make sure that the database is accessible from the IP of the virtual machine.

  2. 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 (opens in a new tab) a tutorial on how you can install Docker.

  1. 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
  1. Install and configure nginx (opens in a new tab) 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

    deploy script
  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)

    ecosystem

    This file is used by PM2 to start the server. You can find more information about the ecosystem file here (opens in a new tab)


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

npm run publish
  1. 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:


Status

!!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.

  1. 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.