deploying go to a vpn ubuntu server with github and ssl

Deploy a go application to a small server with SSL support and Github as CI/CD

Assumptions:

  • root repository contain main.go with the package name `aocweb`

  • we are using small VPS with ubuntu 20.04

  • app will be deployed to /home/web/aocweb

  • App runs HTTP on PORT 8080

Setup user and home

some VPS have root as default user, if not already we can create a user. In this example web

adduser web
usermod -aG sudo web

Install NGINX & Letsencrypt

sudo apt-get update
sudo apt-get install nginx
sudo ufw allow 'Nginx HTTP'

sudo apt-get install certbot python3-certbot-nginx
sudo certbot --nginx

Generate certificate

to test auto renewal

Add /etc/nginx/sites-enabled/example.com

Install systemd service

if the application requires environment we can put it on /home/web/aocweb/.env

create /etc/systemd/system/aocweb.service

To make sure we can run systemctl restart with sudo without password. Create sudo configuration file (make sure to use visudo so you don't accidentally locked your self.

With content

Deploy with Github Action

Generate SSH key for deployment

example with that command I created 2 files on ~/.ssh/ : aocweb & aocweb.pub

add generated public key to server

Copy the content of aocweb.pub to /home/web/.ssh/authorized_key. This allows login with ssh private key. Test that you can login to the server with that key (check ssh -v output)

Add secrets

create these secrets on the github repository

  • HOST

  • KEY

  • USERNAME

KEY is the content of aocweb.pub ssh key

Github workflow

create .github/workflows/release.yaml

Last updated

Was this helpful?