Kumar Gaurav About

Deploying a Golang app on Linux Server

This post lists the steps to get a golang app running on a linux server


Background

I am writing a recommendation engine in Golang and wanted to put up the initial version on the web. Hence this post. Please note that this may not be the recommended way. Quick and Dirty is the key phrase here.

Steps

  • Get a VPS. I got one from Digital Ocean. Use this referral link to get some free credits for you and me.
  • Secure the VPS(relatively speaking). Since my server was running Ubuntu, I followed this tutorial
  • Cross compile your golang for the target OS and Architecture. More info here. More info here regarding OS and ARCH.
$ env GOOS=linux GOARCH=amd64 go build -v github.com/<github_id>/<project_path>
  • SCP the executable
$ scp <executable> <username>@<ip>:/home/<username>/<app_dir>
  • SCP any other assets required. In my case, I keep all assets inside a folder called static and htmls in a folder called templates so
$ rsync -av templates <username>@<ip>:/home/<username>/<app_dir>
$ rsync -av static <username>@<ip>:/home/<username>/<app_dir>
  • Since I am using RethinkDB, it needs to be installed. More info here
$ source /etc/lsb-release && echo "deb http://download.rethinkdb.com/apt $DISTRIB_CODENAME main" | sudo tee /etc/apt/sources.list.d/rethinkdb.list
$ wget -qO- https://download.rethinkdb.com/apt/pubkey.gpg | sudo apt-key add -
$ sudo apt-get update
$ sudo apt-get install rethinkdb
  • You may want to run RethinkDB as a service
$ sudo cp /etc/rethinkdb/default.conf.sample /etc/rethinkdb/instances.d/instance1.conf
$ sudo vim /etc/rethinkdb/instances.d/instance1.conf
$ sudo /etc/init.d/rethinkdb restart
  • You may have to whitelist the port your app is running on
# if you are using ufw
$ sudo ufw allow 8081/tcp
  • Run the binary. You may want to run it with nohup so that the process stays alive even after your session. Remember, quick and dirty. There are other better ways to do this.
$ nohup ./<binary_name> &
  • Access it at http://[ip]:[port]

  • Stop the process

# This will give you the pid
$ ps -aux | grep <binary_name>
# use the pid obtained above
$ kill -9 <pid>