Skip to content

Production deployment

Nathanaël edited this page Oct 23, 2020 · 22 revisions

Requirements

  • nodeJS v14
  • mysql or mariadb
  • pm2

Create the database

On the VPS, create the database with:

[mysql]: CREATE USER 'glowing-octo-guacamole'@'localhost' IDENTIFIED BY '__YOUR_PASSWORD_HERE__';
[mysql]: CREATE DATABASE glowingOctoGuacamole;
[mysql]: GRANT ALL PRIVILEGES ON glowingOctoGuacamole.* TO 'glowing-octo-guacamole'@'localhost';

Configure the server

After pulling the repository, create the /server/.env configuration file, and put these informations:

PORT=5035

DB_HOST=localhost
DB_USER=glowing-octo-guacamole
DB_PASSWORD=__YOUR_PASSWORD_HERE__
DB_DATABASE=glowingOctoGuacamole

Launch the app for the first time

In /server/, launch the app for the first time using pm2

$ pm2 start index.js --name glowing-octo-guacamole-prod

Serve the app

You can access to the app with VPS_IP:$POST, or by setting up a proxy as nginx in front of the app. This is our nginx configuration.

server {
    listen [::]:80;
    listen 80;

    server_name __YOUR_URL__

    charset utf-8;
    client_max_body_size 50M;

    location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://127.0.0.1:__YOUR_PORT__
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_next_upstream error timeout http_502 http_503 http_504;
    }
}

Script to update and restart

This script should be called each time code is pushed to the main or production branches.

#!/bin/sh

if [ "$#" -ne 1 ]; then
  echo "Ref branch not precised"
  exit 1
fi

if ! [ "$1" = 'refs/heads/main' ] && ! [ "$1" = 'refs/heads/production' ]; then
  exit 0
fi

if [ "$1" = 'refs/heads/main' ]; then
  cd /home/glowing-octo-guacamole/glowing-octo-guacamole-beta/ || exit 1
else
  cd /home/glowing-octo-guacamole/glowing-octo-guacamole-prod/ || exit 1
fi

echo "Using Node v14"
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
nvm use default || exit 1

echo "Pulling code"
git reset --hard || exit 1

if [ "$1" = 'refs/heads/main' ]; then
  git pull origin main || exit 1
else
  git pull origin production || exit 1
fi

echo "Building client"
cd client || exit 1
npm install --production || exit 1
npm run build || exit 1
cd .. || exit 1
echo "Updating server"
cd server || exit 1
npm install --production || exit 1
echo "Restart server"

if [ "$1" = 'refs/heads/main' ]; then
  pm2 restart glowing-octo-guacamole-beta || exit 1
else
  pm2 restart glowing-octo-guacamole-prod || exit 1
fi

exit 0
Clone this wiki locally