22title : Deployment Guide
33---
44
5- ## Deploy to any server
6-
7- ### Remote Server Preparations
8- > We will initialize a server and set up an automatic deployment from GitHub Actions.
9- >
10- > This setup assumes the SNM-I deployment server is behind a reverse proxy,
11- > we also need to set up ssh port forwarding on the reverse proxy to make the ssh server accessible from the public.
12- >
13- ### GitHub Secrets and Variable to Set up
14-
15- | KEY | Example Value | Variable or Secret? |
16- | -------------------------| ----------------------------------------------------| ---------------------|
17- | RELEASE_SSH_HOST | 206.12.97.46 | Variable |
18- | RELEASE_SSH_KNOWN_HOSTS | [ 206.12.97.46] :14572 ecdsa-sha2-nistp256 AAAAE2... | Variable |
19- | RELEASE_SSH_PORT | 14572 | Variable |
20- | RELEASE_SSH_USER | ubuntu | Variable |
21- | RELEASE_SSH_PRIVATE_KEY | -----BEGIN OPENSSH PRIVATE KEY-----\nb3Blbn.... | ** Secret** |
22-
23- #### Remote Server Requirement
5+ # Deploy to any server
6+ A push to the relative branch triggers the GitHub Actions to compile the frontend, and run tests if existed.
7+ Then it ssh into the remote server to executive a sequence of commands to update the code & compiled frontend.
8+
9+ ## Things you might what to know
10+ - Docker and how to install Docker Engine
11+ - Understand Docker Compose
12+ - GitHub Actions and GitHub Workflow file
13+ - GitHub Secrets/variables to store credentials/configurations
14+ - How to set up SSH with the private key
15+ - How to preconfigure the SSH ` know_hosts ` in an CI environment
16+ - How to ssh and execute commands on a remote server in an CI environment
17+ - Use rsync to synchronize files to remote servers
18+ - How to host static files (compiled react frontend) in Caddy
19+ - How to use iptables to do a port forward (In the server that hosts a reverse proxy)
20+
21+ ## GitHub Secrets and Variable to Set up
22+
23+ We will go through each of the variables/secrets.
24+
25+ | KEY | Example Value | Variable or Secret? |
26+ | -----------------------| ----------------------------------------------------| ---------------------|
27+ | BETA_SSH_HOST | 206.12.97.46 | Variable |
28+ | BETA_SSH_KNOWN_HOSTS | [ 206.12.97.46] :14572 ecdsa-sha2-nistp256 AAAAE2... | Variable |
29+ | BETA_SSH_PORT | 14572 | Variable |
30+ | BETA_SSH_USER | ubuntu | Variable |
31+ | BETA_SSH_PRIVATE_KEY | -----BEGIN OPENSSH PRIVATE KEY-----\nb3Blbn.... | ** Secret** |
32+
33+
34+ ## Remote Servers Overview
35+ We will initialize a server for SNM-I and set up an automatic deployment from GitHub Actions.
36+
37+ This setup assumes the SNM-I deployment server is behind a reverse proxy,
38+ we also need to set up ssh port forwarding on the reverse proxy to make the ssh server accessible from the public.
39+
40+ The following example assumes:
41+ - The SNM-I is hosted on the subdomain ` beta.socialneedsmarketplace.ca ` .
42+ - SNM-I server has an internal IP ** 192.168.41.202** .
43+ - Reverse proxy server has an internal IP ** 192.168.41.156** and external IP ** 206.12.97.46** .
44+
45+
46+ ### SSH port forward
47+ ssh to ` 206.12.97.46:14572 ` goes directly into the SNM-I server.
48+ ``` text
49+ --------------------- ------------------------
50+ | SNM-I Server | | Reverse Proxy Server |
51+ | 192.168.41.202:22 | <-> | 192.168.41.156:14572 |
52+ | | | 206.12.97.46:14572 | <-> Internet
53+ --------------------- ------------------------
54+ ```
55+
56+ ### Webserver reverse proxy
57+
58+ #### Frontend includes docs
59+ The compiled frontend and docs are hosted on port 80.
60+ ``` text
61+ beta.socialneedsmarketplace.ca -> 192.168.41.202:80
62+ ```
63+
64+ #### Backend API
65+ The Node.js Backend opens a port 5000 for api calls.
66+ ``` text
67+ beta.socialneedsmarketplace.ca/api/* -> 192.168.41.202:5000
68+ ```
69+
70+
71+ ## SNM-I Server Preparations
2472Recommended OS: Ubuntu 22.04 or above
2573
26- Follow [ Docker Engine Installation] ( https://docs.docker.com/engine/install/ubuntu/ ) .
74+ ### Setup Docker
75+ > Follow the [ official guide] ( https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository )
76+ ``` shell
77+ # Add Docker's official GPG key:
78+ sudo apt-get update
79+ sudo apt-get install ca-certificates curl
80+ sudo install -m 0755 -d /etc/apt/keyrings
81+ sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
82+ sudo chmod a+r /etc/apt/keyrings/docker.asc
83+
84+ # Add the repository to Apt sources:
85+ echo \
86+ " deb [arch=$( dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
87+ $( . /etc/os-release && echo " $VERSION_CODENAME " ) stable" | \
88+ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
89+ sudo apt-get update
90+
91+ # Install the latest version
92+ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
93+ ```
2794
2895Create an ` .env ` in the root directory ` ~/ ` that specifies the correct email server and graphdb server location & credentials.
2996
30- #### Generate SSH KEY on Remote Server
97+ ### Generate SSH KEY on Remote Server
3198``` shell
3299ssh-keygen
33100```
@@ -41,11 +108,12 @@ Your identification has been saved in /home/ubuntu/.ssh/id_rsa
41108Your public key has been saved in /home/ubuntu/.ssh/id_rsa.pub
42109...
43110```
44- #### Add to ` authorized_keys `
111+ ### Add to ` authorized_keys `
112+ This authorizes the generated key.
45113``` shell
46114cat ./.ssh/id_rsa.pub >> ./.ssh/authorized_keys
47115```
48- #### Copy the private key ` id_rsa ` to GitHub Secret
116+ ### Copy the private key ` id_rsa ` to GitHub Secret
49117``` shell
50118cat ./.ssh/id_rsa
51119```
@@ -58,17 +126,13 @@ b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn
58126```
591271 . Navigate to "SNM-I Repo Setting" -> "Security" -> "Secrets and variables" -> "Actions" -> [ "Secrets"] ( https://github.com/csse-uoft/SNM-I/settings/secrets/actions ) .
601281 . Click "New repository secret"
61- 1 . Set the name to ` RELEASE_SSH_PRIVATE_KEY ` and copy the whole private key to the secret field.
62- #### Set ` RELEASE_SSH_KNOWN_HOSTS `
63- On Windows or Ubuntu, ssh to the server and disconnect. Open ` ~/.ssh/known_hosts ` . Copy the line for the remote server.
64- ``` text
65- [206.12.97.46]:14572 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBBxpcp7MHTdVfkaFHitfrRdvmXBXDLC+s4FcFb75oaAmxPAj2FeEEwRRaVv0/jXhiaPqsHl92OdY2xiRVHkZsMM=
66- ```
67- 1 . Navigate to "SNM-I Repo Setting" -> "Security" -> "Secrets and variables" -> "Actions" -> [ "Variables"] ( https://github.com/csse-uoft/SNM-I/settings/variables/actions ) .
68- 1 . Click "New repository variable"
69- 1 . Set the name to ` RELEASE_SSH_KNOWN_HOSTS ` and copy the whole line to the secret field.
129+ 1 . Set the name to ` BETA_SSH_PRIVATE_KEY ` and copy the whole private key to the secret field.
70130
71- #### Set up ssh port forwarding on ** Reverse Proxy Server**
131+
132+
133+ ## Reverse Proxy Server Setup
134+
135+ ### Set up ssh port forwarding on ** Reverse Proxy Server**
72136> The following command should be executed on the reverse proxy server.
73137 Consider the following setup:
74138``` text
@@ -93,30 +157,61 @@ Add `net.ipv4.ip_forward = 1` to the bottom. Apply the change.
93157sysctl -p
94158```
95159
96-
97160Add ` PREROUTING ` and ` POSTROUTING ` rules to ` iptables ` :
98161``` shell
99162# ens3 is the network interface that has the public ip
100163# Forward all requests of 206.12.97.46:14572 to 192.168.41.202:22
101164sudo iptables -t nat -A PREROUTING -i ens3 -p tcp --dport 14572 -j DNAT --to-destination 192.168.41.202:22
102- sudo iptables -t nat -A POSTROUTING -p tcp --dport 22 -j SNAT --to-source 192.168.41.156
165+ sudo iptables -t nat -A POSTROUTING -d 192.168.41.202 - p tcp --dport 22 -j SNAT --to-source 192.168.41.156
103166```
104167Make the iptables config persistent.
105168``` shell
106169sudo apt update && sudo apt install iptables-persistent
170+ sudo sh -c ' /sbin/iptables-save > /etc/iptables/rules.v4'
171+ ```
172+ ### Setup Caddy Reverse Proxy
173+ ``` text
174+ beta.socialneedsmarketplace.ca {
175+ handle /api* {
176+ reverse_proxy 192.168.41.202:5000
177+ }
178+ reverse_proxy 192.168.41.202:80
179+ }
107180```
108181
109- #### Set Other Variables
182+ ### Add DNS Record
183+ ``` text
184+ Host name Type TTL Data
185+ beta.socialneedsmarketplace.ca A 1 hour 206.12.97.46
186+ ```
187+
188+ ## Set Other Variables
110189Add the following Variables as well according to how you ssh to the remote server by using port forwarding:
111- - ` RELEASE_SSH_HOST ` : 206.12.97.46
112- - ` RELEASE_SSH_PORT ` : 14572
113- - ` RELEASE_SSH_USER ` : ubuntu
190+ - ` BETA_SSH_HOST ` : 206.12.97.46
191+ - ` BETA_SSH_PORT ` : 14572
192+ - ` BETA_SSH_USER ` : ubuntu
114193
115- ### Add GitHub Workflow
116- https://github.com/csse-uoft/SNM-I/blob/master/.github/workflows/release.yml
194+ ### Set ` BETA_SSH_KNOWN_HOSTS `
195+ If the ssh port forward is correctly configured, you can generate the ` known_hosts ` by:
196+ ``` shell
197+ ssh-keyscan -p 14572 -t ecdsa-sha2-nistp256 206.12.97.46
198+ ```
199+ Output (The second line is ` BETA_SSH_KNOWN_HOSTS ` )
200+ ``` text
201+ # 206.12.97.46:14572 SSH-2.0-OpenSSH_8.9p1 Ubuntu-3ubuntu0.6
202+ [206.12.97.46]:14572 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBBxpcp7MHTdVfkaFHitfrRdvmXBXDLC+s4FcFb75oaAmxPAj2FeEEwRRaVv0/jXhiaPqsHl92OdY2xiRVHkZsMM=
203+ ```
204+ 1 . Navigate to "SNM-I Repo Setting" -> "Security" -> "Secrets and variables" -> "Actions" -> [ "Variables"] ( https://github.com/csse-uoft/SNM-I/settings/variables/actions ) .
205+ 1 . Click "New repository variable"
206+ 1 . Set the name to ` BETA_SSH_KNOWN_HOSTS ` and copy the whole line to the secret field.
207+
208+
209+ ## Add GitHub Workflow
210+ https://github.com/csse-uoft/SNM-I/blob/master/.github/workflows/beta.yml
117211Remember to change the branch name:
118212``` yml
119213on :
120214 push :
121- branches : [ master ]
122- ` ` `
215+ branches : [ beta ]
216+ ...
217+ ```
0 commit comments