Repository made public for transparency.
There are 2 .env files which contain runtime environment variables, one each for backend and frontend. Templates are provided in the .env.example files.
It turns out:
- For development, the
.envfile for both backend and frontend are necessary. - For production, only the
.envfile for backned is necessary. The environment variable values used in production will be taken into account when building the production files (npm run build), as described here.
Because we deal with 2 machines (development and production), I'll refer to them using bash prefixes:
- Development machine:
_@dev:<pwd>$ - Production machine:
_@prod:<pwd>$
For convention, this repository's parent on the file systems is assumed to be the home directory ~.
- For backend:
_@dev:~/sthouse/backend$ bacon clippyto check_@dev:~/sthouse/backend$ bacon runto run
- For frontend:
_@dev:~/sthouse/frontend$ npm startto run
_@dev:~/sthouse$ mkcert 127.0.0.1 localhost
Development cert.pem and key.pem files are stored in the repository root and can be generated via steps outlined here.
These instructions are for a production machine that runs Ubuntu.
-
Ensure dependencies. Mainly, ensure that Node is recent enough (Node is the JavaScript runtime; so that modern JavaScript syntax is accepted; the specific one that failed with Nodev12 was the null coalescing operator):
This step is not necessary on a fresh setup. Only necessary if_@prod:~$ sudo apt purge nodejsnodejsornpmwas previously installed viaapt._@prod:~$ sudo rm -r /etc/apt/sources.list.d/nodesource.list
This step is not necessary on a fresh setup. Only necessary ifnodejsornpmwas previously installed viaapt._@prod:~$ curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -_@prod:~$ sudo apt-get install -y nodejs_@prod:~$ sudo npm install -g serve
This is used to deploy the frontend. We aren't cloning this repository onto the production machine, so we install this npm package globally with-g. Command may fail withoutsudo.
A system restart is recommended after performing steps iv:
sudo shutdown -r now.
Original instructions from here. -
_@prod:~$ mkdir -p sthouse/backend sthouse/frontend
This creates the appropriate folder structure for deployment.-pignores if exists, and creates intermediate parents. -
TLS:
_@prod:~/sthouse$ openssl req -new -nodes -keyout production.key -out production.csr
production.keyis the key file.- The non-crossed out values were provided. Those crossed out,
.is provided.- Country Name (2 letter code)
- State or Province Name (full name)
- Locality Name (eg, city)
Organization Name (eg, company)Organizational Unit Name (eg, section)- Common Name (e.g. server FQDN or YOUR name):
stpaulswalk-jswz.servehttp.com Email AddressA challenge passwordAn optional company name
- The non-crossed out values were provided. Those crossed out,
_@prod:~/sthouse$ cat production.csrand provide these values to NoIP.- After some delay, NoIP will email you the cert file. Transfer that to
_@prod:~/sthouse.
Original instructions from here.
-
Environment variables:
_@prod:~/sthouse/backend$ wget --no-cache https://raw.githubusercontent.com/jackykwe/sthouse/main/backend/.env.example_@prod:~/sthouse/backend$ mv .env.example .env_@prod:~/sthouse/backend$ nano .envand fill in the necessary values.
No setting up environment variables for frontend is required as explained above.
-
For backend, because we deploy onto a different architecture, cross-compilation may be necessary (compilation on production machine was prohibitively slow):
-
_@prod:~$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
This attempts to installs Rust. No need to follow through, just note the "Current installation options: default host triple" value. I'll refer to this as<prod_arch>.This command comes from here.
-
_@dev:~$ rustup target add <prod_arch>(Remember to fill in the gaps)
This installs cross-compilation tools. -
_@dev:~/sthouse/backend$ CARGO_TARGET_<PROD_ARCH_ALL_CAPS_HYPHENS_INTO_UNDERSCORES>_LINKER=arm-linux-gnueabihf-gcc cargo build --release --target <prod_arch>(Remember to fill in the gaps)- There was a massive rabbit hole that I went through to install the pre-requisite dependencies at this stage. The one needed was
arm-linux-gnueabihf-gcc. The installation order was, iirc:arm-linux-gnueabihf-binutils(needed by stage1)arm-linux-gnueabihf-gcc-stage1arm-linux-gnueabihf-linux-api-headers(needed by glibc-headers)arm-linux-gnueabihf-glibc-headers(needed by stage2)arm-linux-gnueabihf-gcc-stage2(conflicts with and will remove stage1)arm-linux-gnueabihf-glibc(needed by gcc)arm-linux-gnueabihf-gcc(conflicts with and will remove stage2)
- Since I'm on an Arch-based OS,
makepkg -sriwas a common command to build these. - Sometimes, public key verification fails when building these packages (obtained from AUR).
gpg --recv-key <ID>resolved these, where<ID>is obtained from the error message following verification error
- There was a massive rabbit hole that I went through to install the pre-requisite dependencies at this stage. The one needed was
-
_@dev:~/sthouse/backend$ scp target/<prod_arch>/release/backend _@prod:~/sthouse/backend(Remember to fill in the gaps) -
_@prod~/sthouse/backend$ ./backendto serve the backend.
-
-
For frontend:
- Change the frontend
.envvalues to the production values. Important; explained above. _@dev:~/sthouse/frontend$ npm run build._@dev:~/sthouse/frontend$ tar cvzf build.tar.gz build_@dev:~/sthouse/frontend$ scp build.tar.gz _@prod:~/sthouse/frontend_@prod:~/sthouse/frontend$ rm -r build_@prod:~/sthouse/frontend$ tar xvzf build.tar.gz
After this step, changes are deployed automatically. There's no need to restart the production machine._@prod:~/sthouse/frontend$ serve -s build -l <port> --ssl-cert=<path to cert file> --ssl-key=<path to key file>to serve the frontend. (Remember to fill in the gaps)
- Change the frontend
-
To automate the above during startup, we use
systemd:-
(Remember to fill in the gaps)
_@prod:~/sthouse$ cat <<EOF > startup.sh #!/bin/bash cd ~/sthouse/backend ./backend & cd ~/sthouse/frontend serve -s build -l <port> --ssl-cert=<path to cert file> --ssl-key=<path to key file> & EOF _@prod:~/sthouse$ chmod +x startup.sh_@prod:~/sthouse$ crontab -eand append this line:@reboot sh ~/sthouse/startup.sh
Now the backend and frontends should startup automatically on reboot.
-