Servers
A live server can be easily set up to serve over local http port to the internet at droplet.bci4kids.ca/service with no additional DNS setup required.
Application1
As the communal ‘git’ user,
- create a directory to hold your app
/home/git/my-service
mkdir ~/my-service
- add and run the server on a chosen port
- get application code in the folder (see below)
- perform initial installation steps (npm install)
- run app with pm2
pm2 start <entry_script> -n <service_name>
More About PM2
You can use the following commands to manage applications running on pm2, providing an application name, process id, or “all” where relevant. Note: pm2 processes are tied to the user account that started them.
pm2 start
pm2 list
om2 stop
pm2 delete
pm2 restart
By default, processes will be killed on a system shutdown/reboot.
To address this, you can add a pm2 startup script:
pm2 startup
If you already have a startup script, new processes are added to it once started by saving the list
pm2 save
Nginx2
- add your service as a location to the
droplet.bci4kids.canginx server block (requires sudo)
sudo vi /etc/nginx/sites-available/droplet.bci4kids.ca
server {
# base configuration and other locations
...
location /service/ {
proxy_pass http://localhost:port_number/;
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;
}
...
# certbot certificate block
...
}
- check for syntax errors in the nginx config
sudo nginx -t
- restart nginx to enable your changes
sudo systemctl restart nginx
- optional - add your new service to the index
- edit
/var/www/droplet.bci4kids.ca/index.htmlto include a link to your new service
- edit
CI/CD
Repository setup will differ depending on your desired build process. I have generally preferred to keep a separate bare repository by convention, but that is a matter of style.
Post-Receive Hook Example3
#!/bin/bash
TARGET="$HOME/my-service"
GIT_DIR="$HOME/my-service.git"
PROCESS=service
echo "Push received, deploying..."
git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f
cd $TARGET
echo "installing node packages"
npm install
echo "restarting ${PROCESS} process"
pm2 restart process