Stupid easy deploy golang app in ubuntu server with nginx

Rifki (Kubid) Fauzi
2 min readAug 8, 2020

I’m curious about deploying golang in ubuntu server, since with go you can compile your application with go build main.go and it will create an executable file. so all you need to do to run your app is by calling the compiled app by executing ./main

you can deploy your app using a systemctl services, containerize your app with docker. but is there any easy way to just up and run your golang app in production ? yes.. the step is very easy.. all you need is:

  1. run your app in background.
  2. use nginx to bind your domain to your server. that’s it.

of course, this is not a best practice.. this method used only for fun.. you want your application up and run in matter of seconds. let’s breakdown the steps.

STEPS

  1. ./main& it means by adding & at the end, your application will run in the background. but still attached to the current terminal session, so when you quit your ssh connection. the process will be terminated. if you run multiple background command you can type jobs in your terminal then it will display all your background process.
  2. disown I use disown to detach the process from the terminal session, from the documentation disown command still attached to the terminal session, but somehow. mine is working.
  3. best practice, use nohup instead of disown , eg: nohup ./main &
  4. setup nginx using proxy forwarding, since my app running on port 2001, here is my configuration
server {
listen 80;
server_name mydomain.com;
location / {
proxy_pass http://127.0.0.1:2001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

that’s it, I can access my trough my domain.. in this example is mydomain.com

--

--